Skip to content

Commit 6042e30

Browse files
committed
Fixed bug in observer sets when block returns complex data types.
1 parent b80d707 commit 6042e30

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

lib/concurrent/copy_on_notify_observer_set.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,8 @@ def duplicate_and_clear_observers
7272
def notify_to(observers, *args)
7373
raise ArgumentError.new('cannot give arguments and a block') if block_given? && ! args.empty?
7474
observers.each do |observer, function|
75-
if block_given?
76-
observer.send(function, *[yield].flatten)
77-
else
78-
observer.send(function, *args)
79-
end
75+
args = yield if block_given?
76+
observer.send(function, *args)
8077
end
8178
end
8279
end

lib/concurrent/copy_on_write_observer_set.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,8 @@ def notify_and_delete_observers(*args, &block)
7070
def notify_to(observers, *args)
7171
raise ArgumentError.new('cannot give arguments and a block') if block_given? && ! args.empty?
7272
observers.each do |observer, function|
73-
if block_given?
74-
observer.send(function, *[yield].flatten)
75-
else
76-
observer.send(function, *args)
77-
end
73+
args = yield if block_given?
74+
observer.send(function, *args)
7875
end
7976
end
8077

lib/concurrent/dataflow.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ def update(time, value, reason)
4343
# Concurrent::dataflow(n1, n2) { |v1, v2| v1 + v2 }
4444
# end
4545
# end
46-
#
46+
#
4747
# f = fib(14) #=> #<Concurrent::Future:0x000001019a26d8 ...
48-
# sleep(0.5)
49-
#
50-
# f.value #=> 377
48+
#
49+
# # wait up to 1 second for the answer...
50+
# f.value(1) #=> 377
5151
#
5252
# @param [Future] inputs zero or more +Future+ operations that this dataflow depends upon
5353
#

spec/concurrent/observer_set_shared.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,20 @@
110110
observer_set.notify_observers{ [1, 2, 3, 4] }
111111
end
112112

113+
it 'accepts blocks returning a single value' do
114+
115+
expect(observer).to receive(:update).with(:foo)
116+
observer_set.add_observer(observer)
117+
observer_set.notify_observers{ :foo }
118+
end
119+
120+
it 'accepts block return values that include arrays' do
121+
122+
expect(observer).to receive(:update).with(1, [2, 3], 4)
123+
observer_set.add_observer(observer)
124+
observer_set.notify_observers{ [1, [2, 3], 4] }
125+
end
126+
113127
it 'raises an exception if given both arguments and a block' do
114128

115129
observer_set.add_observer(observer)

0 commit comments

Comments
 (0)