class Proc
Public Instance Methods
watch!()
click to toggle source
# File lib/volt/reactive/computation.rb, line 159 def watch! computation = Volt::Computation.new(self) # Initial run computation.compute!(true) # return the computation computation end
watch_and_resolve!(success, failure=nil, yield_nil_for_unresolved_promise=false)
click to toggle source
Does an watch and if the result is a promise, resolves the promise. watch_and_resolve!
takes two procs, one for the promise resolution (then), and one for promise rejection (fail).
Example:
-> { }
# File lib/volt/reactive/computation.rb, line 201 def watch_and_resolve!(success, failure=nil, yield_nil_for_unresolved_promise=false) # Keep results between runs result = nil computation = proc do |comp| result = call last_promise = nil if result.is_a?(Promise) last_promise = result # Often you want a to be alerted that an unresolved promise is waiting # to be resolved. if yield_nil_for_unresolved_promise && !result.resolved? success.call(nil) end # The handler gets called once the promise resolves or is rejected. handler = lambda do |&after_handle| # Check to make sure that a new value didn't get reactively pushed # before the promise resolved. if last_promise.is_a?(Promise) && last_promise == result # Don't resolve if the computation was stopped unless comp.stopped? # Call the passed in proc after_handle.call end # Clear result for GC result = nil end end result.then do |final| # Call the success proc passing in the resolved value handler.call { success.call(final) } end.fail do |err| # call the fail callback, passing in the error handler.call { failure.call(err) if failure } end else success.call(result) # Clear result for GC result = nil end end.watch! # Return the computation computation end
watch_until!(value, &block)
click to toggle source
Watches a proc until the value returned equals the passed in value. When the value matches, the block is called.
@param the value to match @return [Volt::Computation] the initial computation is returned.
# File lib/volt/reactive/computation.rb, line 174 def watch_until!(value, &block) computation = proc do |comp| # First fetch the value result = call if result == value # Values match # call the block Volt::Computation.run_without_tracking do block.call end # stop the computation comp.stop end end.watch! computation end