Class: FluidCLI::Theme::DevServer::HotReload
- Inherits:
-
Object
- Object
- FluidCLI::Theme::DevServer::HotReload
- Defined in:
- lib/fluid_cli/theme/dev_server/hot_reload.rb,
lib/fluid_cli/theme/dev_server/hot_reload/script_injector.rb
Defined Under Namespace
Classes: ScriptInjector
Instance Method Summary collapse
- #call(env) ⇒ Object
- #close ⇒ Object
-
#initialize(ctx, app, broadcast_hooks: [], script_injector: nil, watcher:, mode:) ⇒ HotReload
constructor
A new instance of HotReload.
- #notify_streams_of_file_change(modified, added, removed) ⇒ Object
Constructor Details
#initialize(ctx, app, broadcast_hooks: [], script_injector: nil, watcher:, mode:) ⇒ HotReload
Returns a new instance of HotReload.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/fluid_cli/theme/dev_server/hot_reload.rb', line 7 def initialize(ctx, app, broadcast_hooks: [], script_injector: nil, watcher:, mode:) @ctx = ctx @app = app @mode = mode @broadcast_hooks = broadcast_hooks @script_injector = script_injector @streams = SSE::Streams.new @watcher = watcher @watcher.add_observer(self, :notify_streams_of_file_change) end |
Instance Method Details
#call(env) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/fluid_cli/theme/dev_server/hot_reload.rb', line 18 def call(env) path = env["PATH_INFO"] if path == "/hot-reload" create_stream else status, headers, body = @app.call(env) if request_is_html?(headers) && leads_to_injectable_body?(path) body = inject_hot_reload_javascript(body) end [status, headers, body] end end |
#close ⇒ Object
33 34 35 |
# File 'lib/fluid_cli/theme/dev_server/hot_reload.rb', line 33 def close @streams.close end |
#notify_streams_of_file_change(modified, added, removed) ⇒ Object
37 38 39 40 41 |
# File 'lib/fluid_cli/theme/dev_server/hot_reload.rb', line 37 def notify_streams_of_file_change(modified, added, removed) @broadcast_hooks.each do |hook| hook.call(modified, added, removed, streams: @streams) end end |