Class: ZeevexConcurrency::Promise

Inherits:
Delayed
  • Object
show all
Includes:
Observable, Delayed::Bindable, Delayed::LatchBased
Defined in:
lib/zeevex_concurrency/promise.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Delayed::LatchBased

#ready?, #wait

Methods included from Delayed::Bindable

#bind, #binding, #bound?, #call, #execute

Methods inherited from Delayed

#exception, #exception?, #executed?, #set_result, #value, #wait

Constructor Details

#initialize(computation = nil, options = {}, &block) ⇒ Promise

Returns a new instance of Promise.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/zeevex_concurrency/promise.rb', line 10

def initialize(computation = nil, options = {}, &block)
  @mutex       = Mutex.new
  @exec_mutex  = Mutex.new
  @exception   = nil
  @done        = false
  @result      = false
  @executed    = false

  _initialize_latch

  # has to happen after exec_mutex initialized
  bind(computation, &block) if (computation || block)

  Array(options.delete(:observer) || options.delete(:observers)).each do |observer|
    self.add_observer observer
  end
end

Class Method Details

.create(callable = nil, options = {}, &block) ⇒ Object



28
29
30
31
# File 'lib/zeevex_concurrency/promise.rb', line 28

def self.create(callable = nil, options = {}, &block)
  return callable if callable && callable.is_a?(ZeevexConcurrency::Delayed)
  new(callable, options, &block)
end