Class: PromiseV2

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ PromiseV2

Returns a new instance of PromiseV2.

Yields:

  • (_self)

Yield Parameters:

  • _self (PromiseV2)

    the object that the method was called on



361
362
363
# File 'opal/stdlib/promise/v2.rb', line 361

def initialize(&block)
  yield self if block_given?
end

Instance Attribute Details

#nextObject (readonly)

Returns the value of attribute next.



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

def next
  @next
end

#prevObject (readonly)

Returns the value of attribute prev.



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

def prev
  @prev
end

Class Method Details

.all_resolved(*promises) ⇒ Object



129
130
131
132
133
134
# File 'opal/stdlib/promise/v2.rb', line 129

def all_resolved(*promises)
  promises = Array(promises.length == 1 ? promises.first : promises)
  `Promise.allResolved(#{promises})`.tap do |prom|
    prom.instance_variable_set(:@type, :all_resolved)
  end
end

.allocateObject



110
111
112
113
114
115
116
117
118
# File 'opal/stdlib/promise/v2.rb', line 110

def allocate
  ok, fail = nil, nil

  prom = `new self.$$constructor(function(_ok, _fail) { #{ok} = _ok; #{fail} = _fail; })`
  prom.instance_variable_set(:@type, :opal)
  prom.instance_variable_set(:@resolve_proc, ok)
  prom.instance_variable_set(:@reject_proc, fail)
  prom
end

.any(*promises) ⇒ Object



136
137
138
139
140
141
# File 'opal/stdlib/promise/v2.rb', line 136

def any(*promises)
  promises = Array(promises.length == 1 ? promises.first : promises)
  `Promise.any(#{promises})`.tap do |prom|
    prom.instance_variable_set(:@type, :any)
  end
end

.errorObject



166
167
168
169
170
171
172
# File 'opal/stdlib/promise/v2.rb', line 166

def reject(value = nil)
  `Promise.reject(#{value})`.tap do |prom|
    prom.instance_variable_set(:@type, :reject)
    prom.instance_variable_set(:@realized, :reject)
    prom.instance_variable_set(:@value, value)
  end
end

.race(*promises) ⇒ Object



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

def race(*promises)
  promises = Array(promises.length == 1 ? promises.first : promises)
  `Promise.race(#{promises})`.tap do |prom|
    prom.instance_variable_set(:@type, :race)
  end
end

.reject(value = nil) ⇒ Object



159
160
161
162
163
164
165
# File 'opal/stdlib/promise/v2.rb', line 159

def reject(value = nil)
  `Promise.reject(#{value})`.tap do |prom|
    prom.instance_variable_set(:@type, :reject)
    prom.instance_variable_set(:@realized, :reject)
    prom.instance_variable_set(:@value, value)
  end
end

.resolve(value = nil) ⇒ Object



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

def resolve(value = nil)
  `Promise.resolve(#{value})`.tap do |prom|
    prom.instance_variable_set(:@type, :resolve)
    prom.instance_variable_set(:@realized, :resolve)
    prom.instance_variable_set(:@value, value)
  end
end

.valueObject



157
158
159
160
161
162
163
# File 'opal/stdlib/promise/v2.rb', line 157

def resolve(value = nil)
  `Promise.resolve(#{value})`.tap do |prom|
    prom.instance_variable_set(:@type, :resolve)
    prom.instance_variable_set(:@realized, :resolve)
    prom.instance_variable_set(:@value, value)
  end
end

.when(*promises) ⇒ Object Also known as: all



120
121
122
123
124
125
# File 'opal/stdlib/promise/v2.rb', line 120

def when(*promises)
  promises = Array(promises.length == 1 ? promises.first : promises)
  `Promise.all(#{promises})`.tap do |prom|
    prom.instance_variable_set(:@type, :when)
  end
end

Instance Method Details

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



268
269
270
271
272
273
274
275
276
277
278
279
# File 'opal/stdlib/promise/v2.rb', line 268

def always(&block)
  prom = nil
  blk = gen_tracing_proc(block) do |val|
    prom.instance_variable_set(:@realized, :resolve)
    prom.instance_variable_set(:@value, val)
  end
  prom = `self.finally(#{blk})`
  prom.instance_variable_set(:@prev, self)
  prom.instance_variable_set(:@type, :always)
  (@next ||= []) << prom
  prom
end

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



281
282
283
284
# File 'opal/stdlib/promise/v2.rb', line 281

def always!(&block)
  there_can_be_only_one!
  always(&block)
end

#and(*promises) ⇒ Object



348
349
350
351
352
353
354
355
356
357
358
359
# File 'opal/stdlib/promise/v2.rb', line 348

def and(*promises)
  promises = promises.map do |i|
    if PromiseV2 === i
      i
    else
      PromiseV2.value(i)
    end
  end
  PromiseV2.when(self, *promises).then do |a, *b|
    [*a, *b]
  end
end

#errorObject



343
344
345
346
# File 'opal/stdlib/promise/v2.rb', line 343

def error
  light_nativity_check!
  @value if rejected?
end

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



245
246
247
248
249
250
251
252
253
254
255
256
# File 'opal/stdlib/promise/v2.rb', line 245

def fail(&block)
  prom = nil
  blk = gen_tracing_proc(block) do |val|
    prom.instance_variable_set(:@realized, :resolve)
    prom.instance_variable_set(:@value, val)
  end
  prom = `self.catch(#{blk})`
  prom.instance_variable_set(:@prev, self)
  prom.instance_variable_set(:@type, :fail)
  (@next ||= []) << prom
  prom
