跳至內容 跳至搜尋

Active Support 重新載入器

這個類別定義了幾個回呼函式

to_prepare -- Run once at application startup, and also from
+to_run+.

to_run -- Run before a work run that is reloading. If
+reload_classes_only_on_change+ is true (the default), the class
unload will have already occurred.

to_complete -- Run after a work run that has reloaded. If
+reload_classes_only_on_change+ is false, the class unload will
have occurred after the work run, but before this callback.

before_class_unload -- Run immediately before the classes are
unloaded.

after_class_unload -- Run immediately after the classes are
unloaded.
方法
A
B
N
R
T
W

類別公開方法

after_class_unload(*args, &block)

註冊一個在類別卸載後立即執行的回呼函式。

# File activesupport/lib/active_support/reloader.rb, line 44
def self.after_class_unload(*args, &block)
  set_callback(:class_unload, :after, *args, &block)
end

before_class_unload(*args, &block)

註冊一個在類別卸載前立即執行的回呼函式。

# File activesupport/lib/active_support/reloader.rb, line 39
def self.before_class_unload(*args, &block)
  set_callback(:class_unload, *args, &block)
end

new()

# File activesupport/lib/active_support/reloader.rb, line 99
def initialize
  super
  @locked = false
end

reload!()

啟動手動重新載入

# File activesupport/lib/active_support/reloader.rb, line 51
def self.reload!
  executor.wrap do
    new.tap do |instance|
      instance.run!
    ensure
      instance.complete!
    end
  end
  prepare!
end

to_prepare(*args, &block)

註冊一個在應用程式啟動時和每次程式碼重新載入時都會執行一次的回呼函式。

# File activesupport/lib/active_support/reloader.rb, line 34
def self.to_prepare(*args, &block)
  set_callback(:prepare, *args, &block)
end

wrap(**kwargs)

將提供的程式碼塊作為一個工作單元運行,並根據需要重新載入程式碼

# File activesupport/lib/active_support/reloader.rb, line 71
def self.wrap(**kwargs)
  return yield if active?

  executor.wrap(**kwargs) do
    instance = run!
    begin
      yield
    ensure
      instance.complete!
    end
  end
end

實例公開方法

release_unload_lock!()

如果先前已取得卸載鎖定,則釋放它

# File activesupport/lib/active_support/reloader.rb, line 114
def release_unload_lock!
  if @locked
    @locked = false
    ActiveSupport::Dependencies.interlock.done_unloading
  end
end

require_unload_lock!()

取得 ActiveSupport::Dependencies::Interlock 卸載鎖定,確保它會自動釋放

# File activesupport/lib/active_support/reloader.rb, line 106
def require_unload_lock!
  unless @locked
    ActiveSupport::Dependencies.interlock.start_unloading
    @locked = true
  end
end