Callbacks module extended

Now supports callback methods as well observer classes

levels example now uses callback methods

register,deregister now alias add/remove observer methods

minor version bump
This commit is contained in:
onyx-and-iris 2023-07-27 14:55:00 +01:00
parent 155cbe019a
commit 4fdebc8178
5 changed files with 32 additions and 26 deletions

View File

@ -16,14 +16,10 @@
### Bundler
```
bundle add 'obsws'
bundle add obsws
bundle install
```
### Gem
`gem install 'obsws'`
## `Use`
#### Example `main.rb`
@ -67,23 +63,28 @@ For a full list of requests refer to [Requests](https://github.com/obsproject/ob
### Events
Register an observer class and define `on_` methods for events. Method names should match the api event but snake cased.
Register `on_` callback methods. Method names should match the api event but snake cased.
example:
```ruby
class Observer
def initialize
@e_client = OBSWS::Events::Client.new(**kwargs)
# register class with the event client
@e_client.add_observer(self)
@e_client = OBSWS::Events::Client.new(host: "localhost", port: 4455, password: "strongpassword")
# register callback methods with the Event client
@e_client.register(
[
method(:on_current_program_scene_changed),
method(:on_input_mute_state_changed)
]
)
end
# define "on_" event methods.
def on_current_program_scene_changed
def on_current_program_scene_changed(data)
...
end
def on_input_mute_state_changed
def on_input_mute_state_changed(data)
...
end
...

View File

@ -21,11 +21,9 @@ class Main
def infostring
resp = @r_client.get_version
[
"Using obs version:",
resp.obs_version,
"With websocket version:",
resp.obs_web_socket_version
].join("\n")
"Using obs version: #{resp.obs_version}.",
"With websocket version: #{resp.obs_web_socket_version}"
].join(" ")
end
def on_current_program_scene_changed(data)

View File

@ -1,7 +1,6 @@
require_relative "../../lib/obsws"
require "yaml"
module LevelTypes
VU = 0
POSTFADER = 1
@ -14,7 +13,12 @@ class Main
def initialize(**kwargs)
subs = OBSWS::Events::SUBS::LOW_VOLUME | OBSWS::Events::SUBS::INPUTVOLUMEMETERS
@e_client = OBSWS::Events::Client.new(subs:, **kwargs)
@e_client.add_observer(self)
@e_client.register(
[
method(:on_input_mute_state_changed),
method(:on_input_volume_meters)
]
)
end
def run

View File

@ -43,7 +43,7 @@ module OBSWS
def add_observer(observer)
observer = [observer] if !observer.respond_to? :each
observer.each { |o| observers << o }
observer.each { |o| observers << o unless observers.include? o }
end
def remove_observer(observer)
@ -52,15 +52,18 @@ module OBSWS
def notify_observers(event, data)
observers.each do |o|
if o.respond_to? "on_#{snakecase(event)}"
if data.empty?
o.send("on_#{snakecase(event)}")
else
o.send("on_#{snakecase(event)}", data)
end
if o.is_a? Method
if o.name.to_s == "on_#{snakecase(event)}"
data.empty? ? o.call : o.call(data)
end
elsif o.respond_to? "on_#{snakecase(event)}"
data.empty? ? o.send("on_#{snakecase(event)}") : o.send("on_#{snakecase(event)}", data)
end
end
end
alias_method :register, :add_observer
alias_method :deregister, :remove_observer
end
class Client

View File

@ -7,11 +7,11 @@ module OBSWS
end
def minor
3
4
end
def patch
3
0
end
def to_a