Class: Promise::Trace

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

Instance Attribute Summary

Attributes inherited from Promise

#error, #next, #prev, #value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Promise

#<<, #>>, #^, #act?, #always, error, #exception!, #exception?, #fail, #inspect, #realized?, #reject, #reject!, #rejected?, #resolve, #resolve!, #resolved?, #then, #trace, value, when

Constructor Details

#initialize(depth, block) ⇒ Trace

Returns a new instance of Trace



327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'opal/stdlib/promise.rb', line 327

def initialize(depth, block)
  @depth = depth

  super -> {
    trace = Trace.it(self).reverse

    if depth && depth <= trace.length
      trace.shift(trace.length - depth)
    end

    block.call(*trace)
  }
end

Class Method Details

.it(promise) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'opal/stdlib/promise.rb', line 313

def self.it(promise)
  unless promise.realized?
    raise ArgumentError, "the promise hasn't been realized"
  end

  current = promise.act? ? [promise.value] : []

  if prev = promise.prev
    current.concat(it(prev))
  else
    current
  end
end