1
0
mirror of https://github.com/AvengeMedia/DankMaterialShell.git synced 2026-01-24 21:42:51 -05:00

core: refactor to use a generic-compatible syncmap

This commit is contained in:
bbedward
2025-11-15 19:44:47 -05:00
parent 4cb652abd9
commit 67557555f2
36 changed files with 936 additions and 543 deletions

View File

@@ -9,6 +9,7 @@ import (
"time"
"github.com/AvengeMedia/DankMaterialShell/core/internal/log"
"github.com/AvengeMedia/DankMaterialShell/core/pkg/syncmap"
"github.com/fsnotify/fsnotify"
evdev "github.com/holoplot/go-evdev"
)
@@ -35,7 +36,7 @@ type Manager struct {
monitoredPaths map[string]bool
state State
stateMutex sync.RWMutex
subscribers sync.Map
subscribers syncmap.Map[string, chan State]
closeChan chan struct{}
closeOnce sync.Once
watcher *fsnotify.Watcher
@@ -338,13 +339,12 @@ func (m *Manager) Subscribe(id string) chan State {
func (m *Manager) Unsubscribe(id string) {
if val, ok := m.subscribers.LoadAndDelete(id); ok {
close(val.(chan State))
close(val)
}
}
func (m *Manager) notifySubscribers(state State) {
m.subscribers.Range(func(key, value interface{}) bool {
ch := value.(chan State)
m.subscribers.Range(func(key string, ch chan State) bool {
select {
case ch <- state:
default:
@@ -372,8 +372,7 @@ func (m *Manager) Close() {
}
m.devicesMutex.Unlock()
m.subscribers.Range(func(key, value interface{}) bool {
ch := value.(chan State)
m.subscribers.Range(func(key string, ch chan State) bool {
close(ch)
m.subscribers.Delete(key)
return true

View File

@@ -72,7 +72,7 @@ func TestManager_Subscribe(t *testing.T) {
ch := m.Subscribe("test-client")
assert.NotNil(t, ch)
count := 0
m.subscribers.Range(func(key, value interface{}) bool {
m.subscribers.Range(func(key string, ch chan State) bool {
count++
return true
})
@@ -92,7 +92,7 @@ func TestManager_Unsubscribe(t *testing.T) {
ch := m.Subscribe("test-client")
count := 0
m.subscribers.Range(func(key, value interface{}) bool {
m.subscribers.Range(func(key string, ch chan State) bool {
count++
return true
})
@@ -100,7 +100,7 @@ func TestManager_Unsubscribe(t *testing.T) {
m.Unsubscribe("test-client")
count = 0
m.subscribers.Range(func(key, value interface{}) bool {
m.subscribers.Range(func(key string, ch chan State) bool {
count++
return true
})
@@ -180,7 +180,7 @@ func TestManager_Close(t *testing.T) {
}
count := 0
m.subscribers.Range(func(key, value interface{}) bool {
m.subscribers.Range(func(key string, ch chan State) bool {
count++
return true
})