Class: Proc

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

Instance Method Summary collapse

Instance Method Details

#watch!Object



133
134
135
136
137
138
139
140
141
# File 'lib/volt/reactive/computation.rb', line 133

def watch!
  computation = Volt::Computation.new(self)

  # Initial run
  computation.compute!

  # return the computation
  computation
end

#watch_and_resolve!Object

Does an watch and if the result is a promise, resolves the promise. #watch_and_resolve! takes a block that will be passed the resolved results of the proc.

Example:

-> { }


175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/volt/reactive/computation.rb', line 175

def watch_and_resolve!
  unless block_given?
    raise "watch_and_resolve! requires a block to call when the value is resolved or another value other than a promise is returned in the watch."
  end

  computation = Proc.new do
    result = self.call

    if result.is_a?(Promise)
      result.then do |final|
        yield(final)
      end
    else
      yield(result)
    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.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/volt/reactive/computation.rb', line 148

def watch_until!(value, &block)
  computation = Proc.new do |comp|
    # First fetch the value
    result = self.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