~singpolyma/sgx-jmp

ref: 9827b8b48336d249637f88d6921d613ad12b16f1 sgx-jmp/lib/rack_fiber.rb -rw-r--r-- 611 bytes
9827b8b4Stephen Paul Weber Allow finishing adming command 11 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# frozen_string_literal: true

require "em_promise"

module Rack
	class Fiber
		def initialize(app)
			@app = app
		end

		def call(env)
			async_callback = env.delete("async.callback")
			run_fiber(env, async_callback)
			throw :async
		end

	protected

		def run_fiber(env, async_callback)
			# Use EMPromise to get a Fiber trampoline that can be shared by
			# other EMPromise down the stack. Also works as a normal Fiber if
			# no one uses EMPromise.
			EMPromise.resolve(nil).then {
				async_callback.call(@app.call(env))
			}.catch { |e|
				async_callback.call([500, {}, [e.to_s]])
			}
		end
	end
end