2019-04-01 15:22:57 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ActionView
|
|
|
|
class CacheExpiry
|
|
|
|
class Executor
|
|
|
|
def initialize(watcher:)
|
2021-04-11 01:07:09 -04:00
|
|
|
@execution_lock = Concurrent::ReadWriteLock.new
|
|
|
|
@cache_expiry = ViewModificationWatcher.new(watcher: watcher) do
|
|
|
|
clear_cache
|
|
|
|
end
|
2019-04-01 15:22:57 -04:00
|
|
|
end
|
|
|
|
|
2021-04-11 01:07:09 -04:00
|
|
|
def run
|
|
|
|
ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
|
|
|
|
@cache_expiry.execute_if_updated
|
|
|
|
@execution_lock.acquire_read_lock
|
|
|
|
end
|
2019-04-01 15:22:57 -04:00
|
|
|
end
|
|
|
|
|
2021-04-11 01:07:09 -04:00
|
|
|
def complete(_)
|
|
|
|
@execution_lock.release_read_lock
|
|
|
|
end
|
2019-06-03 19:20:55 -04:00
|
|
|
|
2021-04-11 01:07:09 -04:00
|
|
|
private
|
|
|
|
def clear_cache
|
|
|
|
@execution_lock.with_write_lock do
|
|
|
|
ActionView::LookupContext::DetailsKey.clear
|
2019-05-21 22:39:44 -04:00
|
|
|
end
|
2019-04-01 15:22:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-11 01:07:09 -04:00
|
|
|
class ViewModificationWatcher
|
|
|
|
def initialize(watcher:, &block)
|
|
|
|
@watched_dirs = nil
|
|
|
|
@watcher_class = watcher
|
|
|
|
@watcher = nil
|
|
|
|
@mutex = Mutex.new
|
|
|
|
@block = block
|
2019-04-01 15:22:57 -04:00
|
|
|
end
|
|
|
|
|
2021-04-11 01:07:09 -04:00
|
|
|
def execute_if_updated
|
|
|
|
@mutex.synchronize do
|
|
|
|
watched_dirs = dirs_to_watch
|
|
|
|
return if watched_dirs.empty?
|
|
|
|
|
|
|
|
if watched_dirs != @watched_dirs
|
|
|
|
@watched_dirs = watched_dirs
|
|
|
|
@watcher = @watcher_class.new([], watched_dirs, &@block)
|
|
|
|
@watcher.execute
|
|
|
|
else
|
|
|
|
@watcher.execute_if_updated
|
|
|
|
end
|
|
|
|
end
|
2019-04-01 15:22:57 -04:00
|
|
|
end
|
2021-04-11 01:07:09 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
def dirs_to_watch
|
|
|
|
all_view_paths.grep(FileSystemResolver).map!(&:path).tap(&:uniq!).sort!
|
|
|
|
end
|
|
|
|
|
|
|
|
def all_view_paths
|
|
|
|
ActionView::ViewPaths.all_view_paths.flat_map(&:paths)
|
|
|
|
end
|
|
|
|
end
|
2019-04-01 15:22:57 -04:00
|
|
|
end
|
|
|
|
end
|