Sha256: 7269ae31e077b21498015bff55c6422ffd4c2fd709776d5c8ff336ccf4b00813

Contents?: true

Size: 1.86 KB

Versions: 14

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

module ShopifyCLI
  module Theme
    module Extension
      class DevServer
        module Hooks
          class FileChangeHook
            attr_reader :ctx, :extension, :syncer, :streams

            def initialize(ctx, extension:, syncer:)
              @ctx = ctx
              @extension = extension
              @syncer = syncer
            end

            def call(modified, added, removed, streams: nil)
              @streams = streams

              modified = paths(modified).select { |file| @extension.extension_file?(file) }
              added = paths(added).select { |file| @extension.extension_file?(file) }
              removed = paths(removed)

              hot_reload(modified) unless modified.empty?
              reload_page(added, removed) unless (added + removed).empty?
            end

            private

            def hot_reload(modified)
              broadcast(modified: modified)

              ctx.debug("[HotReload] Modified: #{modified.join(", ")}")
            end

            def reload_page(added, removed)
              wait_blocking_operations

              broadcast(reload_page: true)

              ctx.debug("[ReloadPage] Added: #{added.join(", ")}")
              ctx.debug("[ReloadPage] Removed: #{removed.join(", ")}")
            end

            def wait_blocking_operations
              retries = 10
              while syncer.any_blocking_operation? && !retries.zero?
                sleep(0.5)
                retries -= 1
              end
            end

            def paths(files)
              files
                .map { |file| extension[file] }
                .reject(&:liquid_css?)
                .map(&:relative_path)
            end

            def broadcast(message)
              streams&.broadcast(JSON.generate(message))
            end
          end
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
shopify-cli-2.36.0 lib/shopify_cli/theme/extension/dev_server/hooks/file_change_hook.rb
shopify-cli-2.35.0 lib/shopify_cli/theme/extension/dev_server/hooks/file_change_hook.rb
shopify-cli-2.34.0 lib/shopify_cli/theme/extension/dev_server/hooks/file_change_hook.rb
shopify-cli-2.33.1 lib/shopify_cli/theme/extension/dev_server/hooks/file_change_hook.rb
shopify-cli-2.33.0 lib/shopify_cli/theme/extension/dev_server/hooks/file_change_hook.rb
shopify-cli-2.32.1 lib/shopify_cli/theme/extension/dev_server/hooks/file_change_hook.rb
shopify-cli-2.32.0 lib/shopify_cli/theme/extension/dev_server/hooks/file_change_hook.rb
shopify-cli-2.31.0 lib/shopify_cli/theme/extension/dev_server/hooks/file_change_hook.rb
shopify-cli-2.30.0 lib/shopify_cli/theme/extension/dev_server/hooks/file_change_hook.rb
shopify-cli-2.29.0 lib/shopify_cli/theme/extension/dev_server/hooks/file_change_hook.rb
shopify-cli-2.28.0 lib/shopify_cli/theme/extension/dev_server/hooks/file_change_hook.rb
shopify-cli-2.27.0 lib/shopify_cli/theme/extension/dev_server/hooks/file_change_hook.rb
shopify-cli-2.26.0 lib/shopify_cli/theme/extension/dev_server/hooks/file_change_hook.rb
shopify-cli-2.25.0 lib/shopify_cli/theme/extension/dev_server/hooks/file_change_hook.rb