-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtopic.go
90 lines (80 loc) · 1.72 KB
/
topic.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
80
81
82
83
84
85
86
87
88
89
90
package swirl
import (
"github.com/ischenkx/swirl/internal/pubsub/message"
"time"
)
type localTopic struct {
app *App
id string
}
func (t localTopic) Events() TopicEvents {
return t.app.events.forTopic(t.id)
}
func (t localTopic) Emit(name string, options ...interface{}) {
var args []interface{}
var metaInfo interface{}
var timeStamp int64
for _, opt := range options {
switch o := opt.(type) {
case MetaInfo:
metaInfo = o
case TimeStamp:
timeStamp = time.Time(o).UnixNano()
case Args:
args = o
}
}
data, err := t.app.emitter.EncodeRawData(name, args)
if err != nil {
t.app.events.callError(EncodingError{
Reason: err,
EventOptions: EventOptions{
Name: name,
Args: args,
TimeStamp: timeStamp,
MetaInfo: metaInfo,
},
})
return
}
t.app.pubsub.SendToTopic(t.id, message.New(data))
t.app.events.callEmit(EmitOptions{
Topics: []string{t.id},
EventOptions: EventOptions{
Name: name,
Args: args,
TimeStamp: timeStamp,
MetaInfo: metaInfo,
},
})
}
func (t localTopic) Users() IDList {
return variadicIdList{
count: func(list IDList) int {
return t.app.pubsub.Users().CountTopicUsers(t.id)
},
array: func(list IDList) []string {
ids := make([]string, 0, list.Count())
t.app.pubsub.Users().IterTopicUsers(t.id, func(id string) {
ids = append(ids, id)
})
return ids
},
}
}
func (t localTopic) Clients() IDList {
return variadicIdList{
count: func(list IDList) int {
return t.app.pubsub.CountTopicSubscribers(t.id)
},
array: func(list IDList) []string {
return t.app.pubsub.TopicSubscribers(t.id)
},
}
}
type Topic interface {
Emit(string, ...interface{})
Users() IDList
Clients() IDList
Events() TopicEvents
}