-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnamespace_events.go
61 lines (52 loc) · 1.38 KB
/
namespace_events.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package sio
import (
"fmt"
"reflect"
)
func (n *Namespace) OnEvent(eventName string, handler any) {
if IsEventReservedForNsp(eventName) {
panic(fmt.Errorf("sio: OnEvent: attempted to register a reserved event: `%s`", eventName))
}
h, err := newEventHandler(handler)
if err != nil {
panic(err)
}
n.eventHandlers.on(eventName, h)
}
func (n *Namespace) OnceEvent(eventName string, handler any) {
if IsEventReservedForNsp(eventName) {
panic(fmt.Errorf("sio: OnceEvent: attempted to register a reserved event: `%s`", eventName))
}
h, err := newEventHandler(handler)
if err != nil {
panic(err)
}
n.eventHandlers.once(eventName, h)
}
func (n *Namespace) OffEvent(eventName string, handler ...any) {
values := make([]reflect.Value, len(handler))
for i := range values {
values[i] = reflect.ValueOf(handler[i])
}
n.eventHandlers.off(eventName, values...)
}
func (n *Namespace) OffAll() {
n.eventHandlers.offAll()
n.connectionHandlers.offAll()
}
type (
NamespaceConnectionFunc func(socket ServerSocket)
)
func (n *Namespace) OnConnection(f NamespaceConnectionFunc) {
n.connectionHandlers.on(&f)
}
func (n *Namespace) OnceConnection(f NamespaceConnectionFunc) {
n.connectionHandlers.once(&f)
}
func (n *Namespace) OffConnection(_f ...NamespaceConnectionFunc) {
f := make([]*NamespaceConnectionFunc, len(_f))
for i := range f {
f[i] = &_f[i]
}
n.connectionHandlers.off(f...)
}