Class: EMPromise
- Inherits:
-
Promise
- Object
- Promise
- EMPromise
- Defined in:
- lib/em_promise.rb
Defined Under Namespace
Classes: Trampoline
Class Method Summary collapse
Instance Method Summary collapse
- #catch_only(*klasses, filter: ->(*) { false }) ⇒ Object
-
#class ⇒ Object
Make sure that self.class.new inherits the trampoline.
- #defer ⇒ Object
- #fulfill(value, bind_defer: true) ⇒ Object
-
#initialize(deferrable = nil, trampoline: nil) ⇒ EMPromise
constructor
A new instance of EMPromise.
- #wait ⇒ Object
Constructor Details
#initialize(deferrable = nil, trampoline: nil) ⇒ EMPromise
Returns a new instance of EMPromise.
66 67 68 69 70 71 72 73 |
# File 'lib/em_promise.rb', line 66 def initialize(deferrable=nil, trampoline: nil) super() @trampoline = trampoline || Thread.current[:_em_promise_trampoline] || Trampoline.new fulfill(deferrable) if deferrable end |
Class Method Details
.all(enumerable) ⇒ Object
114 115 116 117 118 119 120 121 122 |
# File 'lib/em_promise.rb', line 114 def self.all(enumerable) super(enumerable.map { |input| if input.respond_to?(:promise) input.promise else input end }) end |
.reject(e) ⇒ Object
100 101 102 |
# File 'lib/em_promise.rb', line 100 def self.reject(e) new.tap { |promise| promise.reject(e) } end |
Instance Method Details
#catch_only(*klasses, filter: ->(*) { false }) ⇒ Object
104 105 106 107 108 109 110 111 112 |
# File 'lib/em_promise.rb', line 104 def catch_only(*klasses, filter: ->(*) { false }) catch do |e| if klasses.find { |klass| e.is_a?(klass) } || filter[e] yield e else EMPromise.reject(e) end end end |
#class ⇒ Object
Make sure that self.class.new inherits the trampoline
57 58 59 60 61 62 63 64 |
# File 'lib/em_promise.rb', line 57 def class tramp = @trampoline klass = super.dup klass.define_singleton_method(:new) do |*args| super(*args, trampoline: tramp) end klass end |
#defer ⇒ Object
84 85 86 |
# File 'lib/em_promise.rb', line 84 def defer @trampoline.submit { yield } end |
#fulfill(value, bind_defer: true) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/em_promise.rb', line 75 def fulfill(value, bind_defer: true) if bind_defer && value.is_a?(EM::Deferrable) value.callback { |x| fulfill(x, bind_defer: false) } value.errback(&method(:reject)) else super(value) end end |
#wait ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/em_promise.rb', line 88 def wait fiber = Fiber.current resume = proc do EM.next_tick { fiber.resume } end self.then(resume, resume) # We might be in a trampoline, so keep that going Trampoline.yield end |