https://github.com/karpeleslab/emitter
https://github.com/karpeleslab/emitter
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/karpeleslab/emitter
- Owner: KarpelesLab
- License: mit
- Created: 2024-09-02T12:42:49.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2024-11-12T15:38:03.000Z (over 1 year ago)
- Last Synced: 2025-07-01T00:08:52.622Z (12 months ago)
- Language: Go
- Size: 41 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://godoc.org/github.com/KarpelesLab/emitter)
# emitter
Simple emitter lib.
## Example
```go
h := emitter.New()
go func(ch <-chan *emitter.Event) {
defer h.Off("event", ch)
for ev := range ch {
// handle event
intVal, err := emitter.Arg[int](ev, 0)
// intVal is 42
}
}(h.On("event"))
h.Emit("event", 42)
```
## Global Hub
For some cases it might be useful to have global events that can be handled by multiple packages. This can be done with the global hub.
```go
go func(ch <-chan *emitter.Event) {
for ev := range ch {
// ...
}
})(emitter.Global.On("event"))
emitter.Global.Emit("event", 42)
```
# Trigger
The trigger object allows waking multiple threads at the same time using channels rather than [sync.Cond](https://pkg.go.dev/sync#Cond). This can be useful to wake many threads to specific events while still using other event sources such as timers.
Triggers by default have a queue size of 1, meaning that a call to Push() can be queued and delivered later if the receiving thread is busy. Cap can be set to other values including zero (do not queue) or larger values (queue a number of calls).
Unlike events `Emit()`, a trigger's `Push()` method returns instantly and is non blocking, with minimal resource usage.
## Example
```go
trig := emitter.Global.Trigger("test")
go func() {
t := time.NewTicker(30*time.Second)
defer t.Stop()
l := trig.Listen()
defer l.Release()
for {
select {
case <-t.C:
// do something every 30 secs
case _, closed := <-l.C:
if closed {
// exit loop if channel is closed, would be triggered by trig.Close()
// this can be ignored if trig.Closed() isn't called
return
}
// do something on trigger called
}
}
}
trig.Push() // push event
```