diff --git a/README.md b/README.md index 1d37445..e698b7a 100644 --- a/README.md +++ b/README.md @@ -683,7 +683,7 @@ example: class App(): def initialize(vm) @vm = vm - @vm.callback.register(method(:on_pdirty)) + @vm.register(method(:on_pdirty)) ... def on_pdirty diff --git a/examples/events/main.rb b/examples/events/main.rb index bf34286..7248bdc 100644 --- a/examples/events/main.rb +++ b/examples/events/main.rb @@ -3,7 +3,7 @@ require_relative "../../lib/voicemeeter" class Main def initialize(vm) @vm = vm - @vm.callback.register(method(:on_pdirty), method(:on_mdirty), method(:on_midi), method(:on_ldirty)) + @vm.register(method(:on_pdirty), method(:on_mdirty), method(:on_midi), method(:on_ldirty)) end def run diff --git a/examples/midi/main.rb b/examples/midi/main.rb index e17b453..b0450af 100644 --- a/examples/midi/main.rb +++ b/examples/midi/main.rb @@ -5,7 +5,7 @@ class Main def initialize(vm) @vm = vm - @vm.callback.register(self) + @vm.register(self) end def run diff --git a/lib/voicemeeter/base.rb b/lib/voicemeeter/base.rb index c4f4b3a..3bb9128 100644 --- a/lib/voicemeeter/base.rb +++ b/lib/voicemeeter/base.rb @@ -13,10 +13,10 @@ module Voicemeeter # Base class for Remote types include Logging include Worker + include Events::Callback prepend Util::Cache - attr_reader :kind, :midi, :event, :callback, :running, :delay, :cache - alias_method :observer, :callback + attr_reader :kind, :midi, :event, :running, :delay, :cache RATELIMIT = 0.033 DELAY = 0.001 @@ -31,7 +31,6 @@ module Voicemeeter **(kwargs.select { |k, _| %i[pdirty mdirty ldirty midi].include? k }) ) @midi = Midi.new - @callback = Events::Callback.new @que = Queue.new @cache = {strip_mode: 0} end diff --git a/lib/voicemeeter/event.rb b/lib/voicemeeter/event.rb index 0046411..2415a7a 100644 --- a/lib/voicemeeter/event.rb +++ b/lib/voicemeeter/event.rb @@ -2,25 +2,25 @@ require_relative "logger" module Voicemeeter module Events - class Callback - def initialize - @callbacks = [] + module Callback + private def callbacks + @callbacks ||= [] end - def register(*args) - args.each { |callback| @callbacks.append callback } + def register(*cbs) + cbs.each { |cb| callbacks << cb unless callbacks.include? cb } end - def deregister(*args) - @callbacks.reject! { |c| args.include? c } + def deregister(*cbs) + callbacks.reject! { |cb| cbs.include? cb } end - def trigger(event) - @callbacks.each do |callback| - if callback.respond_to? :on_update + private def trigger(event) + callbacks.each do |callback| + if callback.is_a? Method + callback.call if callback.name == event + elsif callback.respond_to? :on_update callback.on_update event.to_s[3..] - elsif callback.name == event - callback.call end end end diff --git a/lib/voicemeeter/worker.rb b/lib/voicemeeter/worker.rb index 5e0a741..b87583f 100644 --- a/lib/voicemeeter/worker.rb +++ b/lib/voicemeeter/worker.rb @@ -33,15 +33,15 @@ module Voicemeeter logger.debug("closing worker thread") break end - callback.trigger :on_pdirty if e_from_que == :pdirty && pdirty? - callback.trigger :on_mdirty if e_from_que == :mdirty && mdirty? - callback.trigger :on_midi if e_from_que == :midi && get_midi_message + trigger :on_pdirty if e_from_que == :pdirty && pdirty? + trigger :on_mdirty if e_from_que == :mdirty && mdirty? + trigger :on_midi if e_from_que == :midi && get_midi_message if e_from_que == :ldirty && ldirty? cache[:strip_comp] = cache[:strip_level].zip(cache[:strip_buf]).map { |a, b| a != b } cache[:bus_comp] = cache[:bus_level].zip(cache[:bus_buf]).map { |a, b| a != b } cache[:strip_level] = cache[:strip_buf] cache[:bus_level] = cache[:bus_buf] - callback.trigger :on_ldirty + trigger :on_ldirty end end end