Class: Autodeps::Computation
- Inherits:
-
Object
- Object
- Autodeps::Computation
- Defined in:
- lib/autodeps/computation.rb
Instance Attribute Summary collapse
-
#_on_invalidate_callbacks ⇒ Object
Returns the value of attribute _on_invalidate_callbacks.
-
#block ⇒ Object
Returns the value of attribute block.
-
#first_run ⇒ Object
Returns the value of attribute first_run.
-
#invalidated ⇒ Object
Returns the value of attribute invalidated.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#recomputing ⇒ Object
Returns the value of attribute recomputing.
-
#stopped ⇒ Object
Returns the value of attribute stopped.
Instance Method Summary collapse
- #compute ⇒ Object
-
#initialize(block, parent = nil) ⇒ Computation
constructor
A new instance of Computation.
- #invalidate ⇒ Object
- #on_invalidate(f = nil, &block) ⇒ Object
- #recompute ⇒ Object
- #require_flush ⇒ Object
- #stop ⇒ Object
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_callbacks ⇒ Object
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 |
#block ⇒ Object
Returns the value of attribute block.
3 4 5 |
# File 'lib/autodeps/computation.rb', line 3 def block @block end |
#first_run ⇒ Object
Returns the value of attribute first_run.
3 4 5 |
# File 'lib/autodeps/computation.rb', line 3 def first_run @first_run end |
#invalidated ⇒ Object
Returns the value of attribute invalidated.
3 4 5 |
# File 'lib/autodeps/computation.rb', line 3 def invalidated @invalidated end |
#parent ⇒ Object
Returns the value of attribute parent.
3 4 5 |
# File 'lib/autodeps/computation.rb', line 3 def parent @parent end |
#recomputing ⇒ Object
Returns the value of attribute recomputing.
3 4 5 |
# File 'lib/autodeps/computation.rb', line 3 def recomputing @recomputing end |
#stopped ⇒ Object
Returns the value of attribute stopped.
3 4 5 |
# File 'lib/autodeps/computation.rb', line 3 def stopped @stopped end |
Instance Method Details
#compute ⇒ Object
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 |
#invalidate ⇒ Object
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 |
#recompute ⇒ Object
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.) if Autodeps.logger Autodeps.logger.error(e.backtrace.join("\n")) if Autodeps.logger end end self.recomputing = false; end |
#require_flush ⇒ Object
66 67 68 |
# File 'lib/autodeps/computation.rb', line 66 def require_flush Autodeps::flush end |
#stop ⇒ Object
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 |