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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3b9252ef4d1a0e87a58dab94d940c0e007daa57bb29aff67607a53ddd9e49eff
4
- data.tar.gz: c9deea390a562b41346c6b5c6c067d30bb36e8bd71f90cadf7b78928a56be383
3
+ metadata.gz: 7197eaa859e861cb7a0e0d4ba684a8e8095bfcedd1350abe3464d87b845bdf33
4
+ data.tar.gz: df5ee24ccbfca15d4d87978dba61a178e6776ba6233cdbfa497ed55f666b988c
5
5
  SHA512:
6
- metadata.gz: 1cdccfffad92001ced9be4d66298741a1fdf565ce0e2324f9acf53246901f527a9d36d3b26713dcd6128f07df05d7d4a66f762bff4831948b40dba61b3a571b4
7
- data.tar.gz: 0ed5df6e816ae1f221108a5990835e446ac6bc288557131e66baafccf7132ba86881eaa190139e953db5f83e210ceb4d716542b72da4c06ffff89b24a7118647
6
+ metadata.gz: 3eda1c045cbcf0dde66282e473f8b577049c09c97ddcaeb8838ff6275b6e772b02495d6b2b0cbe56bcbfe310c73756784765e5d4e1c16ead1c8f59401d29a7d1
7
+ data.tar.gz: 1e4f2a1110ea4ec82324c0b9096e2cf808141f83d413cc9ee721260be08b681e7f2e452ee050fbbeb5ee9c39e3e25d93ed704579cfc926e0e9268e7c12178ebd
@@ -18,5 +18,6 @@ Gem::Specification.new do |spec|
18
18
  end
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_dependency "eventmachine"
21
22
  spec.add_dependency "promise.rb", "~> 0.7.4"
22
23
  end
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
- def initialize(deferrable=nil)
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
- EM.next_tick { yield }
64
+ @trampoline.submit { yield }
21
65
  end
22
66
 
23
67
  def wait
24
68
  fiber = Fiber.current
25
- resume = proc do |arg|
26
- defer { fiber.resume(arg) }
69
+ resume = proc do
70
+ EM.next_tick { fiber.resume }
27
71
  end
28
72
 
29
73
  self.then(resume, resume)
30
- Fiber.yield
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.2
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-18 00:00:00.000000000 Z
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