sync-defer 0.9.4 → 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # CHANGES
2
2
 
3
+ ## sync-defer 0.9.5 -- 2012-03-21
4
+
5
+ * Also fall back whenever it's not wrapped inside a fiber.
6
+
3
7
  ## sync-defer 0.9.4 -- 2012-03-21
4
8
 
5
9
  * Fixed a bug that where there's an exception in multiple computations,
data/Rakefile CHANGED
@@ -8,7 +8,7 @@ desc 'Generate gemspec'
8
8
  task 'gem:spec' do
9
9
  Gemgem.spec = Gemgem.create do |s|
10
10
  s.name = 'sync-defer'
11
- s.version = '0.9.4'
11
+ s.version = '0.9.5'
12
12
 
13
13
  %w[].each{ |g| s.add_runtime_dependency(g) }
14
14
  end
@@ -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 Object.const_defined?(:EventMachine) && EventMachine.reactor_running?
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
- $stderr.puts("SyncDefer: WARN: No reactor found. " \
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
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "sync-defer"
5
- s.version = "0.9.4"
5
+ s.version = "0.9.5"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Lin Jen-Shin (godfat)"]
@@ -143,18 +143,27 @@ rescue LoadError => e
143
143
  end
144
144
 
145
145
  describe SyncDefer do
146
- after do
147
- RR.verify
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sync-defer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.4
4
+ version: 0.9.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: