Class: Restify::Promise

Inherits:
Concurrent::IVar
  • Object
show all
Defined in:
lib/restify/promise.rb

Defined Under Namespace

Classes: Writer

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*dependencies, &task) ⇒ Promise

Returns a new instance of Promise.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/restify/promise.rb', line 5

def initialize(*dependencies, &task)
  @task         = task
  @dependencies = dependencies.flatten

  super(&nil)

  # When dependencies were passed in, but none are left after flattening,
  # then we don't have to wait for explicit dependencies or resolution
  # through a writer.
  if !@task && @dependencies.empty? && dependencies.any?
    complete true, [], nil
  end
end

Class Method Details

.create {|Writer.new(promise)| ... } ⇒ Object

Yields:



67
68
69
70
71
# File 'lib/restify/promise.rb', line 67

def create
  promise = Promise.new
  yield Writer.new(promise)
  promise
end

.fulfilled(value) ⇒ Object



73
74
75
76
77
# File 'lib/restify/promise.rb', line 73

def fulfilled(value)
  create do |writer|
    writer.fulfill value
  end
end

.rejected(value) ⇒ Object



79
80
81
82
83
# File 'lib/restify/promise.rb', line 79

def rejected(value)
  create do |writer|
    writer.reject value
  end
end

Instance Method Details

#execute(timeout = nil) ⇒ Object



34
35
36
# File 'lib/restify/promise.rb', line 34

def execute(timeout = nil)
  synchronize { ns_execute timeout }
end

#then(&block) ⇒ Object



30
31
32
# File 'lib/restify/promise.rb', line 30

def then(&block)
  Promise.new([self], &block)
end

#wait(timeout = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/restify/promise.rb', line 19

def wait(timeout = nil)
  t = Timeout.new(timeout, self)

  execute(t) if pending?

  super
  raise t if incomplete?

  self
end