This commit is contained in:
trading_peter 2024-03-05 18:46:27 +01:00
parent f634a83bde
commit 7ba9c8cd6a
5 changed files with 77 additions and 0 deletions

5
events/go.mod Normal file
View 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
View 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
View 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
View 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)
}

3
fs/go.mod Normal file
View File

@ -0,0 +1,3 @@
module gitea.codeblob.work/pk/gut/fs
go 1.22.0