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
53end
54
55module EventMachine
56	module Deferrable
57		def promise
58			EMPromise.new(self)
59		end
60
61		[:then, :rescue, :catch].each do |method|
62			define_method(method) do |*args, &block|
63				promise.public_send(method, *args, &block)
64			end
65		end
66	end
67end