end

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



258
259
260
261
# File 'opal/stdlib/promise/v2.rb', line 258

def fail!(&block)
  there_can_be_only_one!
  fail(&block)
end

#gen_tracing_proc(passing, &block) ⇒ Object



196
197
198
199
200
201
202
# File 'opal/stdlib/promise/v2.rb', line 196

def gen_tracing_proc(passing, &block)
  proc do |i|
    res = passing.call(i)
    yield(res)
    res
  end
end

#inspectObject



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'opal/stdlib/promise/v2.rb', line 377

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

  if @type
    result += ":#{@type}" unless %i[opal resolve reject].include? @type
  else
    result += ':native'
  end

  result += ":#{@realized}" if @realized
  result += "(#{object_id})"

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

  result += ": #{@value.inspect}" if @value
  result += '>'

  result
end

#light_nativity_check!Object

Raise an exception when a non-JS-native method is called on a JS-native promise but permits some typed promises

Raises:

  • (ArgumentError)


184
185
186
187
# File 'opal/stdlib/promise/v2.rb', line 184

def light_nativity_check!
  return if %i[reject resolve trace always fail then].include? @type
  raise ArgumentError, 'this promise is native to JavaScript' if native?
end

#native?Boolean

Is this promise native to JavaScript? This means, that methods like resolve or reject won't be available.

Returns:



173
174
175
# File 'opal/stdlib/promise/v2.rb', line 173

def native?
  @type != :opal
end

#nativity_check!Object

Raise an exception when a non-JS-native method is called on a JS-native promise

Raises:

  • (ArgumentError)


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

def nativity_check!
  raise ArgumentError, 'this promise is native to JavaScript' if native?
end

#realized?Boolean

Returns:



328
329
330
331
# File 'opal/stdlib/promise/v2.rb', line 328

def realized?
  light_nativity_check!
  !@realized.nil?
end

#reject(value = nil) ⇒ Object Also known as: reject!

Raises:

  • (ArgumentError)


214
215
216
217
218
219
220
221
# File 'opal/stdlib/promise/v2.rb', line 214

def reject(value = nil)
  nativity_check!
  raise ArgumentError, 'this promise was already resolved' if @realized
  @value = value
  @realized = :reject
  @reject_proc.call(value)
  self
end

#rejected?Boolean

Returns:



323
324
325
326
# File 'opal/stdlib/promise/v2.rb', line 323

def rejected?
  light_nativity_check!
  @realized == :reject
end

#resolve(value = nil) ⇒ Object Also known as: resolve!

Raises:

  • (ArgumentError)


204
205
206
207
208
209
210
211
# File 'opal/stdlib/promise/v2.rb', line 204

def resolve(value = nil)
  nativity_check!
  raise ArgumentError, 'this promise was already resolved' if @realized
  @value = value
  @realized = :resolve
  @resolve_proc.call(value)
  self
end

#resolved?Boolean

Returns:



318
319
320
321
# File 'opal/stdlib/promise/v2.rb', line 318

def resolved?
  light_nativity_check!
  @realized == :resolve
end

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



224
225
226
227
228
229
230
231
232
233
234
235
# File 'opal/stdlib/promise/v2.rb', line 224

def then(&block)
  prom = nil
  blk = gen_tracing_proc(block) do |val|
    prom.instance_variable_set(:@realized, :resolve)
    prom.instance_variable_set(:@value, val)
  end
  prom = `self.then(#{blk})`
  prom.instance_variable_set(:@prev, self)
  prom.instance_variable_set(:@type, :then)
  (@next ||= []) << prom
  prom
end

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



237
238
239
240
# File 'opal/stdlib/promise/v2.rb', line 237

def then!(&block)
  there_can_be_only_one!
  self.then(&block)
end

#there_can_be_only_one!Object

Allow only one chain to be present, as needed by the previous implementation. This isn't a strict check - it's always possible on the JS side to chain a given block.

Raises:

  • (ArgumentError)


192
193
194
# File 'opal/stdlib/promise/v2.rb', line 192

def there_can_be_only_one!
  raise ArgumentError, 'a promise has already been chained' if @next && @next.any?
end

#to_v1Object



367
368
369
370
371
372
373
# File 'opal/stdlib/promise/v2.rb', line 367

def to_v1
  v1 = PromiseV1.new

  self.then { |i| v1.resolve(i) }.rescue { |i| v1.reject(i) }

  v1
end

#trace(depth = nil, &block) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'opal/stdlib/promise/v2.rb', line 291

def trace(depth = nil, &block)
  prom = self.then do
    values = []
    prom = self
    while prom && (!depth || depth > 0)
      val = nil
      begin
        val = prom.value
      rescue ArgumentError
        val = :native
      end
      values.unshift(val)
      depth -= 1 if depth
      prom = prom.prev
    end
    yield(*values)
  end

  prom.instance_variable_set(:@type, :trace)
  prom
end

#trace!(*args, &block) ⇒ Object



313
314
315
316
# File 'opal/stdlib/promise/v2.rb', line 313

def trace!(*args, &block)
  there_can_be_only_one!
  trace(*args, &block)
end

#valueObject



333
334
335
336
337
338
339
340
341
# File 'opal/stdlib/promise/v2.rb', line 333

def value
  if resolved?
    if PromiseV2 === @value
      @value.value
    else
      @value
    end
  end
end