Sha256: a5200ca8a080881eafdbd0602c730763f820c336473573dfd33a0e60ae441856

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

# encoding: utf-8

class Reference
  Error = Class.new(RuntimeError)

  attr_reader :state, :value, :reason

  def initialize
    @state = :pending
    @on_fulfill_callbacks = []
    @on_reject_callbacks = []
    @backtrace
  end

  def pending?
    @state.equal?(:pending)
  end

  def fulfilled?
    @state.equal?(:fulfilled)
  end

  def rejected?
    @state.equal?(:rejected)
  end

  def on_fulfill(&callback)
    @on_fulfill_callbacks << callback
    if fulfilled?
      run_fulfill_callback(callback)
    end
  end

  def on_reject(&callback)
    @on_reject_callbacks << callback
    if rejected?
      run_reject_callback(callback)
    end
  end

  def fulfill(value = nil, backtrace = nil)
    return unless pending?

    @state = :fulfilled
    @value = value
    @backtrace = backtrace

    run_fulfill_callbacks
    value
  end

  def reject(reason = nil, backtrace = nil)
    return unless pending?

    @state = :rejected
    @reason = reason || Error
    @backtrace = backtrace

    run_reject_callbacks
    reason
  end

  private

  def run_fulfill_callbacks
    @on_fulfill_callbacks.each { |callback| run_fulfill_callback(callback) }
  end

  def run_reject_callbacks
    @on_reject_callbacks.each { |callback| run_reject_callback(callback) }
  end

  def run_fulfill_callback(callback)
    callback.call(@value)
  end

  def run_reject_callback(callback)
    callback.call(@reason)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reference.rb-0.1.0 lib/reference.rb