Class: AsyncProxy::ComputedProxy
- Inherits:
-
ObjectProxy
- Object
- ObjectProxy
- AsyncProxy::ComputedProxy
- Defined in:
- lib/computed_proxy.rb
Overview
a specialization of ObjectProxy to be used when the wraped value is dependent on another async proxy and a computation (a block)
will be automatically created by calling a method or when_ready in any async object
the user should not directly create objects of this class
Instance Attribute Summary collapse
-
#callable ⇒ Object
readonly
Returns the value of attribute callable.
-
#proc ⇒ Object
readonly
Returns the value of attribute proc.
Instance Method Summary collapse
- #async ⇒ Object
-
#initialize(callable, proc) ⇒ ComputedProxy
constructor
A new instance of ComputedProxy.
- #launch_computation ⇒ Object
- #ready? ⇒ Boolean
-
#sync(options = {}) ⇒ Object
waits for the computation to finish and returns the actual result.
- #wait_for_computation ⇒ Object
Methods inherited from ObjectProxy
#each, #map, #register_callback, #to_s, #when_ready
Constructor Details
#initialize(callable, proc) ⇒ ComputedProxy
Returns a new instance of ComputedProxy.
16 17 18 19 20 |
# File 'lib/computed_proxy.rb', line 16 def initialize(callable, proc) @callable = callable @proc = proc @callbacks = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class AsyncProxy::ObjectProxy
Instance Attribute Details
#callable ⇒ Object (readonly)
Returns the value of attribute callable.
13 14 15 |
# File 'lib/computed_proxy.rb', line 13 def callable @callable end |
#proc ⇒ Object (readonly)
Returns the value of attribute proc.
14 15 16 |
# File 'lib/computed_proxy.rb', line 14 def proc @proc end |
Instance Method Details
#async ⇒ Object
26 27 28 |
# File 'lib/computed_proxy.rb', line 26 def async self end |
#launch_computation ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/computed_proxy.rb', line 44 def launch_computation @thread = Thread.new do @result = @proc.call(@callable.sync) @ready = true run_callbacks end end |
#ready? ⇒ Boolean
22 23 24 |
# File 'lib/computed_proxy.rb', line 22 def ready? @ready end |
#sync(options = {}) ⇒ Object
waits for the computation to finish and returns the actual result
optional arguments:
- timeout: the maximum number of seconds that the computation can take.
35 36 37 38 39 40 41 42 |
# File 'lib/computed_proxy.rb', line 35 def sync( = {}) if [:timeout] SystemTimer.timeout_after([:timeout]){wait_for_computation} else wait_for_computation end @result end |
#wait_for_computation ⇒ Object
52 53 54 55 |
# File 'lib/computed_proxy.rb', line 52 def wait_for_computation callable.sync # ensures the callable has finished and run its callbacks @thread.join unless @done end |