add support for observers.

init/end pooler on login, logout
This commit is contained in:
onyx-and-iris 2022-06-25 20:11:15 +01:00
parent d3289dbb80
commit d7be997fae
3 changed files with 88 additions and 4 deletions

View File

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

56
voicemeeter/publisher.go Normal file
View File

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

View File

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