register, deregister forwarding methods added to remote type.

observer example updated.

doc strings added to several files.
This commit is contained in:
onyx-and-iris 2022-06-26 01:36:32 +01:00
parent 25f39fcb6a
commit 83dccb483b
7 changed files with 53 additions and 15 deletions

View File

@ -23,14 +23,14 @@ func main() {
o2 := observer{2} o2 := observer{2}
o3 := observer{3} o3 := observer{3}
o4 := observer{4} o4 := observer{4}
vmRem.Pooler.Register(o) vmRem.Register(o)
vmRem.Pooler.Register(o2) vmRem.Register(o2)
vmRem.Pooler.Register(o3) vmRem.Register(o3)
vmRem.Pooler.Register(o4) vmRem.Register(o4)
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
vmRem.Pooler.Deregister(o2) vmRem.Deregister(o2)
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)

View File

@ -83,7 +83,7 @@ func newPhysicalBus(i int) t_bus {
return t_bus(&pb) return t_bus(&pb)
} }
// String implements the stringer interface // String implements the fmt.stringer interface
func (p *physicalBus) String() string { func (p *physicalBus) String() string {
return fmt.Sprintf("PhysicalBus%d", p.index) return fmt.Sprintf("PhysicalBus%d", p.index)
} }
@ -97,7 +97,7 @@ func newVirtualBus(i int) t_bus {
return t_bus(&vb) return t_bus(&vb)
} }
// String implements the stringer interface // String implements the fmt.stringer interface
func (v *virtualBus) String() string { func (v *virtualBus) String() string {
return fmt.Sprintf("VirtualBus%d", v.index) return fmt.Sprintf("VirtualBus%d", v.index)
} }

View File

@ -28,7 +28,7 @@ func (m *button) setter(v bool, mode int) {
setMacroStatus(m.index, value, mode) setMacroStatus(m.index, value, mode)
} }
// String implements the stringer interface // String implements the fmt.stringer interface
func (m *button) String() string { func (m *button) String() string {
return fmt.Sprintf("MacroButton%d", m.index) return fmt.Sprintf("MacroButton%d", m.index)
} }

View File

@ -13,16 +13,19 @@ type kind struct {
physIn, virtIn, physOut, virtOut, vbanIn, vbanOut int physIn, virtIn, physOut, virtOut, vbanIn, vbanOut int
} }
// numStrip returns the total number of strips for a kind
func (k *kind) numStrip() int { func (k *kind) numStrip() int {
n := k.physIn + k.virtIn n := k.physIn + k.virtIn
return n return n
} }
// numBus returns the total number of buses for a kind
func (k *kind) numBus() int { func (k *kind) numBus() int {
n := k.physOut + k.virtOut n := k.physOut + k.virtOut
return n return n
} }
// String implements the fmt.stringer interface
func (k *kind) String() string { func (k *kind) String() string {
return fmt.Sprintf("%s%s", strings.ToUpper(k.name[:1]), k.name[1:]) return fmt.Sprintf("%s%s", strings.ToUpper(k.name[:1]), k.name[1:])
} }

View File

