Skip to content

Commit 2dc51ec

Browse files
Merge branch 'master' into oakbani/revise-event-dispatcher
2 parents 082087d + c720b02 commit 2dc51ec

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

lib/optimizely/notification_center.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,28 @@ def initialize(logger, error_handler)
3939

4040
# Adds notification callback to the notification center
4141
#
42-
# @param notification_type - One of the constants in NOTIFICATION_TYPES
43-
# @param notification_callback - Function to call when the event is sent
42+
# @param notification_type - One of the constants in NOTIFICATION_TYPES
43+
# @param notification_callback [lambda, Method, Callable] (default: block) - Called when the event is sent
44+
# @yield Block to be used as callback if callback omitted.
4445
#
4546
# @return [notification ID] Used to remove the notification
4647

47-
def add_notification_listener(notification_type, notification_callback)
48+
def add_notification_listener(notification_type, notification_callback = nil, &block)
4849
return nil unless notification_type_valid?(notification_type)
4950

51+
if notification_callback && block_given?
52+
@logger.log Logger::ERROR, 'Callback and block are mutually exclusive.'
53+
return nil
54+
end
55+
56+
notification_callback ||= block
57+
5058
unless notification_callback
5159
@logger.log Logger::ERROR, 'Callback can not be empty.'
5260
return nil
5361
end
5462

55-
unless notification_callback.is_a? Method
63+
unless notification_callback.respond_to? :call
5664
@logger.log Logger::ERROR, 'Invalid notification callback given.'
5765
return nil
5866
end
@@ -70,7 +78,7 @@ def add_notification_listener(notification_type, notification_callback)
7078
#
7179
# @param notification_id
7280
#
73-
# @return [Boolean] The function returns true if found and removed, false otherwise
81+
# @return [Boolean] true if found and removed, false otherwise
7482

7583
def remove_notification_listener(notification_id)
7684
unless notification_id

spec/notification_center_spec.rb

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@
3030

3131
before(:context) do
3232
class CallBack
33-
def call; end
33+
attr_reader :args
34+
35+
def call(*args)
36+
@args = args
37+
end
3438
end
3539

3640
@callback = CallBack.new
@@ -73,6 +77,15 @@ def call; end
7377
expect(spy_logger).to have_received(:log).once
7478
.with(Logger::ERROR, 'Invalid notification callback given.')
7579
end
80+
81+
it 'should log and return nil if given both callback and block' do
82+
result = notification_center.add_notification_listener(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:ACTIVATE],
83+
@callback_reference) {}
84+
85+
expect(result).to be_nil
86+
expect(spy_logger).to have_received(:log).once
87+
.with(Logger::ERROR, 'Callback and block are mutually exclusive.')
88+
end
7689
end
7790

7891
describe 'test add notification with valid type and callback' do
@@ -484,6 +497,39 @@ def deliver_three; end
484497
expect(spy_logger).to_not have_received(:log)
485498
.with(Logger::INFO, 'delivered three.')
486499
end
500+
501+
it 'should send notifications to blocks' do
502+
actual_args = []
503+
notification_center.add_notification_listener(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:TRACK]) do |*args|
504+
actual_args = args
505+
end
506+
507+
notification_center.send_notifications(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:TRACK],
508+
:arg1, 'arg2', arg3: 4)
509+
510+
expect(actual_args).to eq([:arg1, 'arg2', arg3: 4])
511+
end
512+
513+
it 'should send notifications to lambdas' do
514+
actual_args = []
515+
notification_center.add_notification_listener(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:TRACK],
516+
->(*args) { actual_args = args })
517+
518+
notification_center.send_notifications(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:TRACK],
519+
:arg1, 'arg2', arg3: 4)
520+
521+
expect(actual_args).to eq([:arg1, 'arg2', arg3: 4])
522+
end
523+
524+
it 'should send notifications to callables' do
525+
callback = CallBack.new
526+
notification_center.add_notification_listener(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:TRACK], callback)
527+
528+
notification_center.send_notifications(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:TRACK],
529+
:arg1, 'arg2', arg3: 4)
530+
531+
expect(callback.args).to eq([:arg1, 'arg2', arg3: 4])
532+
end
487533
end
488534

489535
describe '.notification_count' do

0 commit comments

Comments
 (0)