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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '08090445dc90432a6c73b7101f6c361a899b59cb710f0131b272949e2d5f8870'
4
- data.tar.gz: bcc073fe296e3616ff82cce4e8060ce3bde64bdaf4f63a01622e8d43e36d492d
3
+ metadata.gz: 72a70beea8dc08eb215bb38dbc39a2c98f180c8c53367bdf1eb892f1d5fc6c53
4
+ data.tar.gz: e1bf42f248972e4b5e38bcf9750ffe845020640b6e386da36a5d24bd2b9d0701
5
5
  SHA512:
6
- metadata.gz: 3569fb2c9967bcc5963a8e592d9d0a87313a0368f00756b1a7469a61fc99ff2770fd42e9b3e3233d190e37a4a4dbb2a1e8c6935816869d714d882c93d7c34902
7
- data.tar.gz: 6b788c661ff9fabee37120ef3bf072ef45804552f8db52de595fe5432eeeca7e70402b48ed9127b0078fa144c945fce31800d2b21ab0161fb6e388682cc7400d
6
+ metadata.gz: 9aedc31b0acf65bd786cdfe364c54f13dbe8ca1f0f952f1ecbf4bdf36278c7c93a70a05df500b2cfa6d1931e80709d027dd3a14a64abc830f9e848cfc86bac3a
7
+ data.tar.gz: 7d76798c8347bbaa9843c16cea76bfdb21cc63504414b0d93d7c92c65591c0e42ecb382e08bce068c3fbeb6e31b4903ac58425d549aae2eec07f80de52652f4d
@@ -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 do |f|
15
+ `git ls-files -z`.split("\x00".b).reject { |f|
16
16
  f.start_with?(".", "test/", "scripts/") ||
17
17
  f == "Makefile" || f == "Gemfile"
18
- end
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
- def initialize
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 do |blk|
23
+ @fiber = Fiber.new { |blk|
14
24
  Thread.current[:_em_promise_trampoline] = self
15
- loop do
16
- self.ready = false
17
- result = blk.call
18
- self.ready = true
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=true)
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
- 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
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
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: 2021-11-02 00:00:00.000000000 Z
11
+ date: 2022-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: eventmachine