class Visionary::Future
Attributes
setted_up[RW]
block[R]
error[R]
state[RW]
thread[R]
value[R]
waiting_for[RW]
Public Class Methods
new(&blk)
click to toggle source
# File lib/visionary/future.rb, line 21 def initialize(&blk) @block = blk self.state = :pending end
setup!()
click to toggle source
# File lib/visionary/future.rb, line 5 def setup! unless setted_up Kernel.send :define_method, :future do |&blk| Future.new(&blk).run end self.setted_up = true end end
Public Instance Methods
await()
click to toggle source
# File lib/visionary/future.rb, line 48 def await waiting_for && waiting_for.await unless thread run end thread.join end
run()
click to toggle source
# File lib/visionary/future.rb, line 26 def run raise RuntimeError, "This future have been already started" if thread @thread = Thread.new { run! } self end
then(&blk)
click to toggle source
# File lib/visionary/future.rb, line 32 def then(&blk) fut = Future.new { blk.call(value) } fut.waiting_for = self case state when :pending callbacks << fut when :completed fut.run when :failed fut.fail_with(error) end fut end
Protected Instance Methods
complete_with(value)
click to toggle source
# File lib/visionary/future.rb, line 62 def complete_with(value) @value = value @state = :completed callbacks.each { |callback| callback.run } ensure freeze end
fail_with(error)
click to toggle source
# File lib/visionary/future.rb, line 70 def fail_with(error) @error = error @state = :failed callbacks.each { |callback| callback.fail_with(error) } ensure freeze end
Private Instance Methods
callbacks()
click to toggle source
# File lib/visionary/future.rb, line 89 def callbacks @callbacks ||= [] end
run!()
click to toggle source
# File lib/visionary/future.rb, line 83 def run! complete_with(block.call) rescue => e fail_with(e) end