Class: Volt::Computation

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

Constant Summary collapse

@@current =
nil
@@flush_queue =
[]

Class Method Summary collapse

Instance Method Summary collapse

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

.currentObject



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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/volt/reactive/computation.rb', line 93

def self.flush!
  fail "Can't flush while in a flush" if @flushing

  @flushing = true
  # clear any timers
  @timer    = nil

  computations  = @@flush_queue
  @@flush_queue = []

  computations.each(&:compute!)

  @flushing = false
end

.queue_flush!Object



108
109
110
111
112
113
# File 'lib/volt/reactive/computation.rb', line 108

def self.queue_flush!
  unless @timer
    # Flush once everything else has finished running
    @timer = `setImmediate(function() { self['$flush!'](); });`
  end
end

.run_without_trackingObject



85
86
87
88
89
90
91
# File 'lib/volt/reactive/computation.rb', line 85

def self.run_without_tracking
  previous            = Computation.current
  Computation.current = nil
  return_value        = yield
  Computation.current = previous
  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
# 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(&:call)
  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_inObject

Runs in this computation as the current computation, returns the computation



77
78
79
80
81
82
83
# File 'lib/volt/reactive/computation.rb', line 77

def run_in
  previous            = Computation.current
  Computation.current = self
  yield
  Computation.current = previous
  self
end

#stopObject

Stop re-run of the computations



69
70
71
72
73
74
# File 'lib/volt/reactive/computation.rb', line 69

def stop
  unless @stopped
    @stopped = true
    invalidate!
  end
end