From 2533f1c16210097b0da1ef8f9bca2b79e3a2b203 Mon Sep 17 00:00:00 2001 From: onyx-and-iris <75868496+onyx-and-iris@users.noreply.github.com> Date: Sun, 18 Sep 2022 05:37:10 +0100 Subject: [PATCH] refactor observer example. newObserver factory method added --- examples/observer/main.go | 47 ++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/examples/observer/main.go b/examples/observer/main.go index 9e204fc..dab89e0 100644 --- a/examples/observer/main.go +++ b/examples/observer/main.go @@ -8,18 +8,29 @@ import ( "github.com/onyx-and-iris/voicemeeter-api-go" ) +// observer represents a single receiver of updates type observer struct { vm *voicemeeter.Remote } -func (o observer) Register() { +// newObserver add ldirty events to the eventlist and returns an observer type +func newObserver(vm *voicemeeter.Remote) *observer { + vm.EventAdd("ldirty") + return &observer{vm} +} + +// register registers this observer to receive updates +func (o observer) register() { o.vm.Register(o) } -func (o observer) Deregister() { +// deregister deregisters this observer to receive updates +func (o observer) deregister() { o.vm.Deregister(o) } +// OnUpdate satisfies the observer interface defined in publisher.go +// for each event type an action is triggered when the event occurs. func (o observer) OnUpdate(subject string) { if subject == "pdirty" { fmt.Println("pdirty!") @@ -43,22 +54,32 @@ func (o observer) OnUpdate(subject string) { } } +// main connects to Voiceemeter, registers observer for updates +// runs updates for 30 seconds and then deregisters observer. func main() { - vm, err := voicemeeter.NewRemote("potato", 0) - if err != nil { - log.Fatal(err) - } - - err = vm.Login() + vm, err := vmConnect() if err != nil { log.Fatal(err) } defer vm.Logout() - // enable level updates (disabled by default) - vm.EventAdd("ldirty") - o := observer{vm} - o.Register() + o := newObserver(vm) + o.register() time.Sleep(30 * time.Second) - o.Deregister() + o.deregister() +} + +// vmConnect connects to Voicemeeter potato and logs into the API +func vmConnect() (*voicemeeter.Remote, error) { + vm, err := voicemeeter.NewRemote("potato", 0) + if err != nil { + return nil, err + } + + err = vm.Login() + if err != nil { + return nil, err + } + + return vm, nil }