Class: ShopifyCLI::Theme::DevServer::HotReload

Inherits:
Object
  • Object
show all
Defined in:
lib/shopify_cli/theme/dev_server/hot_reload.rb,
lib/shopify_cli/theme/dev_server/hot_reload/sections_index.rb,
lib/shopify_cli/theme/dev_server/hot_reload/script_injector.rb,
lib/shopify_cli/theme/dev_server/hot_reload/remote_file_deleter.rb,
lib/shopify_cli/theme/dev_server/hot_reload/remote_file_reloader.rb

Direct Known Subclasses

Extension::DevServer::HotReload

Defined Under Namespace

Classes: RemoteFileDeleter, RemoteFileReloader, ScriptInjector, SectionsIndex

Instance Method Summary collapse

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/shopify_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/shopify_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

#closeObject



33
34
35
# File 'lib/shopify_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/shopify_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