mixin Callback module

observers should now be registered directly onto vm object

example updated to reflect changes
This commit is contained in:
onyx-and-iris 2023-07-28 18:51:15 +01:00
parent 0fd4f49670
commit eaa9436081
6 changed files with 21 additions and 22 deletions

View File

@ -683,7 +683,7 @@ example:
class App(): class App():
def initialize(vm) def initialize(vm)
@vm = vm @vm = vm
@vm.callback.register(method(:on_pdirty)) @vm.register(method(:on_pdirty))
... ...
def on_pdirty def on_pdirty

View File

@ -3,7 +3,7 @@ require_relative "../../lib/voicemeeter"
class Main class Main
def initialize(vm) def initialize(vm)
@vm = 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 end
def run def run

View File

@ -5,7 +5,7 @@ class Main
def initialize(vm) def initialize(vm)
@vm = vm @vm = vm
@vm.callback.register(self) @vm.register(self)
end end
def run def run

View File

@ -13,10 +13,10 @@ module Voicemeeter
# Base class for Remote types # Base class for Remote types
include Logging include Logging
include Worker include Worker
include Events::Callback
prepend Util::Cache prepend Util::Cache
attr_reader :kind, :midi, :event, :callback, :running, :delay, :cache attr_reader :kind, :midi, :event, :running, :delay, :cache
alias_method :observer, :callback
RATELIMIT = 0.033 RATELIMIT = 0.033
DELAY = 0.001 DELAY = 0.001
@ -31,7 +31,6 @@ module Voicemeeter
**(kwargs.select { |k, _| %i[pdirty mdirty ldirty midi].include? k }) **(kwargs.select { |k, _| %i[pdirty mdirty ldirty midi].include? k })
) )
@midi = Midi.new @midi = Midi.new
@callback = Events::Callback.new
@que = Queue.new @que = Queue.new
@cache = {strip_mode: 0} @cache = {strip_mode: 0}
end end

View File

@ -2,25 +2,25 @@ require_relative "logger"
module Voicemeeter module Voicemeeter
module Events module Events
class Callback module Callback
def initialize private def callbacks
@callbacks = [] @callbacks ||= []
end end
def register(*args) def register(*cbs)
args.each { |callback| @callbacks.append callback } cbs.each { |cb| callbacks << cb unless callbacks.include? cb }
end end
def deregister(*args) def deregister(*cbs)
@callbacks.reject! { |c| args.include? c } callbacks.reject! { |cb| cbs.include? cb }
end end
def trigger(event) private def trigger(event)
@callbacks.each do |callback| callbacks.each do |callback|
if callback.respond_to? :on_update if callback.is_a? Method
callback.call if callback.name == event
elsif callback.respond_to? :on_update
callback.on_update event.to_s[3..] callback.on_update event.to_s[3..]
elsif callback.name == event
callback.call
end end
end end
end end

View File

@ -33,15 +33,15 @@ module Voicemeeter
logger.debug("closing worker thread") logger.debug("closing worker thread")
break break
end end
callback.trigger :on_pdirty if e_from_que == :pdirty && pdirty? trigger :on_pdirty if e_from_que == :pdirty && pdirty?
callback.trigger :on_mdirty if e_from_que == :mdirty && mdirty? trigger :on_mdirty if e_from_que == :mdirty && mdirty?
callback.trigger :on_midi if e_from_que == :midi && get_midi_message trigger :on_midi if e_from_que == :midi && get_midi_message
if e_from_que == :ldirty && ldirty? if e_from_que == :ldirty && ldirty?
cache[:strip_comp] = cache[:strip_level].zip(cache[:strip_buf]).map { |a, b| a != b } 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[:bus_comp] = cache[:bus_level].zip(cache[:bus_buf]).map { |a, b| a != b }
cache[:strip_level] = cache[:strip_buf] cache[:strip_level] = cache[:strip_buf]
cache[:bus_level] = cache[:bus_buf] cache[:bus_level] = cache[:bus_buf]
callback.trigger :on_ldirty trigger :on_ldirty
end end
end end
end end