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():
def initialize(vm)
@vm = vm
@vm.callback.register(method(:on_pdirty))
@vm.register(method(:on_pdirty))
...
def on_pdirty

View File

@ -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

View File

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

View File

@ -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

View File

@ -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

View File

@ -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