Module: Autodeps

Defined in:
lib/autodeps.rb,
lib/autodeps/version.rb,
lib/autodeps/autodeps.rb,
lib/autodeps/dependency.rb,
lib/autodeps/computation.rb,
lib/autodeps/persistency.rb,
lib/autodeps/reactive_data.rb

Defined Under Namespace

Modules: Persistency Classes: Computation, Dependency, ReactiveData

Constant Summary collapse

VERSION =
"0.0.2"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.activeObject

Returns the value of attribute active.



13
14
15
# File 'lib/autodeps/autodeps.rb', line 13

def active
  @active
end

.loggerObject

Returns the value of attribute logger.



4
5
6
# File 'lib/autodeps/autodeps.rb', line 4

def logger
  @logger
end

Class Method Details

.add_pending_computation(pending_computation) ⇒ Object



14
15
16
# File 'lib/autodeps/autodeps.rb', line 14

def add_pending_computation(pending_computation)
  @pending_computations << pending_computation
end

.afterFlush(&f) ⇒ Object



92
93
94
# File 'lib/autodeps/autodeps.rb', line 92

def afterFlush(&f)
  @after_flush_callbacks.push(f);
end

.autorun(&block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/autodeps/autodeps.rb', line 18

def autorun(&block)
  raise 'Autodeps.autorun requires a block' if block.nil?

  @constructingComputation = true
  c = Computation.new(block, Autodeps.current_computation);


  if (Autodeps.active)
    Autodeps.on_invalidate do
      c.stop();
    end
  end

  return c
end

.current_computationObject



199
200
201
# File 'lib/autodeps/autodeps.rb', line 199

def current_computation
  Thread.current["Autodeps::current_computation"]
end

.current_computation=(computation) ⇒ Object



202
203
204
205
# File 'lib/autodeps/autodeps.rb', line 202

def current_computation= (computation)
  Thread.current["Autodeps::current_computation"] = computation
  self.active = !! computation;
end

.embox(equals = nil, &func) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/autodeps/autodeps.rb', line 118

def embox(equals=nil, &func)
  raise "must define a block in embox" unless func


  curResult = nil;
  #There's one shared Dependency and Computation for all callers of

  # our box function.  It gets kicked off if necessary, and when
  # there are no more dependents, it gets stopped to avoid leaking
  # memory.
  resultDep = nil;
  computation = nil;

  return proc do
    if (!computation)
      if (!Autodeps.active)
        # Not in a reactive context.  Just call func, and don't start a
        # computation if there isn't one running already.
        break func.call();
      end

      # No running computation, so kick one off.  Since this computation
      # will be shared, avoid any association with the current computation
      # by using `Deps.nonreactive`.
      resultDep = Autodeps::Dependency.new;

      computation = Autodeps.nonreactive do
        break Autodeps.autorun do |c|
          oldResult = curResult;
          curResult = func.call();
          if (!c.first_run)
            if (!(equals ? equals.call(curResult, oldResult) :
                curResult == oldResult))
              resultDep.changed();
            end
          end
        end
      end
    end

    if (Autodeps.active)
      is_new = resultDep.depend();
      if (is_new)
        # For each new dependent, schedule a task for after that dependents
        # invalidation time and the subsequent flush. The task checks
        # whether the computation should be torn down.
        Autodeps.on_invalidate do
          if (resultDep && !resultDep.hasDependents())
            Autodeps.afterFlush do
              # use a second afterFlush to bump ourselves to the END of the
              # flush, after computation re-runs have had a chance to
              # re-establish their connections to our computation.
              Autodeps.afterFlush do
                if (resultDep && !resultDep.hasDependents())
                  computation.stop();
                  computation = nil;
                  resultDep = nil;
                end
              end
            end
          end
        end
      end
    end

    curResult
  end
end

.embox_value(value, equals = nil) ⇒ Object



187
188
189
# File 'lib/autodeps/autodeps.rb', line 187

def embox_value(value, equals=nil)
   raise "embox_value on a direct value not implemented"
end

.flushObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/autodeps/autodeps.rb', line 44

def flush

  #if (inFlush)
  #  throw new Error("Can't call Deps.flush while flushing");
  #
  #  if (inCompute)
  #    throw new Error("Can't flush inside Deps.autorun");

  @inFlush = true
  @willFlush = true


  while (@pending_computations.length > 0 ||
      @after_flush_callbacks.length > 0) do

      #recompute all pending computations
    comps = @pending_computations;
    @pending_computations = [];

    comps.each do |comp|
      comp.recompute();

    #if (afterFlushCallbacks.length) {
    #    // call one afterFlush callback, which may
    #// invalidate more computations
    #var func = afterFlushCallbacks.shift();
    #try {
    #  func();
    #} catch (e) {
    #    _debugFunc()("Exception from Deps afterFlush function:",
    #    e.stack || e.message);
    #}
    end
  end

  inFlush = false;
  willFlush = false;

end

.isolateValue(equals = nil, &f) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/autodeps/autodeps.rb', line 96

def isolateValue(equals=nil, &f)
  raise "must define a block in isolateValue" unless f
  if (!Autodeps.active)
    return f.call();
  end

  result_dep = Autodeps::Dependency.new;
  orig_result = nil
  Autodeps.autorun do |c|
    result = f.call();
    if (c.first_run)
      orig_result = result;
    elsif (!(equals ? equals(result, orig_result) :
        result == orig_result))
      result_dep.changed();
    end
  end
  result_dep.depend();

  return orig_result;
end

.nonreactive(&f) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/autodeps/autodeps.rb', line 34

def nonreactive(&f)
  previous = self.current_computation;
  self.current_computation = nil;
  begin
    f.call()
  ensure
    self.current_computation = previous;
  end
end

.on_invalidate(&f) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/autodeps/autodeps.rb', line 84

def on_invalidate(&f)
  if (! Autodeps.active)
    raise "AutoDeps.on_invalidate requires a currentComputation"
  end

    Autodeps.current_computation.on_invalidate(f);
end