em_promise.rb 0.0.4 → 0.0.5
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 +2 -2
- data/lib/em_promise.rb +34 -23
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 72a70beea8dc08eb215bb38dbc39a2c98f180c8c53367bdf1eb892f1d5fc6c53
|
4
|
+
data.tar.gz: e1bf42f248972e4b5e38bcf9750ffe845020640b6e386da36a5d24bd2b9d0701
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9aedc31b0acf65bd786cdfe364c54f13dbe8ca1f0f952f1ecbf4bdf36278c7c93a70a05df500b2cfa6d1931e80709d027dd3a14a64abc830f9e848cfc86bac3a
|
7
|
+
data.tar.gz: 7d76798c8347bbaa9843c16cea76bfdb21cc63504414b0d93d7c92c65591c0e42ecb382e08bce068c3fbeb6e31b4903ac58425d549aae2eec07f80de52652f4d
|
data/em_promise.rb.gemspec
CHANGED
@@ -12,10 +12,10 @@ Gem::Specification.new do |spec|
|
|
12
12
|
spec.homepage = "https://git.singpolyma.net/em_promise.rb"
|
13
13
|
|
14
14
|
spec.files =
|
15
|
-
`git ls-files -z`.split("\x00".b).reject
|
15
|
+
`git ls-files -z`.split("\x00".b).reject { |f|
|
16
16
|
f.start_with?(".", "test/", "scripts/") ||
|
17
17
|
f == "Makefile" || f == "Gemfile"
|
18
|
-
|
18
|
+
}
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
21
|
spec.add_dependency "eventmachine"
|
data/lib/em_promise.rb
CHANGED
@@ -8,28 +8,49 @@ class EMPromise < Promise
|
|
8
8
|
class Trampoline
|
9
9
|
attr_accessor :ready
|
10
10
|
|
11
|
-
|
11
|
+
class SubmittedBlock
|
12
|
+
def initialize(blk)
|
13
|
+
@blk = blk
|
14
|
+
end
|
15
|
+
|
16
|
+
def call(*args, **kwargs, &blk)
|
17
|
+
@blk.call(*args, **kwargs, &blk)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def initialize(&initial_blk)
|
12
22
|
@ready = true
|
13
|
-
@fiber = Fiber.new
|
23
|
+
@fiber = Fiber.new { |blk|
|
14
24
|
Thread.current[:_em_promise_trampoline] = self
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
blk = Fiber.yield result
|
20
|
-
end
|
21
|
-
end
|
25
|
+
self.ready = false
|
26
|
+
Trampoline.yield(blk.call)
|
27
|
+
}
|
28
|
+
@fiber.resume(&initial_blk) if initial_blk
|
22
29
|
end
|
23
30
|
|
24
31
|
def submit(&blk)
|
25
32
|
EM.next_tick do
|
26
33
|
if ready
|
27
|
-
@fiber.resume(blk)
|
34
|
+
@fiber.resume(SubmittedBlock.new(blk))
|
28
35
|
else
|
29
36
|
submit(&blk)
|
30
37
|
end
|
31
38
|
end
|
32
39
|
end
|
40
|
+
|
41
|
+
def self.yield(arg=nil)
|
42
|
+
tramp = Thread.current[:_em_promise_trampoline]
|
43
|
+
raise "Trampoline already ready?" if tramp&.ready
|
44
|
+
|
45
|
+
loop do
|
46
|
+
tramp&.ready = true
|
47
|
+
blk = Fiber.yield arg
|
48
|
+
tramp&.ready = false
|
49
|
+
return arg unless blk.is_a?(SubmittedBlock)
|
50
|
+
|
51
|
+
arg = blk.call
|
52
|
+
end
|
53
|
+
end
|
33
54
|
end
|
34
55
|
|
35
56
|
# Make sure that self.class.new inherits the trampoline
|
@@ -51,9 +72,9 @@ class EMPromise < Promise
|
|
51
72
|
fulfill(deferrable) if deferrable
|
52
73
|
end
|
53
74
|
|
54
|
-
def fulfill(value, bind_defer
|
75
|
+
def fulfill(value, bind_defer: true)
|
55
76
|
if bind_defer && value.is_a?(EM::Deferrable)
|
56
|
-
value.callback { |x| fulfill(x, false) }
|
77
|
+
value.callback { |x| fulfill(x, bind_defer: false) }
|
57
78
|
value.errback(&method(:reject))
|
58
79
|
else
|
59
80
|
super(value)
|
@@ -73,17 +94,7 @@ class EMPromise < Promise
|
|
73
94
|
self.then(resume, resume)
|
74
95
|
|
75
96
|
# We might be in a trampoline, so keep that going
|
76
|
-
|
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
|
97
|
+
Trampoline.yield
|
87
98
|
end
|
88
99
|
|
89
100
|
def self.reject(e)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.5
|
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:
|
11
|
+
date: 2022-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: eventmachine
|