em_promise.rb 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/em_promise.rb.gemspec +1 -0
- data/lib/em_promise.rb +62 -6
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7197eaa859e861cb7a0e0d4ba684a8e8095bfcedd1350abe3464d87b845bdf33
|
4
|
+
data.tar.gz: df5ee24ccbfca15d4d87978dba61a178e6776ba6233cdbfa497ed55f666b988c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3eda1c045cbcf0dde66282e473f8b577049c09c97ddcaeb8838ff6275b6e772b02495d6b2b0cbe56bcbfe310c73756784765e5d4e1c16ead1c8f59401d29a7d1
|
7
|
+
data.tar.gz: 1e4f2a1110ea4ec82324c0b9096e2cf808141f83d413cc9ee721260be08b681e7f2e452ee050fbbeb5ee9c39e3e25d93ed704579cfc926e0e9268e7c12178ebd
|
data/em_promise.rb.gemspec
CHANGED
data/lib/em_promise.rb
CHANGED
@@ -1,9 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "eventmachine"
|
4
|
+
require "fiber"
|
2
5
|
require "promise"
|
3
6
|
|
4
7
|
class EMPromise < Promise
|
5
|
-
|
8
|
+
class Trampoline
|
9
|
+
attr_accessor :ready
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@ready = true
|
13
|
+
@fiber = Fiber.new do |blk|
|
14
|
+
Thread.current[:_em_promise_trampoline] = self
|
15
|
+
loop do
|
16
|
+
result = blk.call
|
17
|
+
self.ready = true
|
18
|
+
blk = Fiber.yield result
|
19
|
+
self.ready = false
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def submit(&blk)
|
25
|
+
EM.next_tick do
|
26
|
+
if ready
|
27
|
+
@fiber.resume(blk)
|
28
|
+
else
|
29
|
+
submit(&blk)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Make sure that self.class.new inherits the trampoline
|
36
|
+
def class
|
37
|
+
tramp = @trampoline
|
38
|
+
klass = super.dup
|
39
|
+
klass.define_singleton_method(:new) do |*args|
|
40
|
+
super(*args, trampoline: tramp)
|
41
|
+
end
|
42
|
+
klass
|
43
|
+
end
|
44
|
+
|
45
|
+
def initialize(deferrable=nil, trampoline: nil)
|
6
46
|
super()
|
47
|
+
@trampoline =
|
48
|
+
trampoline ||
|
49
|
+
Thread.current[:_em_promise_trampoline] ||
|
50
|
+
Trampoline.new
|
7
51
|
fulfill(deferrable) if deferrable
|
8
52
|
end
|
9
53
|
|
@@ -17,17 +61,29 @@ class EMPromise < Promise
|
|
17
61
|
end
|
18
62
|
|
19
63
|
def defer
|
20
|
-
|
64
|
+
@trampoline.submit { yield }
|
21
65
|
end
|
22
66
|
|
23
67
|
def wait
|
24
68
|
fiber = Fiber.current
|
25
|
-
resume = proc do
|
26
|
-
|
69
|
+
resume = proc do
|
70
|
+
EM.next_tick { fiber.resume }
|
27
71
|
end
|
28
72
|
|
29
73
|
self.then(resume, resume)
|
30
|
-
|
74
|
+
|
75
|
+
# We might be in a trampoline, so keep that going
|
76
|
+
tramp = Thread.current[:_em_promise_trampoline]
|
77
|
+
raise "Trampoline already ready?" if tramp&.ready
|
78
|
+
|
79
|
+
result = nil
|
80
|
+
loop do
|
81
|
+
tramp&.ready = true
|
82
|
+
blk = Fiber.yield result
|
83
|
+
tramp&.ready = false
|
84
|
+
result = blk&.call
|
85
|
+
break unless blk
|
86
|
+
end
|
31
87
|
end
|
32
88
|
|
33
89
|
def self.reject(e)
|
@@ -61,7 +117,7 @@ module EventMachine
|
|
61
117
|
EMPromise.new(self)
|
62
118
|
end
|
63
119
|
|
64
|
-
[:then, :rescue, :catch].each do |method|
|
120
|
+
[:then, :rescue, :catch, :sync].each do |method|
|
65
121
|
define_method(method) do |*args, &block|
|
66
122
|
promise.public_send(method, *args, &block)
|
67
123
|
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: em_promise.rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stephen Paul Weber
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-05
|
11
|
+
date: 2021-08-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: eventmachine
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: promise.rb
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|