Skip to content

Commit

Permalink
Merge pull request #54 from yhara/loading
Browse files Browse the repository at this point in the history
Show loading status
  • Loading branch information
yhara authored Jul 9, 2024
2 parents 71d5e9c + 6d8fe7d commit 45f1976
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion lib/dxopal/remote_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class RemoteResource
@@promises = Hash.new{|h,k| h[k] = {}}
# Contains instances of Image, Sound
@@instances = Hash.new{|h,k| h[k] = {}}
# `true` if the resource is loaded
@@loaded = Hash.new{|h,k| h[k] = {}}

# Subclasses of RemoteResource
@@klasses = {}
Expand Down Expand Up @@ -41,7 +43,11 @@ def self._load_resources(&block)
if !@@promises[klass_name][name]
instance, promise = klass._load(*args, &block2)
@@instances[klass_name][name] = instance
@@promises[klass_name][name] = promise
@@loaded[klass_name][name] = false
@@promises[klass_name][name] = promise.JS.then{
@@loaded[klass_name][name] = true
RemoteResource._update_loading_status
}
end
end
end
Expand All @@ -63,5 +69,31 @@ def self._load(*args)
def self._klass_name
return self.name.split(/::/).last
end

# Update loading status if `dxopal-loading` is defined.
def self._update_loading_status
done = true
report = "DXOpal loading...\n" +
@@promises.map{|klass_name, promises|
n_total = @@resources[klass_name].count
n_loaded = @@loaded[klass_name].values.count(true)
done = false if n_loaded < n_total
"#{klass_name}: #{n_loaded}/#{n_total}\n"
}.join
div = `document.getElementById('dxopal-loading')`
if `div`
%x{
let pre = div.firstChild;
if (!pre) {
pre = document.createElement('pre');
div.appendChild(pre);
}
pre.textContent = #{report};
if (#{done}) {
div.style.display = 'none';
}
}
end
end
end
end

0 comments on commit 45f1976

Please sign in to comment.