class MrDarcy::Promise::Base

An abstract superclass for all promise implementations.

Attributes

state[RW]
value[RW]

Public Class Methods

new(block) click to toggle source

Create a new promise and schedule it for execution.

# File lib/mr_darcy/promise/base.rb, line 13
def initialize block
  state
  schedule_promise do
    evaluate_promise &block
  end
  did_initialize
end

Public Instance Methods

fail(&block) click to toggle source

Create a new promise that rejects and calls the supplied block when this promise is rejected.

# File lib/mr_darcy/promise/base.rb, line 32
def fail &block
  ensure_child_promise
  child_promise.reject_block = block
  resolve_or_reject_child_as_needed
  child_promise.promise
end
final() click to toggle source

Wait until the promise is resolved or rejected and return self.

# File lib/mr_darcy/promise/base.rb, line 46
def final
  Kernel::raise "Subclasses must implement me"
end
raise() click to toggle source

Wait until the promise is resolver or rejected, and if rejected raise the error value in this context.

Calls superclass method
# File lib/mr_darcy/promise/base.rb, line 52
def raise
  r = result
  if rejected?
    if r.is_a? Exception
      super r
    else
      super RuntimeError, r
    end
  end
end
reject(exception) click to toggle source

Reject this promise with the provided error/value.

# File lib/mr_darcy/promise/base.rb, line 70
def reject exception
  do_reject exception
  self
end
resolve(value) click to toggle source

Resolve this promise with the provided value.

# File lib/mr_darcy/promise/base.rb, line 64
def resolve value
  do_resolve value
  self
end
result() click to toggle source

Wait until the promise is resolved or rejected and return it’s result value.

# File lib/mr_darcy/promise/base.rb, line 41
def result
  Kernel::raise "Subclasses must implement me"
end
then(&block) click to toggle source

Create a new promise that resolves and calls the supplied block when this promise is resolved.

# File lib/mr_darcy/promise/base.rb, line 23
def then &block
  ensure_child_promise
  child_promise.resolve_block = block
  resolve_or_reject_child_as_needed
  child_promise.promise
end

Private Instance Methods

child_promise() click to toggle source
# File lib/mr_darcy/promise/base.rb, line 158
def child_promise
  @child_promise
end
did_initialize() click to toggle source
# File lib/mr_darcy/promise/base.rb, line 99
def did_initialize; end
did_reject(value;) click to toggle source
# File lib/mr_darcy/promise/base.rb, line 98
def did_reject value; end
did_resolve(value;) click to toggle source
# File lib/mr_darcy/promise/base.rb, line 97
def did_resolve value; end
do_reject(exception) click to toggle source
# File lib/mr_darcy/promise/base.rb, line 87
def do_reject exception
  will_reject exception
  set_value_to exception
  state_machine_reject
  reject_child_promise
  did_reject exception
end
do_resolve(value) click to toggle source
# File lib/mr_darcy/promise/base.rb, line 79
def do_resolve value
  will_resolve value
  set_value_to value
  state_machine_resolve
  resolve_child_promise
  did_resolve value
end
ensure_child_promise() click to toggle source
# File lib/mr_darcy/promise/base.rb, line 154
def ensure_child_promise
  @child_promise ||= generate_child_promise
end
evaluate_promise(&block) click to toggle source
# File lib/mr_darcy/promise/base.rb, line 142
def evaluate_promise &block
  begin
    block.call DSL.new(self)
  rescue Exception => e
    reject e
  end
end
generate_child_promise() click to toggle source
# File lib/mr_darcy/promise/base.rb, line 150
def generate_child_promise
  Kernel::raise "Subclasses must implement me"
end
has_child_promise?() click to toggle source
# File lib/mr_darcy/promise/base.rb, line 121
def has_child_promise?
  !!child_promise
end
reject_child_promise() click to toggle source
# File lib/mr_darcy/promise/base.rb, line 129
def reject_child_promise
  child_promise.parent_rejected(value) if has_child_promise?
end
resolve_child_promise() click to toggle source
# File lib/mr_darcy/promise/base.rb, line 125
def resolve_child_promise
  child_promise.parent_resolved(value) if has_child_promise?
end
resolve_or_reject_child_as_needed() click to toggle source
# File lib/mr_darcy/promise/base.rb, line 133
def resolve_or_reject_child_as_needed
  resolve_child_promise if resolved?
  reject_child_promise  if rejected?
end
schedule_promise() click to toggle source
# File lib/mr_darcy/promise/base.rb, line 138
def schedule_promise
  Kernel::raise "Subclasses must implement me"
end
set_value_to(value) click to toggle source
# File lib/mr_darcy/promise/base.rb, line 105
def set_value_to value
  @value = value
end
state_machine() click to toggle source
# File lib/mr_darcy/promise/base.rb, line 117
def state_machine
  State.state(self)
end
state_machine_reject() click to toggle source
# File lib/mr_darcy/promise/base.rb, line 113
def state_machine_reject
  state_machine.reject
end
state_machine_resolve() click to toggle source
# File lib/mr_darcy/promise/base.rb, line 109
def state_machine_resolve
  state_machine.resolve
end
will_reject(value;) click to toggle source
# File lib/mr_darcy/promise/base.rb, line 96
def will_reject value; end
will_resolve(value;) click to toggle source
# File lib/mr_darcy/promise/base.rb, line 95
def will_resolve value; end