Class: Promise

Inherits:
Object show all
Defined in:
opal/stdlib/promise.rb

Direct Known Subclasses

Trace, When

Defined Under Namespace

Classes: Trace, When

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(success = nil, failure = nil) ⇒ Promise

Returns a new instance of Promise



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'opal/stdlib/promise.rb', line 16

def initialize(success = nil, failure = nil)
  @success = success
  @failure = failure

  @realized  = nil
  @exception = false
  @value     = nil
  @error     = nil
  @delayed   = nil

  @prev = nil
  @next = nil
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error



14
15
16
# File 'opal/stdlib/promise.rb', line 14

def error
  @error
end

#nextObject (readonly)

Returns the value of attribute next



14
15
16
# File 'opal/stdlib/promise.rb', line 14

def next
  @next
end

#prevObject (readonly)

Returns the value of attribute prev



14
15
16
# File 'opal/stdlib/promise.rb', line 14

def prev
  @prev
end

#valueObject (readonly)

Returns the value of attribute value



14
15
16
# File 'opal/stdlib/promise.rb', line 14

def value
  @value
end

Class Method Details

.error(value) ⇒ Object



6
7
8
# File 'opal/stdlib/promise.rb', line 6

def self.error(value)
  new.reject(value)
end

.value(value) ⇒ Object



2
3
4
# File 'opal/stdlib/promise.rb', line 2

def self.value(value)
  new.resolve(value)
end

.when(*promises) ⇒ Object



10
11
12
# File 'opal/stdlib/promise.rb', line 10

def self.when(*promises)
  When.new(promises)
end

Instance Method Details

#<<(promise) ⇒ Object



57
58
59
60
61
# File 'opal/stdlib/promise.rb', line 57

def <<(promise)
  @prev = promise

  self
end

#>>(promise) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'opal/stdlib/promise.rb', line 63

def >>(promise)
  @next = promise

  if exception?
    promise.reject(@delayed)
  elsif resolved?
    promise.resolve(@delayed || value)
  elsif rejected? && (!@failure || Promise === (@delayed || @error))
    promise.reject(@delayed || error)
  end

  self
end

#^(promise) ⇒ Object



50
51
52
53
54
55
# File 'opal/stdlib/promise.rb', line 50

def ^(promise)
  promise << self
  self    >> promise

  promise
end

#act?Boolean

Returns:



30
31
32
# File 'opal/stdlib/promise.rb', line 30

def act?
  @success != nil
end

#always(&block) ⇒ Object Also known as: finally, ensure



170
171
172
# File 'opal/stdlib/promise.rb', line 170

def always(&block)
  self ^ Promise.new(block, block)
end

#exception!(error) ⇒ Object



151
152
153
154
155
# File 'opal/stdlib/promise.rb', line 151

def exception!(error)
  @exception = true

  reject!(error)
end

#exception?Boolean

Returns:



34
35
36
# File 'opal/stdlib/promise.rb', line 34

def exception?
  @exception
end

#fail(&block) ⇒ Object Also known as: rescue, catch



163
164
165
# File 'opal/stdlib/promise.rb', line 163

def fail(&block)
  self ^ Promise.new(nil, block)
end

#inspectObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'opal/stdlib/promise.rb', line 181

def inspect
  result = "#<#{self.class}(#{object_id})"

  if @next
    result += " >> #{@next.inspect}"
  end

  if realized?
    result += ": #{(@value || @error).inspect}>"
  else
    result += ">"
  end

  result
end

#realized?Boolean

Returns:



38
39
40
# File 'opal/stdlib/promise.rb', line 38

def realized?
  @realized != nil
end

#reject(value = nil) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'opal/stdlib/promise.rb', line 112

def reject(value = nil)
  if realized?
    raise ArgumentError, 'the promise has already been realized'
  end

  if Promise === value
    value << @prev

    return value ^ self
  end

  @realized = :reject
  @error    = value

  begin
    if @failure
      value = @failure.call(value)

      if Promise === value
        reject!(value)
      end
    else
      reject!(value)
    end
  rescue Exception => e
    exception!(e)
  end

  self
end

#reject!(value) ⇒ Object



143
144
145
146
147
148
149
# File 'opal/stdlib/promise.rb', line 143

def reject!(value)
  if @next
    @next.reject(value)
  else
    @delayed = value
  end
end

#rejected?Boolean

Returns:



46
47
48
# File 'opal/stdlib/promise.rb', line 46

def rejected?
  @realized == :reject
end

#resolve(value = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'opal/stdlib/promise.rb', line 77

def resolve(value = nil)
  if realized?
    raise ArgumentError, 'the promise has already been realized'
  end

  if Promise === value
    value << @prev

    return value ^ self
  end

  @realized = :resolve
  @value    = value

  begin
    if @success
      value = @success.call(value)
    end

    resolve!(value)
  rescue Exception => e
    exception!(e)
  end

  self
end

#resolve!(value) ⇒ Object



104
105
106
107
108
109
110
# File 'opal/stdlib/promise.rb', line 104

def resolve!(value)
  if @next
    @next.resolve(value)
  else
    @delayed = value
  end
end

#resolved?Boolean

Returns:



42
43
44
# File 'opal/stdlib/promise.rb', line 42

def resolved?
  @realized == :resolve
end

#then(&block) ⇒ Object Also known as: do



157
158
159
# File 'opal/stdlib/promise.rb', line 157

def then(&block)
  self ^ Promise.new(block)
end

#trace(&block) ⇒ Object



177
178
179
# File 'opal/stdlib/promise.rb', line 177

def trace(&block)
  self ^ Trace.new(block)
end