Sha256: 8c452a6007e883fec1e54fee7e9ded6cb57c75ff4ff3e998230de312da2f7adb

Contents?: true

Size: 1.64 KB

Versions: 23

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

module ActionView
  module CacheExpiry # :nodoc: all
    class ViewReloader
      def initialize(watcher:, &block)
        @mutex = Mutex.new
        @watcher_class = watcher
        @watched_dirs = nil
        @watcher = nil
        @previous_change = false

        ActionView::PathRegistry.file_system_resolver_hooks << method(:rebuild_watcher)
      end

      def updated?
        build_watcher unless @watcher
        @previous_change || @watcher.updated?
      end

      def execute
        return unless @watcher

        watcher = nil
        @mutex.synchronize do
          @previous_change = false
          watcher = @watcher
        end
        watcher.execute
      end

      private
        def reload!
          ActionView::LookupContext::DetailsKey.clear
        end

        def build_watcher
          @mutex.synchronize do
            old_watcher = @watcher

            if @watched_dirs != dirs_to_watch
              @watched_dirs = dirs_to_watch
              new_watcher = @watcher_class.new([], @watched_dirs) do
                reload!
              end
              @watcher = new_watcher

              # We must check the old watcher after initializing the new one to
              # ensure we don't miss any events
              @previous_change ||= old_watcher&.updated?
            end
          end
        end

        def rebuild_watcher
          return unless @watcher
          build_watcher
        end

        def dirs_to_watch
          all_view_paths.uniq.sort
        end

        def all_view_paths
          ActionView::PathRegistry.all_file_system_resolvers.map(&:path)
        end
    end
  end
end

Version data entries

23 entries across 23 versions & 2 rubygems

Version Path
actionview-8.0.1 lib/action_view/cache_expiry.rb
actionview-8.0.0.1 lib/action_view/cache_expiry.rb
actionview-7.2.2.1 lib/action_view/cache_expiry.rb
actionview-8.0.0 lib/action_view/cache_expiry.rb
actionview-7.2.2 lib/action_view/cache_expiry.rb
actionview-8.0.0.rc2 lib/action_view/cache_expiry.rb
actionview-7.2.1.2 lib/action_view/cache_expiry.rb
actionview-8.0.0.rc1 lib/action_view/cache_expiry.rb
actionview-7.2.1.1 lib/action_view/cache_expiry.rb
actionview-8.0.0.beta1 lib/action_view/cache_expiry.rb
omg-actionview-8.0.0.alpha9 lib/action_view/cache_expiry.rb
omg-actionview-8.0.0.alpha8 lib/action_view/cache_expiry.rb
omg-actionview-8.0.0.alpha7 lib/action_view/cache_expiry.rb
omg-actionview-8.0.0.alpha4 lib/action_view/cache_expiry.rb
omg-actionview-8.0.0.alpha3 lib/action_view/cache_expiry.rb
omg-actionview-8.0.0.alpha2 lib/action_view/cache_expiry.rb
omg-actionview-8.0.0.alpha1 lib/action_view/cache_expiry.rb
actionview-7.2.1 lib/action_view/cache_expiry.rb
actionview-7.2.0 lib/action_view/cache_expiry.rb
actionview-7.2.0.rc1 lib/action_view/cache_expiry.rb