Skip to content

Commit f63950f

Browse files
author
brainopia
committed
Support block observer in IVar, ScheduledTask, Observable
1 parent e84d1e0 commit f63950f

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

lib/concurrent/ivar.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,14 @@ def initialize(value = NO_VALUE, opts = {})
4242
#
4343
# @param [Object] observer the object that will be notified of changes
4444
# @param [Symbol] func symbol naming the method to call when this `Observable` has changes`
45-
def add_observer(observer, func = :update)
45+
def add_observer(observer, func = :update, &block)
4646
direct_notification = false
4747

48+
if block
49+
observer = block
50+
func = :call
51+
end
52+
4853
mutex.synchronize do
4954
if event.set?
5055
direct_notification = true
@@ -54,7 +59,7 @@ def add_observer(observer, func = :update)
5459
end
5560

5661
observer.send(func, Time.now, self.value, reason) if direct_notification
57-
func
62+
observer
5863
end
5964

6065
def set(value)

lib/concurrent/observable.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ module Concurrent
55

66
module Observable
77

8-
def add_observer(*args)
9-
observers.add_observer(*args)
8+
def add_observer(*args, &block)
9+
observers.add_observer(*args, &block)
1010
end
1111

1212
def delete_observer(*args)

lib/concurrent/scheduled_task.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ def cancel
5151
end
5252
alias_method :stop, :cancel
5353

54-
def add_observer(*args)
54+
def add_observer(*args, &block)
5555
if_state(:unscheduled, :pending, :in_progress) do
56-
observers.add_observer(*args)
56+
observers.add_observer(*args, &block)
5757
end
5858
end
5959

0 commit comments

Comments
 (0)