diff --git a/examples/observer/observer.go b/examples/observer/observer.go new file mode 100644 index 0000000..fec0d38 --- /dev/null +++ b/examples/observer/observer.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "time" + + "github.com/onyx-and-iris/voicemeeter-api-go/voicemeeter" +) + +type observer struct{} + +func (o observer) OnUpdate(subject string) { + fmt.Println(subject) +} + +func main() { + vmRem := voicemeeter.GetRemote("banana") + vmRem.Login() + + o := observer{} + vmRem.Pooler.Publisher.Register(o) + + time.Sleep(10 * time.Second) + + vmRem.Logout() +} diff --git a/voicemeeter/publisher.go b/voicemeeter/publisher.go new file mode 100644 index 0000000..015714b --- /dev/null +++ b/voicemeeter/publisher.go @@ -0,0 +1,56 @@ +package voicemeeter + +type observer interface { + OnUpdate(subject string) +} + +type Publisher struct { + observerList []observer +} + +func (p *Publisher) Register(o observer) { + p.observerList = append(p.observerList, o) +} + +func (p *Publisher) Deregister(o observer) { + var indexToRemove int + + for i, observer := range p.observerList { + if observer == o { + indexToRemove = i + break + } + } + + p.observerList = append(p.observerList[:indexToRemove], p.observerList[indexToRemove+1:]...) +} + +func (p *Publisher) notify(subject string) { + for _, observer := range p.observerList { + observer.OnUpdate(subject) + } +} + +type Pooler struct { + run bool + Publisher +} + +func newPooler() *Pooler { + p := &Pooler{ + run: true, + } + go p.runner() + return p +} + +func (r *Pooler) runner() { + for r.run { + if pdirty() { + r.Publisher.notify("pdirty") + } + if mdirty() { + r.Publisher.notify("mdirty") + } + } +} diff --git a/voicemeeter/remote.go b/voicemeeter/remote.go index 5d32026..6df3e34 100644 --- a/voicemeeter/remote.go +++ b/voicemeeter/remote.go @@ -14,6 +14,8 @@ type remote struct { Button []button Command *command Vban *vban + + Pooler *Pooler } // String implements the stringer interface @@ -22,10 +24,12 @@ func (r *remote) String() string { } func (r *remote) Login() { + r.Pooler = newPooler() login(r.kind.name) } func (r *remote) Logout() { + r.Pooler.run = false logout() } @@ -117,15 +121,13 @@ func (b *genericBuilder) makeButton() remoteBuilder { func (b *genericBuilder) makeCommand() remoteBuilder { fmt.Println("building command") - _command := newCommand() - b.r.Command = _command + b.r.Command = newCommand() return b } func (b *genericBuilder) makeVban() remoteBuilder { fmt.Println("building vban") - _vban := newVban(b.k) - b.r.Vban = _vban + b.r.Vban = newVban(b.k) return b }