Class: DXOpal::RemoteResource

Inherits:
Object
  • Object
show all
Defined in:
lib/dxopal/remote_resource.rb

Overview

A base class for resources acquired through JS Promises. Provides ‘<klass>.register` and `<klass>`. A subclass must impelment `<klass>._load`.

TODO: better name? (SoundEffect is not actually ‘remote’)

Direct Known Subclasses

Image, Sound

Constant Summary collapse

@@resources =

List of registered resources (Contains path_or_url)

Hash.new{|h,k| h[k] = {}}
@@promises =

Contains promises

Hash.new{|h,k| h[k] = {}}
@@instances =

Contains instances of Image, Sound

Hash.new{|h,k| h[k] = {}}
@@loaded =

‘true` if the resource is loaded

Hash.new{|h,k| h[k] = {}}
@@klasses =

Subclasses of RemoteResource

{}

Class Method Summary collapse

Class Method Details

.[](name) ⇒ Object

Return instance of loaded resource (call on subclasses)



30
31
32
33
34
35
36
# File 'lib/dxopal/remote_resource.rb', line 30

def self.[](name)
  if (ret = @@instances[_klass_name][name])
    return ret
  else
    raise "#{_klass_name} #{name.inspect} is not registered"
  end
end

._klass_nameObject

Return a string like “Image” or “Sound”



69
70
71
# File 'lib/dxopal/remote_resource.rb', line 69

def self._klass_name
  return self.name.split(/::/).last
end

._load(*args) ⇒ Object

Load actual content (defined on subclasses) Return ‘[instance, promise]`



64
65
66
# File 'lib/dxopal/remote_resource.rb', line 64

def self._load(*args)
  raise "override me"
end

._load_resources(&block) ⇒ Object

Called from Window.load_resources



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dxopal/remote_resource.rb', line 39

def self._load_resources(&block)
  @@resources.each do |klass_name, items|
    klass = @@klasses[klass_name] 
    items.each do |name, (block2, args)|
      if !@@promises[klass_name][name]
        instance, promise = klass._load(*args, &block2)
        @@instances[klass_name][name] = instance
        @@loaded[klass_name][name] = false
        @@promises[klass_name][name] = promise.JS.then{ 
          @@loaded[klass_name][name] = true
          RemoteResource._update_loading_status
        }
      end
    end
  end
  promises = @@promises.values.flat_map(&:values)
  %x{
    Promise.all(#{promises}).then(function() {
      #{block.call}
    });
  }
end

._update_loading_statusObject

Update loading status if ‘dxopal-loading` is defined.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/dxopal/remote_resource.rb', line 74

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

.add_class(subklass) ⇒ Object



19
20
21
# File 'lib/dxopal/remote_resource.rb', line 19

def self.add_class(subklass)
  @@klasses[subklass._klass_name] = subklass
end

.register(name, *args, &block) ⇒ Object

Reserve instance generation



24
25
26
27
# File 'lib/dxopal/remote_resource.rb', line 24

def self.register(name, *args, &block)
  @@resources[_klass_name] ||= {}
  @@resources[_klass_name][name] = [block, args]
end