lspace 0.7 → 0.8
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/lib/lspace/class_methods.rb +5 -1
- data/lib/lspace/eventmachine.rb +25 -0
- data/lspace.gemspec +1 -1
- data/spec/eventmachine_spec.rb +34 -0
- metadata +1 -1
data/lib/lspace/class_methods.rb
CHANGED
@@ -16,7 +16,11 @@ class LSpace
|
|
16
16
|
# @param [Proc] block The logical block that will be run in the clean LSpace
|
17
17
|
# @see LSpace.enter
|
18
18
|
def self.clean(&block)
|
19
|
-
|
19
|
+
if block_given?
|
20
|
+
enter new({}, nil), &block
|
21
|
+
else
|
22
|
+
new({}, nil)
|
23
|
+
end
|
20
24
|
end
|
21
25
|
|
22
26
|
# Create a new LSpace with the given keys set to the given values, and run the
|
data/lib/lspace/eventmachine.rb
CHANGED
@@ -28,6 +28,31 @@ module EventMachine
|
|
28
28
|
s
|
29
29
|
end
|
30
30
|
end
|
31
|
+
|
32
|
+
# By default EM::run will temporarily switch to a clean LSpace to ensure
|
33
|
+
# that all your around_filters are run for every block executed on the
|
34
|
+
# reactor.
|
35
|
+
#
|
36
|
+
# If you don't want this behaviour, you can call EM.run_in_current_lspace
|
37
|
+
# which will continue using the current lspace.
|
38
|
+
alias_method :run_in_current_lspace, :run
|
39
|
+
|
40
|
+
# Override run to ensure that the LSpace context is re-entered more
|
41
|
+
# appropriately.
|
42
|
+
#
|
43
|
+
# This ensures that all blocks scheduled on the eventmachine reactor will
|
44
|
+
# be wrapped in the around_filters you define, and makes implementing things
|
45
|
+
# like a global error handler, or em-monitor a little easier.
|
46
|
+
def run(*args, &block)
|
47
|
+
lspace = LSpace.current
|
48
|
+
LSpace.clean do
|
49
|
+
run_in_current_lspace(*args) do |*a, &b|
|
50
|
+
lspace.enter do
|
51
|
+
block.call *a, &b if block_given?
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
31
56
|
end
|
32
57
|
|
33
58
|
# Many EM APIs (e.g. em-http-request) are based on deferrables. Preserving lspace for
|
data/lspace.gemspec
CHANGED
data/spec/eventmachine_spec.rb
CHANGED
@@ -132,5 +132,39 @@ describe LSpace do
|
|
132
132
|
$server.should == [[:post_init, :server, :baz], [:receive_data, :server, :baz], [:unbind, :server, :baz]]
|
133
133
|
$tick.should == [[:tick, :baz]]
|
134
134
|
end
|
135
|
+
|
136
|
+
it "should ensure that around_filters defined outside EM::run are run on each callback inside the reactor" do
|
137
|
+
LSpace[:count] = 0
|
138
|
+
LSpace.around_filter do |&block|
|
139
|
+
LSpace[:count] += 1
|
140
|
+
block.call
|
141
|
+
end
|
142
|
+
|
143
|
+
LSpace[:count].should == 0
|
144
|
+
EM::run do
|
145
|
+
LSpace[:count].should == 1
|
146
|
+
EM::next_tick do
|
147
|
+
LSpace[:count].should == 2
|
148
|
+
EM::stop
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
it "should not re-enter LSpace if run_in_current_lspace is used" do
|
154
|
+
LSpace[:count] = 0
|
155
|
+
LSpace.around_filter do |&block|
|
156
|
+
LSpace[:count] += 1
|
157
|
+
block.call
|
158
|
+
end
|
159
|
+
|
160
|
+
LSpace[:count].should == 0
|
161
|
+
EM::run_in_current_lspace do
|
162
|
+
LSpace[:count].should == 0
|
163
|
+
EM::next_tick do
|
164
|
+
LSpace[:count].should == 0
|
165
|
+
EM::stop
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
135
169
|
end
|
136
170
|
end
|