Class: MrDarcy::Promise::EM

Inherits:
Base
  • Object
show all
Defined in:
lib/mr_darcy/promise/em.rb

Overview

EventMachine implementation of promises.

See MrDarcy::Promise::Base for method documentation.

Defined Under Namespace

Classes: DeferrableAdapter

Instance Method Summary collapse

Methods inherited from Base

#fail, #raise, #then

Constructor Details

#initialize(*args) ⇒ EM

Returns a new instance of EM



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mr_darcy/promise/em.rb', line 13

def initialize *args
  Kernel::raise "EventMachine driver is unsupported on JRuby, sorry" if RUBY_ENGINE=='jruby'
  unless EventMachine.reactor_running?
    ::Thread.new { EventMachine.run }
    ::Thread.pass until EventMachine.reactor_running?
  end
  @wait_lock = Mutex.new
  @wait_cond = ConditionVariable.new
  @wait_lock.lock
  deferrable_adapter.callback &method(:do_resolve)
  deferrable_adapter.errback &method(:do_reject)
  super
end

Instance Method Details

#finalObject



42
43
44
45
# File 'lib/mr_darcy/promise/em.rb', line 42

def final
  wait_if_unresolved
  self
end

#reject(value) ⇒ Object



32
33
34
35
# File 'lib/mr_darcy/promise/em.rb', line 32

def reject value
  deferrable_adapter.set_deferred_status :failed, value
  self
end

#resolve(value) ⇒ Object



27
28
29
30
# File 'lib/mr_darcy/promise/em.rb', line 27

def resolve value
  deferrable_adapter.set_deferred_status :succeeded, value
  self
end

#resultObject



37
38
39
40
# File 'lib/mr_darcy/promise/em.rb', line 37

def result
  wait_if_unresolved
  value
end