mirror of
https://github.com/onyx-and-iris/obsws-ruby.git
synced 2025-01-18 21:40:48 +00:00
refactor Callbacks
This commit is contained in:
parent
438f3b1659
commit
d12a1a5954
@ -37,25 +37,26 @@ module OBSWS
|
|||||||
module Callbacks
|
module Callbacks
|
||||||
include Util
|
include Util
|
||||||
|
|
||||||
|
def observers
|
||||||
|
@observers ||= []
|
||||||
|
end
|
||||||
|
|
||||||
def add_observer(observer)
|
def add_observer(observer)
|
||||||
@observers = [] unless defined?(@observers)
|
|
||||||
observer = [observer] if !observer.respond_to? :each
|
observer = [observer] if !observer.respond_to? :each
|
||||||
observer.each { |o| @observers.append(o) }
|
observer.each { |o| observers << o }
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_observer(observer)
|
def remove_observer(observer)
|
||||||
@observers.delete(observer)
|
observers.delete(observer)
|
||||||
end
|
end
|
||||||
|
|
||||||
def notify_observers(event, data)
|
def notify_observers(event, data)
|
||||||
if defined?(@observers)
|
observers.each do |o|
|
||||||
@observers.each do |o|
|
if o.respond_to? "on_#{event.to_snake}"
|
||||||
if o.respond_to? "on_#{event.to_snake}"
|
if data.empty?
|
||||||
if data.empty?
|
o.send("on_#{event.to_snake}")
|
||||||
o.send("on_#{event.to_snake}")
|
else
|
||||||
else
|
o.send("on_#{event.to_snake}", data)
|
||||||
o.send("on_#{event.to_snake}", data)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user