2019-04-01 15:22:57 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ActionView
|
|
|
|
class CacheExpiry
|
|
|
|
class Executor
|
|
|
|
def initialize(watcher:)
|
|
|
|
@cache_expiry = CacheExpiry.new(watcher: watcher)
|
|
|
|
end
|
|
|
|
|
|
|
|
def before(target)
|
|
|
|
@cache_expiry.clear_cache_if_necessary
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(watcher:)
|
|
|
|
@watched_dirs = nil
|
|
|
|
@watcher_class = watcher
|
|
|
|
@watcher = nil
|
2019-05-21 22:39:44 -04:00
|
|
|
@mutex = Mutex.new
|
2019-04-01 15:22:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def clear_cache_if_necessary
|
2019-05-21 22:39:44 -04:00
|
|
|
@mutex.synchronize do
|
|
|
|
watched_dirs = dirs_to_watch
|
2019-06-03 19:20:55 -04:00
|
|
|
return if watched_dirs.empty?
|
|
|
|
|
2019-05-21 22:39:44 -04:00
|
|
|
if watched_dirs != @watched_dirs
|
|
|
|
@watched_dirs = watched_dirs
|
|
|
|
@watcher = @watcher_class.new([], watched_dirs) do
|
|
|
|
clear_cache
|
|
|
|
end
|
|
|
|
@watcher.execute
|
|
|
|
else
|
|
|
|
@watcher.execute_if_updated
|
2019-04-01 15:22:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_cache
|
|
|
|
ActionView::LookupContext::DetailsKey.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def dirs_to_watch
|
2020-05-22 14:31:57 -04:00
|
|
|
all_view_paths.grep(FileSystemResolver).map!(&:path).tap(&:uniq!).sort!
|
2019-04-01 15:22:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def all_view_paths
|
|
|
|
ActionView::ViewPaths.all_view_paths.flat_map(&:paths)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|