neko/internal/webrtc/cursor/position.go

63 lines
1.3 KiB
Go
Raw Normal View History

package cursor
import (
"reflect"
"sync"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
2021-10-22 09:53:54 +13:00
func NewPosition() *PositionCtx {
return &PositionCtx{
2021-08-30 04:59:46 +12:00
logger: log.With().Str("module", "webrtc").Str("submodule", "cursor-position").Logger(),
listeners: map[uintptr]*func(x, y int){},
}
}
type PositionCtx struct {
2022-02-15 06:41:47 +13:00
logger zerolog.Logger
listeners map[uintptr]*func(x, y int)
listenersMu sync.Mutex
}
func (manager *PositionCtx) Shutdown() {
2021-08-30 04:59:46 +12:00
manager.logger.Info().Msg("shutdown")
2022-02-15 06:41:47 +13:00
manager.listenersMu.Lock()
for key := range manager.listeners {
delete(manager.listeners, key)
}
2022-02-15 06:41:47 +13:00
manager.listenersMu.Unlock()
}
2021-03-01 06:50:32 +13:00
func (manager *PositionCtx) Set(x, y int) {
2022-02-15 06:41:47 +13:00
manager.listenersMu.Lock()
defer manager.listenersMu.Unlock()
2021-03-01 06:50:32 +13:00
for _, emit := range manager.listeners {
(*emit)(x, y)
}
}
func (manager *PositionCtx) AddListener(listener *func(x, y int)) {
2022-02-15 06:41:47 +13:00
manager.listenersMu.Lock()
defer manager.listenersMu.Unlock()
if listener != nil {
ptr := reflect.ValueOf(listener).Pointer()
manager.listeners[ptr] = listener
}
}
func (manager *PositionCtx) RemoveListener(listener *func(x, y int)) {
2022-02-15 06:41:47 +13:00
manager.listenersMu.Lock()
defer manager.listenersMu.Unlock()
if listener != nil {
ptr := reflect.ValueOf(listener).Pointer()
delete(manager.listeners, ptr)
}
}