Class: IOPromise::Base

Inherits:
Promise
  • Object
show all
Defined in:
lib/iopromise.rb

Direct Known Subclasses

Deferred::DeferredPromise

Instance Method Summary collapse

Instance Method Details

#beginningObject



30
31
32
33
34
# File 'lib/iopromise.rb', line 30

def beginning
  @instrument_begin&.each { |cb| cb.call(self) }
  @instrument_begin&.clear
  @started_executing = true
end

#cancelObject

makes this promise inert, ensuring that promise chains do not continue propegation once this promise has been cancelled.



70
71
72
73
74
75
76
77
# File 'lib/iopromise.rb', line 70

def cancel
  return unless pending?
  
  @cancelled = true
  @observers = []

  execute_pool.promise_cancelled(self)
end

#cancelled?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/iopromise.rb', line 79

def cancelled?
  !!defined?(@cancelled)
end

#execute_poolObject

Subclasses are expected to implement ‘execute_pool’ to return an IOPromise::ExecutorPool that is responsible for completing the given promise.

Raises:

  • (NotImplementedError)


64
65
66
# File 'lib/iopromise.rb', line 64

def execute_pool
  raise NotImplementedError
end

#fulfill(value) ⇒ Object



45
46
47
48
49
# File 'lib/iopromise.rb', line 45

def fulfill(value)
  return if cancelled?
  notify_completion(value: value)
  super(value)
end

#instrument(begin_cb = nil, end_cb = nil) ⇒ Object

Raises:



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

def instrument(begin_cb = nil, end_cb = nil)
  raise ::IOPromise::Error.new("Instrumentation called after promise already started executing") if started_executing?
  unless begin_cb.nil?
    @instrument_begin ||= []
    @instrument_begin << begin_cb
  end
  unless end_cb.nil?
    @instrument_end ||= []
    @instrument_end << end_cb
  end
end

#notify_completion(value: nil, reason: nil) ⇒ Object



40
41
42
43
# File 'lib/iopromise.rb', line 40

def notify_completion(value: nil, reason: nil)
  @instrument_end&.each { |cb| cb.call(self, value: value, reason: reason) }
  @instrument_end&.clear
end

#reject(reason) ⇒ Object



51
52
53
54
55
# File 'lib/iopromise.rb', line 51

def reject(reason)
  return if cancelled?
  notify_completion(reason: reason)
  super(reason)
end

#started_executing?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/iopromise.rb', line 36

def started_executing?
  !!@started_executing
end

#waitObject



57
58
59
60
# File 'lib/iopromise.rb', line 57

def wait
  raise IOPromise::CancelledError if cancelled?
  super
end