From 83dccb483b5d4884afec3266a0b5469154fbe98d Mon Sep 17 00:00:00 2001 From: onyx-and-iris <75868496+onyx-and-iris@users.noreply.github.com> Date: Sun, 26 Jun 2022 01:36:32 +0100 Subject: [PATCH] register, deregister forwarding methods added to remote type. observer example updated. doc strings added to several files. --- examples/observer/observer.go | 10 +++++----- voicemeeter/bus.go | 4 ++-- voicemeeter/button.go | 2 +- voicemeeter/kinds.go | 3 +++ voicemeeter/publisher.go | 15 +++++++++++---- voicemeeter/remote.go | 30 +++++++++++++++++++++++++++++- voicemeeter/strip.go | 4 ++-- 7 files changed, 53 insertions(+), 15 deletions(-) diff --git a/examples/observer/observer.go b/examples/observer/observer.go index 737e124..887abf9 100644 --- a/examples/observer/observer.go +++ b/examples/observer/observer.go @@ -23,14 +23,14 @@ func main() { o2 := observer{2} o3 := observer{3} o4 := observer{4} - vmRem.Pooler.Register(o) - vmRem.Pooler.Register(o2) - vmRem.Pooler.Register(o3) - vmRem.Pooler.Register(o4) + vmRem.Register(o) + vmRem.Register(o2) + vmRem.Register(o3) + vmRem.Register(o4) time.Sleep(5 * time.Second) - vmRem.Pooler.Deregister(o2) + vmRem.Deregister(o2) time.Sleep(5 * time.Second) diff --git a/voicemeeter/bus.go b/voicemeeter/bus.go index f10982a..9f9b139 100644 --- a/voicemeeter/bus.go +++ b/voicemeeter/bus.go @@ -83,7 +83,7 @@ func newPhysicalBus(i int) t_bus { return t_bus(&pb) } -// String implements the stringer interface +// String implements the fmt.stringer interface func (p *physicalBus) String() string { return fmt.Sprintf("PhysicalBus%d", p.index) } @@ -97,7 +97,7 @@ func newVirtualBus(i int) t_bus { return t_bus(&vb) } -// String implements the stringer interface +// String implements the fmt.stringer interface func (v *virtualBus) String() string { return fmt.Sprintf("VirtualBus%d", v.index) } diff --git a/voicemeeter/button.go b/voicemeeter/button.go index 5b5ab23..ab1aa2e 100644 --- a/voicemeeter/button.go +++ b/voicemeeter/button.go @@ -28,7 +28,7 @@ func (m *button) setter(v bool, mode int) { setMacroStatus(m.index, value, mode) } -// String implements the stringer interface +// String implements the fmt.stringer interface func (m *button) String() string { return fmt.Sprintf("MacroButton%d", m.index) } diff --git a/voicemeeter/kinds.go b/voicemeeter/kinds.go index f1738ef..c320f1b 100644 --- a/voicemeeter/kinds.go +++ b/voicemeeter/kinds.go @@ -13,16 +13,19 @@ type kind struct { physIn, virtIn, physOut, virtOut, vbanIn, vbanOut int } +// numStrip returns the total number of strips for a kind func (k *kind) numStrip() int { n := k.physIn + k.virtIn return n } +// numBus returns the total number of buses for a kind func (k *kind) numBus() int { n := k.physOut + k.virtOut return n } +// String implements the fmt.stringer interface func (k *kind) String() string { return fmt.Sprintf("%s%s", strings.ToUpper(k.name[:1]), k.name[1:]) } diff --git a/voicemeeter/publisher.go b/voicemeeter/publisher.go index e4dce88..278c78d 100644 --- a/voicemeeter/publisher.go +++ b/voicemeeter/publisher.go @@ -1,17 +1,21 @@ package voicemeeter +// observer defines the interface any registered observers must satisfy type observer interface { OnUpdate(subject string) } +// Publisher defines methods that support observers type Publisher struct { observerList []observer } +// Register adds an observer to observerList func (p *Publisher) Register(o observer) { p.observerList = append(p.observerList, o) } +// Deregister removes an observer from observerList func (p *Publisher) Deregister(o observer) { var indexToRemove int @@ -25,12 +29,15 @@ func (p *Publisher) Deregister(o observer) { p.observerList = append(p.observerList[:indexToRemove], p.observerList[indexToRemove+1:]...) } +// notify updates observers of any changes func (p *Publisher) notify(subject string) { for _, observer := range p.observerList { observer.OnUpdate(subject) } } +// Pooler continuously polls the dirty paramters +// it is expected to be run in a goroutine type Pooler struct { run bool Publisher @@ -44,13 +51,13 @@ func newPooler() *Pooler { return p } -func (r *Pooler) runner() { - for r.run { +func (p *Pooler) runner() { + for p.run { if pdirty() { - r.notify("pdirty") + p.notify("pdirty") } if mdirty() { - r.notify("mdirty") + p.notify("mdirty") } } } diff --git a/voicemeeter/remote.go b/voicemeeter/remote.go index 6df3e34..b10de8c 100644 --- a/voicemeeter/remote.go +++ b/voicemeeter/remote.go @@ -18,16 +18,20 @@ type remote struct { Pooler *Pooler } -// String implements the stringer interface +// String implements the fmt.stringer interface func (r *remote) String() string { return fmt.Sprintf("Voicemeeter %s", r.kind) } +// Login logs into the API +// then it intializes the pooler func (r *remote) Login() { r.Pooler = newPooler() login(r.kind.name) } +// Logout logs out of the API +// it also terminates the pooler func (r *remote) Logout() { r.Pooler.run = false logout() @@ -45,6 +49,16 @@ func (r *remote) SendText(script string) { setParametersMulti(script) } +// Register forwards the register method to Pooler +func (r *remote) Register(o observer) { + r.Pooler.Register(o) +} + +// Register forwards the deregister method to Pooler +func (r *remote) Deregister(o observer) { + r.Pooler.Deregister(o) +} + type remoteBuilder interface { setKind() remoteBuilder makeStrip() remoteBuilder @@ -55,18 +69,22 @@ type remoteBuilder interface { Get() *remote } +// directory is responsible for directing the genericBuilder type director struct { builder remoteBuilder } +// SetBuilder sets the appropriate builder type for a kind func (d *director) SetBuilder(b remoteBuilder) { d.builder = b } +// Construct defines the steps required for building a remote type func (d *director) Construct() { d.builder.setKind().makeStrip().makeBus().makeButton().makeCommand().makeVban() } +// Get forwards the Get method to the builder func (d *director) Get() *remote { return d.builder.Get() } @@ -81,6 +99,8 @@ func (b *genericBuilder) setKind() remoteBuilder { return b } +// makeStrip makes a strip slice and assigns it to remote.Strip +// []t_strip comprises of both physical and virtual strip types func (b *genericBuilder) makeStrip() remoteBuilder { fmt.Println("building strip") _strip := make([]t_strip, b.k.numStrip()) @@ -95,6 +115,8 @@ func (b *genericBuilder) makeStrip() remoteBuilder { return b } +// makeBus makes a bus slice and assigns it to remote.Bus +// []t_bus comprises of both physical and virtual bus types func (b *genericBuilder) makeBus() remoteBuilder { fmt.Println("building bus") _bus := make([]t_bus, b.k.numBus()) @@ -109,6 +131,7 @@ func (b *genericBuilder) makeBus() remoteBuilder { return b } +// makeButton makes a button slice and assigns it to remote.Button func (b *genericBuilder) makeButton() remoteBuilder { fmt.Println("building button") _button := make([]button, 80) @@ -119,18 +142,21 @@ func (b *genericBuilder) makeButton() remoteBuilder { return b } +// makeCommand makes a Command type and assignss it to remote.Command func (b *genericBuilder) makeCommand() remoteBuilder { fmt.Println("building command") b.r.Command = newCommand() return b } +// makeVban makes a Vban type and assignss it to remote.Vban func (b *genericBuilder) makeVban() remoteBuilder { fmt.Println("building vban") b.r.Vban = newVban(b.k) return b } +// Get returns a fully constructed remote type for a kind func (b *genericBuilder) Get() *remote { return &b.r } @@ -147,6 +173,8 @@ type potatoBuilder struct { genericBuilder } +// GetRemote returns a remote type for a kind +// this is the interface entry point func GetRemote(kindId string) *remote { _kind, ok := kindMap[kindId] if !ok { diff --git a/voicemeeter/strip.go b/voicemeeter/strip.go index a859b87..b72d0c3 100644 --- a/voicemeeter/strip.go +++ b/voicemeeter/strip.go @@ -106,7 +106,7 @@ func newPhysicalStrip(i int) t_strip { return t_strip(&ps) } -// implement stringer interface in fmt +// implement fmt.stringer interface in fmt func (p *physicalStrip) String() string { return fmt.Sprintf("PhysicalStrip%d", p.index) } @@ -161,7 +161,7 @@ func newVirtualStrip(i int) t_strip { return t_strip(&vs) } -// implement stringer interface in fmt +// implement fmt.stringer interface in fmt func (v *virtualStrip) String() string { return fmt.Sprintf("VirtualStrip%d", v.index) }