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}
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)

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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:])
}

View File

@ -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")
}
}
}

View File

@ -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 {

View File

@ -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)
}