Only allow OnChange() to be called once

This commit is contained in:
Elara 2021-11-24 17:21:12 -08:00
parent e12ccb2ff5
commit 9966880bc8

View File

@ -2,6 +2,7 @@ package player
import ( import (
"strings" "strings"
"sync"
"github.com/godbus/dbus/v5" "github.com/godbus/dbus/v5"
"go.arsenm.dev/infinitime/internal/utils" "go.arsenm.dev/infinitime/internal/utils"
@ -10,6 +11,7 @@ import (
var ( var (
method, monitor *dbus.Conn method, monitor *dbus.Conn
monitorCh chan *dbus.Message monitorCh chan *dbus.Message
onChangeOnce sync.Once
) )
// Init makes required connections to DBis and // Init makes required connections to DBis and
@ -130,7 +132,7 @@ func (ct ChangeType) String() string {
// OnChange runs cb when a value changes // OnChange runs cb when a value changes
func OnChange(cb func(ChangeType, string)) { func OnChange(cb func(ChangeType, string)) {
go func() { go onChangeOnce.Do(func() {
// For every message on channel // For every message on channel
for msg := range monitorCh { for msg := range monitorCh {
// Parse PropertiesChanged // Parse PropertiesChanged
@ -175,7 +177,7 @@ func OnChange(cb func(ChangeType, string)) {
} }
} }
} }
}() })
} }
// getPlayerNames gets all DBus MPRIS player bus names // getPlayerNames gets all DBus MPRIS player bus names