Sha256: b27aaa9523697c834119f822a9e5ffdc84805b439339f33b2f5397d018d63dd2

Contents?: true

Size: 1.5 KB

Versions: 19

Compression:

Stored size: 1.5 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

        rebuild_watcher

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

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

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

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

        def rebuild_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 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

19 entries across 19 versions & 5 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/actionview-7.1.3.4/lib/action_view/cache_expiry.rb
actionview-7.1.5.1 lib/action_view/cache_expiry.rb
actionview-7.1.5 lib/action_view/cache_expiry.rb
actionview-7.1.4.2 lib/action_view/cache_expiry.rb
actionview-7.1.4.1 lib/action_view/cache_expiry.rb
actionview-7.1.4 lib/action_view/cache_expiry.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actionview-7.1.3.4/lib/action_view/cache_expiry.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/actionview-7.1.3.4/lib/action_view/cache_expiry.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/actionview-7.1.3.4/lib/action_view/cache_expiry.rb
actionview-7.1.3.4 lib/action_view/cache_expiry.rb
actionview-7.1.3.2 lib/action_view/cache_expiry.rb
actionview-7.1.3.1 lib/action_view/cache_expiry.rb
actionview-7.1.3 lib/action_view/cache_expiry.rb
actionview-7.1.2 lib/action_view/cache_expiry.rb
actionview-7.1.1 lib/action_view/cache_expiry.rb
actionview-7.1.0 lib/action_view/cache_expiry.rb
actionview-7.1.0.rc2 lib/action_view/cache_expiry.rb
actionview-7.1.0.rc1 lib/action_view/cache_expiry.rb
actionview-7.1.0.beta1 lib/action_view/cache_expiry.rb