-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemitter.go
47 lines (40 loc) · 1.11 KB
/
emitter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package eio
import (
"github.com/olebedev/emitter"
)
var _ Emitter = (*wrapEmitter)(nil)
const (
TopicOpen = "open"
TopicClose = "close"
TopicConnection = "connection"
TopicDisconnect = "disconnect"
TopicDisconnecting = "disconnecting"
TopicHeartbeat = "heartbeat"
TopicData = "data"
TopicMessage = "message"
TopicPacket = "packet"
TopicError = "error"
)
type Emitter interface {
Use(pattern string, middlewares ...func(event *emitter.Event))
On(topic string, middlewares ...func(event *emitter.Event)) <-chan emitter.Event
Once(topic string, middlewares ...func(event *emitter.Event)) <-chan emitter.Event
Off(topic string, channels ...<-chan emitter.Event)
Listeners(topic string) []<-chan emitter.Event
Topics() []string
Emit(topic string, args ...any) chan struct{}
Fire(topic string, args ...any)
}
type wrapEmitter struct {
*emitter.Emitter
}
func NewEmitter() *wrapEmitter {
e := emitter.New(1)
e.Use("*", emitter.Sync)
return &wrapEmitter{
Emitter: e,
}
}
func (e *wrapEmitter) Fire(topic string, args ...any) {
go e.Emit(topic, args...)
}