@ -1,17 +1,21 @@
package voicemeeter package voicemeeter
// observer defines the interface any registered observers must satisfy
type observer interface { type observer interface {
OnUpdate(subject string) OnUpdate(subject string)
} }
// Publisher defines methods that support observers
type Publisher struct { type Publisher struct {
observerList []observer observerList []observer
} }
// Register adds an observer to observerList
func (p *Publisher) Register(o observer) { func (p *Publisher) Register(o observer) {
p.observerList = append(p.observerList, o) p.observerList = append(p.observerList, o)
} }
// Deregister removes an observer from observerList
func (p *Publisher) Deregister(o observer) { func (p *Publisher) Deregister(o observer) {
var indexToRemove int var indexToRemove int
@ -25,12 +29,15 @@ func (p *Publisher) Deregister(o observer) {
p.observerList = append(p.observerList[:indexToRemove], p.observerList[indexToRemove+1:]...) p.observerList = append(p.observerList[:indexToRemove], p.observerList[indexToRemove+1:]...)
} }
// notify updates observers of any changes
func (p *Publisher) notify(subject string) { func (p *Publisher) notify(subject string) {
for _, observer := range p.observerList { for _, observer := range p.observerList {
observer.OnUpdate(subject) observer.OnUpdate(subject)
} }
} }
// Pooler continuously polls the dirty paramters
// it is expected to be run in a goroutine
type Pooler struct { type Pooler struct {
run bool run bool
Publisher Publisher
@ -44,13 +51,13 @@ func newPooler() *Pooler {
return p return p
} }
func (r *Pooler) runner() { func (p *Pooler) runner() {
for r.run { for p.run {
if pdirty() { if pdirty() {
r.notify("pdirty") p.notify("pdirty")
} }
if mdirty() { if mdirty() {
r.notify("mdirty") p.notify("mdirty")
} }
} }
} }

View File

@ -18,16 +18,20 @@ type remote struct {
Pooler *Pooler Pooler *Pooler
} }
// String implements the stringer interface // String implements the fmt.stringer interface
func (r *remote) String() string { func (r *remote) String() string {
return fmt.Sprintf("Voicemeeter %s", r.kind) return fmt.Sprintf("Voicemeeter %s", r.kind)
} }
// Login logs into the API
// then it intializes the pooler
func (r *remote) Login() { func (r *remote) Login() {
r.Pooler = newPooler() r.Pooler = newPooler()
login(r.kind.name) login(r.kind.name)
} }
// Logout logs out of the API
// it also terminates the pooler
func (r *remote) Logout() { func (r *remote) Logout() {
r.Pooler.run = false r.Pooler.run = false
logout() logout()
@ -45,6 +49,16 @@ func (r *remote) SendText(script string) {
setParametersMulti(script) 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 { type remoteBuilder interface {
setKind() remoteBuilder setKind() remoteBuilder
makeStrip() remoteBuilder makeStrip() remoteBuilder
@ -55,18 +69,22 @@ type remoteBuilder interface {
Get() *remote Get() *remote
} }
// directory is responsible for directing the genericBuilder
type director struct { type director struct {
builder remoteBuilder builder remoteBuilder
} }
// SetBuilder sets the appropriate builder type for a kind
func (d *director) SetBuilder(b remoteBuilder) { func (d *director) SetBuilder(b remoteBuilder) {
d.builder = b d.builder = b
} }
// Construct defines the steps required for building a remote type
func (d *director) Construct() { func (d *director) Construct() {
d.builder.setKind().makeStrip().makeBus().makeButton().makeCommand().makeVban() d.builder.setKind().makeStrip().makeBus().makeButton().makeCommand().makeVban()
} }
// Get forwards the Get method to the builder
func (d *director) Get() *remote { func (d *director) Get() *remote {
return d.builder.Get() return d.builder.Get()
} }
@ -81,6 +99,8 @@ func (b *genericBuilder) setKind() remoteBuilder {
return b 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 { func (b *genericBuilder) makeStrip() remoteBuilder {
fmt.Println("building strip") fmt.Println("building strip")
_strip := make([]t_strip, b.k.numStrip()) _strip := make([]t_strip, b.k.numStrip())
@ -95,6 +115,8 @@ func (b *genericBuilder) makeStrip() remoteBuilder {
return b 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 { func (b *genericBuilder) makeBus() remoteBuilder {
fmt.Println("building bus") fmt.Println("building bus")
_bus := make([]t_bus, b.k.numBus()) _bus := make([]t_bus, b.k.numBus())
@ -109,6 +131,7 @@ func (b *genericBuilder) makeBus() remoteBuilder {
return b return b
} }
// makeButton makes a button slice and assigns it to remote.Button
func (b *genericBuilder) makeButton() remoteBuilder { func (b *genericBuilder) makeButton() remoteBuilder {
fmt.Println("building button") fmt.Println("building button")
_button := make([]button, 80) _button := make([]button, 80)
@ -119,18 +142,21 @@ func (b *genericBuilder) makeButton() remoteBuilder {
return b return b
} }
// makeCommand makes a Command type and assignss it to remote.Command
func (b *genericBuilder) makeCommand() remoteBuilder { func (b *genericBuilder) makeCommand() remoteBuilder {
fmt.Println("building command") fmt.Println("building command")
b.r.Command = newCommand() b.r.Command = newCommand()
return b return b
} }
// makeVban makes a Vban type and assignss it to remote.Vban
func (b *genericBuilder) makeVban() remoteBuilder { func (b *genericBuilder) makeVban() remoteBuilder {
fmt.Println("building vban") fmt.Println("building vban")
b.r.Vban = newVban(b.k) b.r.Vban = newVban(b.k)
return b return b
} }
// Get returns a fully constructed remote type for a kind
func (b *genericBuilder) Get() *remote { func (b *genericBuilder) Get() *remote {
return &b.r return &b.r
} }
@ -147,6 +173,8 @@ type potatoBuilder struct {
genericBuilder genericBuilder
} }
// GetRemote returns a remote type for a kind
// this is the interface entry point
func GetRemote(kindId string) *remote { func GetRemote(kindId string) *remote {
_kind, ok := kindMap[kindId] _kind, ok := kindMap[kindId]
if !ok { if !ok {

View File

@ -106,7 +106,7 @@ func newPhysicalStrip(i int) t_strip {
return t_strip(&ps) return t_strip(&ps)
} }
// implement stringer interface in fmt // implement fmt.stringer interface in fmt
func (p *physicalStrip) String() string { func (p *physicalStrip) String() string {
return fmt.Sprintf("PhysicalStrip%d", p.index) return fmt.Sprintf("PhysicalStrip%d", p.index)
} }
@ -161,7 +161,7 @@ func newVirtualStrip(i int) t_strip {
return t_strip(&vs) return t_strip(&vs)
} }
// implement stringer interface in fmt // implement fmt.stringer interface in fmt
func (v *virtualStrip) String() string { func (v *virtualStrip) String() string {
return fmt.Sprintf("VirtualStrip%d", v.index) return fmt.Sprintf("VirtualStrip%d", v.index)
} }