Class: Proc

Inherits:
Object show all
Defined in:
lib/volt/reactive/computation.rb

Instance Method Summary collapse

Instance Method Details

#watch!Object



159
160
161
162
163
164
165
166
167
# 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) ⇒ 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:

-> { }


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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# 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) ⇒ Volt::Computation

Watches a proc until the value returned equals the passed in value. When the value matches, the block is called.

Parameters:

  • the

    value to match

Returns:



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# 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