wip
This commit is contained in:
parent
f634a83bde
commit
7ba9c8cd6a
5
events/go.mod
Normal file
5
events/go.mod
Normal file
@ -0,0 +1,5 @@
|
||||
module gitea.codeblob.work/pk/gut/events
|
||||
|
||||
go 1.22.0
|
||||
|
||||
require github.com/google/uuid v1.6.0
|
2
events/go.sum
Normal file
2
events/go.sum
Normal file
@ -0,0 +1,2 @@
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
56
events/simple_emitter.go
Normal file
56
events/simple_emitter.go
Normal file
@ -0,0 +1,56 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type syncEventListener struct {
|
||||
id string
|
||||
cb func(event string, data any)
|
||||
}
|
||||
|
||||
type SyncEmitter struct {
|
||||
sync.Mutex
|
||||
listeners map[string][]syncEventListener
|
||||
}
|
||||
|
||||
func NewSyncEventListener(cb func(event string, data any)) *syncEventListener {
|
||||
return &syncEventListener{
|
||||
id: uuid.NewString(),
|
||||
cb: cb,
|
||||
}
|
||||
}
|
||||
|
||||
func NewSyncEmitter() *SyncEmitter {
|
||||
return &SyncEmitter{
|
||||
listeners: make(map[string][]syncEventListener),
|
||||
}
|
||||
}
|
||||
|
||||
func (e *SyncEmitter) Emit(event string, data any) {
|
||||
e.Lock()
|
||||
defer e.Unlock()
|
||||
targets := e.listeners[event]
|
||||
|
||||
for _, listener := range targets {
|
||||
listener.cb(event, data)
|
||||
}
|
||||
}
|
||||
|
||||
func (e *SyncEmitter) AddListener(event string, listener syncEventListener) {
|
||||
e.Lock()
|
||||
defer e.Unlock()
|
||||
e.listeners[event] = append(e.listeners[event], listener)
|
||||
}
|
||||
|
||||
func (e *SyncEmitter) RemoveListener(event string, listener syncEventListener) {
|
||||
e.Lock()
|
||||
defer e.Unlock()
|
||||
for i, l := range e.listeners[event] {
|
||||
if l.id == listener.id {
|
||||
e.listeners[event] = append(e.listeners[event][:i], e.listeners[event][i+1:]...)
|
||||
}
|
||||
}
|
||||
}
|
11
fs/file.go
Normal file
11
fs/file.go
Normal file
@ -0,0 +1,11 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
)
|
||||
|
||||
func PathExists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return !errors.Is(err, os.ErrNotExist)
|
||||
}
|
Loading…
Reference in New Issue
Block a user