forked from dunglas/mercure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscriber_list.go
59 lines (47 loc) · 1.2 KB
/
subscriber_list.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
package mercure
import (
"strings"
"github.com/kevburnsjr/skipfilter"
)
type SubscriberList struct {
skipfilter *skipfilter.SkipFilter
}
func NewSubscriberList(size int) *SubscriberList {
return &SubscriberList{
skipfilter: skipfilter.New(func(s interface{}, topic interface{}) bool {
p := strings.SplitN(topic.(string), "_", 2)
if len(p) < 2 {
return false
}
return s.(*Subscriber).MatchTopic(p[1], p[0] == "p")
}, size),
}
}
func (sc *SubscriberList) MatchAny(u *Update) (res []*Subscriber) {
scopedTopics := make([]interface{}, len(u.Topics))
for i, t := range u.Topics {
if u.Private {
scopedTopics[i] = "p_" + t
} else {
scopedTopics[i] = "_" + t
}
}
for _, m := range sc.skipfilter.MatchAny(scopedTopics...) {
res = append(res, m.(*Subscriber))
}
return
}
func (sc *SubscriberList) Walk(start uint64, callback func(s *Subscriber) bool) uint64 {
return sc.skipfilter.Walk(start, func(val interface{}) bool {
return callback(val.(*Subscriber))
})
}
func (sc *SubscriberList) Add(s *Subscriber) {
sc.skipfilter.Add(s)
}
func (sc *SubscriberList) Remove(s *Subscriber) {
sc.skipfilter.Remove(s)
}
func (sc *SubscriberList) Len() int {
return sc.skipfilter.Len()
}