From d12a1a5954f19fa926a4de1b88ce425782995d0a Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Wed, 26 Jul 2023 18:55:19 +0100 Subject: [PATCH] refactor Callbacks --- lib/obsws/event.rb | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/obsws/event.rb b/lib/obsws/event.rb index c56a5cc..3cbddb6 100644 --- a/lib/obsws/event.rb +++ b/lib/obsws/event.rb @@ -37,25 +37,26 @@ module OBSWS module Callbacks include Util + def observers + @observers ||= [] + end + def add_observer(observer) - @observers = [] unless defined?(@observers) observer = [observer] if !observer.respond_to? :each - observer.each { |o| @observers.append(o) } + observer.each { |o| observers << o } end def remove_observer(observer) - @observers.delete(observer) + observers.delete(observer) end def notify_observers(event, data) - if defined?(@observers) - @observers.each do |o| - if o.respond_to? "on_#{event.to_snake}" - if data.empty? - o.send("on_#{event.to_snake}") - else - o.send("on_#{event.to_snake}", data) - end + observers.each do |o| + if o.respond_to? "on_#{event.to_snake}" + if data.empty? + o.send("on_#{event.to_snake}") + else + o.send("on_#{event.to_snake}", data) end end end