You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

47 lines
779 B

package watcher
import (
"time"
)
type Event[P any, T any] struct {
Publisher P
Payload T
UnixMilli int64
}
type Watcher[P any, T any] struct {
events chan *Event[P, T]
stop chan bool
handler func(event *Event[P, T])
}
func NewWatcher[P any, T any](buffer int, handler func(event *Event[P, T])) *Watcher[P, T] {
return &Watcher[P, T]{
events: make(chan *Event[P, T], buffer),
stop: make(chan bool),
handler: handler,
}
}
func (w *Watcher[P, T]) Run() {
for {
select {
case e := <-w.events:
w.handler(e)
case <-w.stop:
return
}
}
}
func (w *Watcher[P, T]) Stop() {
w.stop <- true
}
func (w *Watcher[P, T]) Add(publisher P, event T) {
e := &Event[P, T]{publisher, event, time.Now().UnixMilli()}
go func() {
w.events <- e
}()
}