Skip to content

Use a Hash for caching things #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions lib/importmap/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class InvalidFile < StandardError; end

def initialize
@packages, @directories = {}, {}
@cache = {}
end

def draw(path = nil, &block)
Expand Down Expand Up @@ -87,24 +88,15 @@ def cache_sweeper(watches: nil)
MappedFile = Struct.new(:name, :path, :preload, keyword_init: true)

def cache_as(name)
if result = instance_variable_get("@cached_#{name}")
if result = @cache[name.to_s]
result
else
remember_cache_key(name)
instance_variable_set("@cached_#{name}", yield)
@cache[name.to_s] = yield
end
end

def remember_cache_key(name)
@cache_keys ||= Set.new
@cache_keys.add name
end


def clear_cache
@cache_keys&.each do |name|
instance_variable_set("@cached_#{name}", nil)
end
@cache.clear
end

def rescuable_asset_error?(error)
Expand Down