Module: Rack::Async::AutoloadHook

Included in:
Rack, Session
Defined in:
lib/rack/async/autoload_hook.rb

Instance Method Summary collapse

Instance Method Details

#autoload(const_name, path) ⇒ Object

Hooks into Rack.autoload to allow replacing already defined autoload paths with the paths to the async counterparts but avoids accidentally overwriting them with the old paths, for instance if Rack::Reloader is used.

If the middleware is already loaded, require the counterpart.



11
12
13
14
15
16
17
18
# File 'lib/rack/async/autoload_hook.rb', line 11

def autoload(const_name, path)
  if const_defined?(const_name) and not autoload?(const_name)
    require path
  elsif autoload?(const_name) !~ /^rack\/async/
    @autoloaded = false
    super
  end
end

#autoload!Object

Module#autoload is not thread-safe. Use Rack.autoload! to preload all middleware in order to avoid race conditions.



23
24
25
26
27
28
29
30
# File 'lib/rack/async/autoload_hook.rb', line 23

def autoload!
  return if @autoloaded
  @autoloaded = true
  constants.each do |const_name|
    const = const_get(const_name)
    const.autoload! if const.respond_to? :autoload!
  end
end