Class: Autodeps::Computation

Inherits:
Object
  • Object
show all
Defined in:
lib/autodeps/computation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block, parent = nil) ⇒ Computation

Returns a new instance of Computation.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/autodeps/computation.rb', line 4

def initialize(block, parent=nil)
  self.stopped = false;
      self.invalidated = false;
      self.first_run = true;

  self.parent = parent;
  self.block = block;
  self.recomputing = false;
  self._on_invalidate_callbacks = ThreadSafe::Array.new

  errored = true;
  begin
    self.compute();
    errored = false;
  rescue => e
      raise e
  ensure
    self.first_run = false;
    self.stop() if errored
  end
end

Instance Attribute Details

#_on_invalidate_callbacksObject

Returns the value of attribute _on_invalidate_callbacks.



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

def _on_invalidate_callbacks
  @_on_invalidate_callbacks
end

#blockObject

Returns the value of attribute block.



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

def block
  @block
end

#first_runObject

Returns the value of attribute first_run.



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

def first_run
  @first_run
end

#invalidatedObject

Returns the value of attribute invalidated.



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

def invalidated
  @invalidated
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

#recomputingObject

Returns the value of attribute recomputing.



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

def recomputing
  @recomputing
end

#stoppedObject

Returns the value of attribute stopped.



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

def stopped
  @stopped
end

Instance Method Details

#computeObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/autodeps/computation.rb', line 26

def compute
  self.invalidated = false;

  previous = Autodeps.current_computation;
  Autodeps.current_computation = self;

  in_compute = true;
  begin
    block.call(self)
  #rescue => e

  #  Autodeps.logger.error(e.message) if Autodeps.logger

  #  Autodeps.logger.error(e.backtrace.join("\n")) if Autodeps.logger

  ensure
    Autodeps.current_computation = previous;
    in_compute = false;
  end
end

#invalidateObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/autodeps/computation.rb', line 70

def invalidate
  ##we request an immediate flush because we don't have timeout




  if (! self.invalidated)
    # if we're currently in _recompute(), don't enqueue

    # ourselves, since we'll rerun immediately anyway.


    self.invalidated = true;
    self._on_invalidate_callbacks.each do |f|
      f.call(); #// already bound with self as argument

    end
    self._on_invalidate_callbacks = ThreadSafe::Array.new;

    if (! self.recomputing && ! self.stopped)
      self.invalidated = true;

      Autodeps.add_pending_computation(self);
      require_flush();
    end

  # callbacks can't add callbacks, because

  #self.invalidated === true.



  end
end

#on_invalidate(f = nil, &block) ⇒ Object



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

def on_invalidate(f=nil, &block)
    if block_given?
      f = block
    end
    raise ("on_invalidate requires a block") unless f;

    g = proc do
      Autodeps.nonreactive do
        f.call(self);
      end
    end

    if (self.invalidated)
      g();
    else
      self._on_invalidate_callbacks.push(g);
    end
end

#recomputeObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/autodeps/computation.rb', line 44

def recompute
  self.recomputing = true

  while (self.invalidated && !self.stopped)
    begin
      self.compute()
    rescue => e
      Autodeps.logger.error(e.message) if Autodeps.logger
      Autodeps.logger.error(e.backtrace.join("\n")) if Autodeps.logger
    end
  end

  self.recomputing = false;
end

#require_flushObject



66
67
68
# File 'lib/autodeps/computation.rb', line 66

def require_flush
  Autodeps::flush
end

#stopObject



59
60
61
62
63
64
# File 'lib/autodeps/computation.rb', line 59

def stop
  if (! self.stopped)
        self.stopped = true;
        self.invalidate();
  end
end