sync-defer 0.9.4 → 0.9.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES.md +4 -0
- data/Rakefile +1 -1
- data/lib/sync-defer.rb +29 -10
- data/sync-defer.gemspec +1 -1
- data/test/test_sync-defer.rb +14 -5
- metadata +1 -1
data/CHANGES.md
CHANGED
data/Rakefile
CHANGED
data/lib/sync-defer.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
|
2
|
+
require 'fiber'
|
3
|
+
|
2
4
|
begin
|
3
5
|
require 'eventmachine/sync-defer'
|
4
6
|
rescue LoadError
|
@@ -10,23 +12,40 @@ rescue LoadError
|
|
10
12
|
end
|
11
13
|
|
12
14
|
module SyncDefer
|
15
|
+
# assume we would always require 'rest-core' in root fiber
|
16
|
+
RootFiber = Fiber.current
|
17
|
+
|
13
18
|
module_function
|
14
19
|
def defer *args, &block
|
15
|
-
if
|
20
|
+
if root_fiber?
|
21
|
+
fallback("Not called inside a fiber.", *args, &block)
|
22
|
+
|
23
|
+
elsif Object.const_defined?(:EventMachine) &&
|
24
|
+
EventMachine.reactor_running?
|
16
25
|
EventMachine::SyncDefer.defer(*args, &block)
|
26
|
+
|
17
27
|
elsif Object.const_defined?(:Coolio) &&
|
18
28
|
Coolio::Loop.default.has_active_watchers?
|
19
29
|
Coolio::SyncDefer.defer(*args, &block)
|
30
|
+
|
31
|
+
else
|
32
|
+
fallback("No reactor found.", *args, block)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def root_fiber?
|
37
|
+
RootFiber == Fiber.current
|
38
|
+
end
|
39
|
+
|
40
|
+
def fallback message, *args, &block
|
41
|
+
$stderr.puts("SyncDefer: WARN: #{message}")
|
42
|
+
$stderr.puts(" Falling back to run the computation directly.")
|
43
|
+
$stderr.puts(" Called from: #{caller.last(5).inspect}")
|
44
|
+
args << block if block_given?
|
45
|
+
if args.size == 1
|
46
|
+
args.first.call
|
20
47
|
else
|
21
|
-
|
22
|
-
"Only cool.io and eventmachine are supported.")
|
23
|
-
$stderr.puts(" Called from: #{caller.last(5).inspect}")
|
24
|
-
args << block if block_given?
|
25
|
-
if args.size == 1
|
26
|
-
args.first.call
|
27
|
-
else
|
28
|
-
args.map(&:call)
|
29
|
-
end
|
48
|
+
args.map(&:call)
|
30
49
|
end
|
31
50
|
end
|
32
51
|
end
|
data/sync-defer.gemspec
CHANGED
data/test/test_sync-defer.rb
CHANGED
@@ -143,18 +143,27 @@ rescue LoadError => e
|
|
143
143
|
end
|
144
144
|
|
145
145
|
describe SyncDefer do
|
146
|
-
|
147
|
-
|
148
|
-
end
|
146
|
+
before do mock($stderr).puts(is_a(String)).times(3) end
|
147
|
+
after do RR.verify end
|
149
148
|
|
150
149
|
should 'also work without a reactor, but print a warning' do
|
151
|
-
mock($stderr).puts(is_a(String)).times(2)
|
152
150
|
SyncDefer.defer{ 123 }.should.eql 123
|
153
151
|
end
|
154
152
|
|
155
153
|
should 'multiple computations' do
|
156
|
-
mock($stderr).puts(is_a(String)).times(2)
|
157
154
|
SyncDefer.defer(lambda{1}, lambda{2}){ 3 }.
|
158
155
|
inspect.should.eql [1, 2, 3].inspect
|
159
156
|
end
|
157
|
+
|
158
|
+
should 'also fallback if there is no fibers in EM' do
|
159
|
+
EM.run{ SyncDefer.defer{ 1 }.should.eql 1; EM.stop }
|
160
|
+
end if Object.const_defined?(:EventMachine)
|
161
|
+
|
162
|
+
should 'also fallback if there is no fibers in Coolio' do
|
163
|
+
watcher = Coolio::AsyncWatcher.new.attach(Coolio::Loop.default)
|
164
|
+
watcher.on_signal{detach}
|
165
|
+
SyncDefer.defer{ 1 }.should.eql 1
|
166
|
+
watcher.signal
|
167
|
+
Coolio::Loop.default.run
|
168
|
+
end if Object.const_defined?(:Coolio)
|
160
169
|
end
|