em_promise.rb

 1# Copyright (C) 2017  Stephen Paul Weber <singpolyma@singpolyma.net>
 2#
 3# This file is part of sgx-bwmsgsv2.
 4#
 5# sgx-bwmsgsv2 is free software: you can redistribute it and/or modify it under
 6# the terms of the GNU Affero General Public License as published by the Free
 7# Software Foundation, either version 3 of the License, or (at your option) any
 8# later version.
 9#
10# sgx-bwmsgsv2 is distributed in the hope that it will be useful, but WITHOUT
11# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12# FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
13# details.
14#
15# You should have received a copy of the GNU Affero General Public License along
16# with sgx-bwmsgsv2.  If not, see <http://www.gnu.org/licenses/>.
17
18require "eventmachine"
19require "promise"
20
21class EMPromise < Promise
22	def initialize(deferrable=nil)
23		super()
24		fulfill(deferrable) if deferrable
25	end
26
27	def fulfill(value, bind_defer=true)
28		if bind_defer && value.is_a?(EM::Deferrable)
29			value.callback { |x| fulfill(x, false) }
30			value.errback(&method(:reject))
31		else
32			super(value)
33		end
34	end
35
36	def defer
37		EM.next_tick { yield }
38	end
39
40	def wait
41		fiber = Fiber.current
42		resume = proc do |arg|
43			defer { fiber.resume(arg) }
44		end
45
46		self.then(resume, resume)
47		Fiber.yield
48	end
49
50	def self.reject(e)
51		new.tap { |promise| promise.reject(e) }
52	end
53
54	def self.all(enumerable)
55		super(enumerable.map { |input|
56			if input.respond_to?(:promise)
57				input.promise
58			else
59				input
60			end
61		})
62	end
63end
64
65module EventMachine
66	module Deferrable
67		def promise
68			EMPromise.new(self)
69		end
70
71		[:then, :rescue, :catch].each do |method|
72			define_method(method) do |*args, &block|
73				promise.public_send(method, *args, &block)
74			end
75		end
76	end
77end