Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 5b6bb8074353
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: c96d7d190120
Choose a head ref
  • 2 commits
  • 4 files changed
  • 1 contributor

Commits on Nov 23, 2014

  1. Copy the full SHA
    ac265d5 View commit details
  2. Copy the full SHA
    c96d7d1 View commit details
Showing with 34 additions and 19 deletions.
  1. +16 −12 lib/opal/builder.rb
  2. +4 −3 lib/opal/builder/cache_store.rb
  3. +12 −2 lib/opal/builder/cached_asset.rb
  4. +2 −2 lib/opal/sprockets/cache_store.rb
28 changes: 16 additions & 12 deletions lib/opal/builder.rb
Original file line number Diff line number Diff line change
@@ -13,6 +13,9 @@ class Builder
# A set of paths which have been processed already.
attr_reader :processed

# Array of compiled assets (either cached, or re-processed).
attr_reader :assets

def initialize(options = nil)
(options || {}).each_pair do |k,v|
public_send("#{k}=", v)
@@ -67,9 +70,6 @@ def process_require(filename, options)
process_requires asset, path, options

@assets << asset

# TODO: cache asset (should check for cache_store first)
# cache_store.store filename, asset.to_s, asset.requires
end

def process_requires(asset, path, options)
@@ -79,9 +79,7 @@ def process_requires(asset, path, options)
end

def find_asset(path)
if asset = cached_asset(path)
asset
else
cached_asset(path) do
source = stub?(path) ? '' : read(path)

if source.nil?
@@ -93,15 +91,23 @@ def find_asset(path)
end

fname = path_reader.expand(path).to_s
asset = processor_for(source, path, fname, requirable: true)

processor_for(source, path, fname, requirable: true)
end
end

def cached_asset(path)
asset = cache_store.retrieve(path)
asset = cache_store[path]

if asset and asset.fresh?
return asset
if asset && asset.fresh?(self)
asset
else
asset = yield

# TODO: cache asset (should check for cache_store first)
# cache_store[path] = asset

asset
end
end

@@ -113,8 +119,6 @@ def source_map
assets.map(&:source_map).reduce(:+).as_json.to_json
end

attr_reader :assets

attr_accessor :processors, :default_processor, :path_reader,
:compiler_options, :stubs, :prerequired, :preload

7 changes: 4 additions & 3 deletions lib/opal/builder/cache_store.rb
Original file line number Diff line number Diff line change
@@ -3,16 +3,17 @@ class Builder
# Simple Memory based cache store. This is used generally in
# non-sprockets environments, or in a sprocket environment which does
# not have a cache predefined.
#
class CacheStore
def initialize
@cache = {}
end

def store(key, contents, requires)
@cache[key] = {:contents => contents, :requires => requires}
def []=(key, asset)
@cache[key] = asset.encode
end

def retrieve(key)
def [](key)
if hash = @cache[key]
return CachedAsset.new(hash)
else
14 changes: 12 additions & 2 deletions lib/opal/builder/cached_asset.rb
Original file line number Diff line number Diff line change
@@ -16,12 +16,21 @@ def initialize(data)
@data = data
end

# When re-encoding a cached asset, we just pass back the initial data.
# A cached asset doesn't usually need to be re-encoded back to the
# cache, as it is already in the cache.
#
def encode
puts "warning: trying to re-encode a cached asset"
@data.dup
end

def requires
@data[:requires]
@data['requires']
end

def to_s
@data[:contents]
@data['source']
end

def source_map
@@ -42,6 +51,7 @@ def fresh?(builder)
# TODO: for now, always assume stale assets
false
end

end
end
end
4 changes: 2 additions & 2 deletions lib/opal/sprockets/cache_store.rb
Original file line number Diff line number Diff line change
@@ -8,12 +8,12 @@ def initialize(environment)
@environment = environment
end

def store(key, contents, requires)
def []=(key, asset)
environment.cache_set("opal/#{key}.cache", {
:contents => contents, :requires => requires})
end

def retrieve(key)
def [](key)
if obj = environment.cache_get("opal/#{key}.cache")
return CachedAsset.new(obj)
else