-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtypes.go
79 lines (62 loc) · 1.37 KB
/
types.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package event_emitter
import "sync"
type eventCallback func(msg any)
type topicField[T comparable, S Subscriber[T]] struct {
subers map[T]topicElement[S]
}
type topicElement[S any] struct {
suber S
cb eventCallback
}
type (
Subscriber[T comparable] interface {
// GetSubscriberID 获取订阅者唯一ID
// Get subscriber unique ID
GetSubscriberID() T
// GetMetadata 获取元数据
// Getting Metadata
GetMetadata() Metadata
}
Metadata interface {
Load(key string) (value any, exist bool)
Store(key string, value any)
Delete(key string)
Range(f func(key string, value any) bool)
}
)
type subscriber[T comparable] struct {
id T
md Metadata
}
func (c *subscriber[T]) GetMetadata() Metadata { return c.md }
func (c *subscriber[T]) GetSubscriberID() T { return c.id }
func newSmap() *smap { return &smap{data: make(map[string]any)} }
type smap struct {
sync.RWMutex
data map[string]any
}
func (c *smap) Load(key string) (value any, exist bool) {
c.RLock()
defer c.RUnlock()
value, exist = c.data[key]
return
}
func (c *smap) Delete(key string) {
c.Lock()
defer c.Unlock()
delete(c.data, key)
}
func (c *smap) Store(key string, value any) {
c.Lock()
defer c.Unlock()
c.data[key] = value
}
func (c *smap) Range(f func(key string, value any) bool) {
c.RLock()
defer c.RUnlock()
for k, v := range c.data {
if !f(k, v) {
return
}
}
}