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
# 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.
  complete true, [], nil if !@task && @dependencies.empty? && dependencies.any?
end

Class Method Details

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

Yields:



65
66
67
68
69
# File 'lib/restify/promise.rb', line 65

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

.fulfilled(value) ⇒ Object



71
72
73
74
75
# File 'lib/restify/promise.rb', line 71

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

.rejected(value) ⇒ Object



77
78
79
80
81
# File 'lib/restify/promise.rb', line 77

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

Instance Method Details

#execute(timeout = nil) ⇒ Object



32
33
34
# File 'lib/restify/promise.rb', line 32

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

#then(&block) ⇒ Object



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

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

#wait(timeout = nil) ⇒ Object



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

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

  execute(t) if pending?

  super
  raise t if incomplete?

  self
end