2021-03-01 06:37:35 +13:00
|
|
|
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 {
|
2021-03-01 06:37:35 +13:00
|
|
|
return &PositionCtx{
|
2021-08-30 04:59:46 +12:00
|
|
|
logger: log.With().Str("module", "webrtc").Str("submodule", "cursor-position").Logger(),
|
2021-03-01 06:37:35 +13:00
|
|
|
listeners: map[uintptr]*func(x, y int){},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type PositionCtx struct {
|
|
|
|
logger zerolog.Logger
|
|
|
|
emitMu sync.Mutex
|
|
|
|
listeners map[uintptr]*func(x, y int)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *PositionCtx) Shutdown() {
|
2021-08-30 04:59:46 +12:00
|
|
|
manager.logger.Info().Msg("shutdown")
|
2021-03-01 06:37:35 +13:00
|
|
|
|
|
|
|
manager.emitMu.Lock()
|
|
|
|
for key := range manager.listeners {
|
|
|
|
delete(manager.listeners, key)
|
|
|
|
}
|
|
|
|
manager.emitMu.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-03-01 06:50:32 +13:00
|
|
|
func (manager *PositionCtx) Set(x, y int) {
|
|
|
|
for _, emit := range manager.listeners {
|
|
|
|
(*emit)(x, y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 06:37:35 +13:00
|
|
|
func (manager *PositionCtx) AddListener(listener *func(x, y int)) {
|
|
|
|
manager.emitMu.Lock()
|
|
|
|
defer manager.emitMu.Unlock()
|
|
|
|
|
|
|
|
if listener != nil {
|
|
|
|
ptr := reflect.ValueOf(listener).Pointer()
|
|
|
|
manager.listeners[ptr] = listener
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *PositionCtx) RemoveListener(listener *func(x, y int)) {
|
|
|
|
manager.emitMu.Lock()
|
|
|
|
defer manager.emitMu.Unlock()
|
|
|
|
|
|
|
|
if listener != nil {
|
|
|
|
ptr := reflect.ValueOf(listener).Pointer()
|
|
|
|
delete(manager.listeners, ptr)
|
|
|
|
}
|
|
|
|
}
|