Class: Proc
Instance Method Summary collapse
- #watch! ⇒ Object
- 
  
    
      #watch_and_resolve!(success, failure = nil, yield_nil_for_unresolved_promise = false)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Does an watch and if the result is a promise, resolves the promise. 
- 
  
    
      #watch_until!(value, &block)  ⇒ Volt::Computation 
    
    
  
  
  
  
  
  
  
  
  
    Watches a proc until the value returned equals the passed in value. 
Instance Method Details
#watch! ⇒ Object
| 139 140 141 142 143 144 145 146 147 | # File 'lib/volt/reactive/computation.rb', line 139 def watch! computation = Volt::Computation.new(self) # Initial run computation.compute! # return the computation computation end | 
#watch_and_resolve!(success, failure = nil, yield_nil_for_unresolved_promise = false) ⇒ Object
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:
-> { }
| 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | # File 'lib/volt/reactive/computation.rb', line 181 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) ⇒ Volt::Computation
Watches a proc until the value returned equals the passed in value. When the value matches, the block is called.
| 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | # File 'lib/volt/reactive/computation.rb', line 154 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 |