From 9940fbbf9fb4f72336c38beb2a3166b0136f5bc4 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Wed, 26 Jul 2023 10:51:38 +0100 Subject: [PATCH] assign client updater methods as base lambdas --- lib/obsws/base.rb | 18 +++++++++++------- lib/obsws/event.rb | 16 +++++++--------- lib/obsws/req.rb | 10 ++++------ 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/lib/obsws/base.rb b/lib/obsws/base.rb index 62255d7..6601c7a 100644 --- a/lib/obsws/base.rb +++ b/lib/obsws/base.rb @@ -2,7 +2,6 @@ require "socket" require "websocket/driver" require "digest/sha2" require "json" -require "observer" require "waitutil" require_relative "mixin" @@ -23,10 +22,10 @@ module OBSWS end class Base - include Observable include Mixin::OPCodes - attr_reader :id, :driver, :closed + attr_reader :closed + attr_writer :updater def initialize(**kwargs) host = kwargs[:host] || "localhost" @@ -56,7 +55,7 @@ module OBSWS ) { @identified } end - def start_driver + private def start_driver Thread.new do @driver.start @@ -68,6 +67,12 @@ module OBSWS end end + public def stop_driver + @driver.close + end + + private + def auth_token(salt:, challenge:) Digest::SHA256.base64digest( Digest::SHA256.base64digest(@password + salt) + challenge @@ -99,12 +104,11 @@ module OBSWS when Mixin::OPCodes::IDENTIFIED @identified = true when Mixin::OPCodes::EVENT, Mixin::OPCodes::REQUESTRESPONSE - changed - notify_observers(data[:op], data[:d]) + @updater.call(data[:op], data[:d]) end end - def req(id, type_, data = nil) + public def req(id, type_, data = nil) payload = { op: Mixin::OPCodes::REQUEST, d: { diff --git a/lib/obsws/event.rb b/lib/obsws/event.rb index 17d542c..7223f50 100644 --- a/lib/obsws/event.rb +++ b/lib/obsws/event.rb @@ -70,20 +70,18 @@ module OBSWS kwargs[:subs] ||= SUBS::LOW_VOLUME @base_client = Base.new(**kwargs) LOGGER.info("#{self} succesfully identified with server") - @base_client.add_observer(self) + @base_client.updater = ->(op_code, data) { + if op_code == Mixin::OPCodes::EVENT + event = data[:eventType] + data = data.fetch(:eventData, {}) + notify_observers(event, Mixin::Data.new(data, data.keys)) + end + } end def to_s self.class.name.split("::").last(2).join("::") end - - def update(op_code, data) - if op_code == Mixin::OPCodes::EVENT - event = data[:eventType] - data = data.key?(:eventData) ? data[:eventData] : {} - notify_observers(event, Mixin::Data.new(data, data.keys)) - end - end end end end diff --git a/lib/obsws/req.rb b/lib/obsws/req.rb index 7bc2cc3..7803d9b 100644 --- a/lib/obsws/req.rb +++ b/lib/obsws/req.rb @@ -15,7 +15,9 @@ module OBSWS def initialize(**kwargs) @base_client = Base.new(**kwargs) LOGGER.info("#{self} succesfully identified with server") - @base_client.add_observer(self) + @base_client.updater = ->(op_code, data) { + @response = data if op_code == Mixin::OPCodes::REQUESTRESPONSE + } @response = {requestId: 0} end @@ -26,7 +28,7 @@ module OBSWS def run yield(self) ensure - close + stop_driver WaitUtil.wait_for_condition( "driver to close", delay_sec: 0.01, @@ -34,10 +36,6 @@ module OBSWS ) { @base_client.closed } end - def update(op_code, data) - @response = data if op_code == Mixin::OPCodes::REQUESTRESPONSE - end - def call(req, data = nil) id = rand(1..1000) @base_client.req(id, req, data)