Class: Computation
Constant Summary collapse
- @@current =
nil
- @@flush_queue =
[]
Class Method Summary collapse
- .current ⇒ Object
- .current=(val) ⇒ Object
- .flush! ⇒ Object
- .queue_flush! ⇒ Object
- .run_without_tracking ⇒ Object
Instance Method Summary collapse
-
#compute! ⇒ Object
Runs the computation.
-
#initialize(computation) ⇒ Computation
constructor
A new instance of Computation.
-
#invalidate! ⇒ Object
Calling invalidate removes the computation from all of its dependencies.
- #on_invalidate(&callback) ⇒ Object
-
#run_in ⇒ Object
Runs in this computation as the current computation, returns the computation.
-
#stop ⇒ Object
Stop re-run of the computations.
Constructor Details
#initialize(computation) ⇒ Computation
Returns a new instance of Computation.
14 15 16 17 |
# File 'lib/volt/reactive/computation.rb', line 14 def initialize(computation) @computation = computation @invalidations = [] end |
Class Method Details
.current ⇒ Object
10 11 12 |
# File 'lib/volt/reactive/computation.rb', line 10 def self.current @@current end |
.current=(val) ⇒ Object
6 7 8 |
# File 'lib/volt/reactive/computation.rb', line 6 def self.current=(val) @@current = val end |
.flush! ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/volt/reactive/computation.rb', line 98 def self.flush! raise "Can't flush while in a flush" if @flushing @flushing = true # clear any timers @timer = nil computations = @@flush_queue @@flush_queue = [] computations.each do |computation| computation.compute! end @flushing = false end |
.queue_flush! ⇒ Object
115 116 117 118 119 120 |
# File 'lib/volt/reactive/computation.rb', line 115 def self.queue_flush! if !@timer # Flush once everything else has finished running @timer = `setImmediate(function() { self['$flush!'](); });` end end |
.run_without_tracking ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/volt/reactive/computation.rb', line 88 def self.run_without_tracking previous = Computation.current Computation.current = nil return_value = yield Computation.current = previous return return_value end |
Instance Method Details
#compute! ⇒ Object
Runs the computation
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/volt/reactive/computation.rb', line 20 def compute! @invalidated = false unless @stopped @computing = true run_in do @computation.call end @computing = false end end |
#invalidate! ⇒ Object
Calling invalidate removes the computation from all of its dependencies. This keeps its dependencies from invalidating it again.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/volt/reactive/computation.rb', line 48 def invalidate! unless @invalidated @invalidated = true if !@stopped && !@computing @@flush_queue << self # If we are in the browser, we queue a flush for the next tick if Volt.in_browser? self.class.queue_flush! end end invalidations = @invalidations @invalidations = [] invalidations.each do |invalidation| invalidation.call end end end |
#on_invalidate(&callback) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/volt/reactive/computation.rb', line 33 def on_invalidate(&callback) if @invalidated # Call invalidate now, since its already invalidated Computation.run_without_tracking do callback.call end else # Store the invalidation @invalidations << callback end end |
#run_in ⇒ Object
Runs in this computation as the current computation, returns the computation
79 80 81 82 83 84 85 86 |
# File 'lib/volt/reactive/computation.rb', line 79 def run_in previous = Computation.current Computation.current = self yield Computation.current = previous return self end |
#stop ⇒ Object
Stop re-run of the computations
71 72 73 74 75 76 |
# File 'lib/volt/reactive/computation.rb', line 71 def stop unless @stopped @stopped = true invalidate! end end |