Skip to content

Commit e84d1e0

Browse files
author
brainopia
committed
Support for a block observer in observer sets
1 parent 14e4cb3 commit e84d1e0

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

lib/concurrent/atomic/copy_on_notify_observer_set.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,21 @@ def initialize
1515
# @param [Object] observer the observer to add
1616
# @param [Symbol] func the function to call on the observer during notification. Default is :update
1717
# @return [Symbol] the added function
18-
def add_observer(observer, func=:update)
18+
def add_observer(observer=nil, func=:update, &block)
19+
unless !!observer ^ block # xor
20+
raise ArgumentError, 'should pass observer as a first argument or block'
21+
end
22+
23+
if block
24+
observer = block
25+
func = :call
26+
end
27+
1928
@mutex.lock
2029
@observers[observer] = func
2130
@mutex.unlock
2231

23-
func
32+
observer
2433
end
2534

2635
# @param [Object] observer the observer to remove

lib/concurrent/atomic/copy_on_write_observer_set.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,23 @@ def initialize
1414
# @param [Object] observer the observer to add
1515
# @param [Symbol] func the function to call on the observer during notification. Default is :update
1616
# @return [Symbol] the added function
17-
def add_observer(observer, func=:update)
17+
def add_observer(observer=nil, func=:update, &block)
18+
unless !!observer ^ block # xor
19+
raise ArgumentError, 'should pass observer as a first argument or block'
20+
end
21+
22+
if block
23+
observer = block
24+
func = :call
25+
end
26+
1827
@mutex.lock
1928
new_observers = @observers.dup
2029
new_observers[observer] = func
2130
@observers = new_observers
2231
@mutex.unlock
2332

24-
func
33+
observer
2534
end
2635

2736
# @param [Object] observer the observer to remove

spec/concurrent/atomic/observer_set_shared.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88

99
describe '#add_observer' do
1010

11-
context 'with argument' do
12-
it 'should return the passed function' do
13-
observer_set.add_observer(observer, :a_method).should eq(:a_method)
11+
context 'with arguments' do
12+
it 'should return the observer' do
13+
observer_set.add_observer(observer, :a_method).should == observer
1414
end
1515
end
1616

17-
context 'without arguments' do
18-
it 'should return the default function' do
19-
observer_set.add_observer(observer).should eq(:update)
17+
context 'with a block' do
18+
it 'should return the observer based on a block' do
19+
observer = observer_set.add_observer { :block }
20+
observer.call.should == :block
2021
end
2122
end
2223
end
@@ -61,6 +62,14 @@
6162
observer_set.notify_observers('a string arg')
6263
end
6364

65+
it 'should notify an observer from a block' do
66+
notification = double
67+
expect(notification).to receive(:catch)
68+
69+
observer_set.add_observer {|arg| arg.catch }
70+
observer_set.notify_observers notification
71+
end
72+
6473
it 'can be called many times' do
6574
expect(observer).to receive(:update).with(:an_arg).twice
6675
expect(observer).to receive(:update).with(no_args).once

0 commit comments

Comments
 (0)