-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscriptions.go
59 lines (49 loc) · 1.43 KB
/
subscriptions.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 hub
import "reflect"
// subscriptions keeps track of sinks associated with a particular type of event.
// this type follows copy-on-write semantics.
type subscriptions map[reflect.Type][]sink
// add makes a clone of this subscriptions instance with the given type mapped to a
// new sink.
func (s subscriptions) add(eventType reflect.Type, newSink sink) subscriptions {
clone := make(subscriptions)
for k, v := range s {
clone[k] = append([]sink{}, v...)
}
clone[eventType] = append(clone[eventType], newSink)
return clone
}
// remove makes a clone of this subscriptions instance with the given event type's sink
// removed. if the tuple of eventType and oldSink do not exist in this subscriptions,
// this instance is returned without modification.
func (s subscriptions) remove(eventType reflect.Type, oldSink sink) subscriptions {
existing, ok := s[eventType]
if !ok {
return s
}
var updated []sink
for _, candidate := range existing {
if candidate != oldSink {
updated = append(updated, candidate)
}
}
if len(existing) == len(updated) {
return s
}
clone := make(subscriptions)
for k, v := range s {
if k != eventType {
clone[k] = append([]sink{}, v...)
} else {
clone[k] = updated
}
}
return clone
}
// publish broadcasts the given event to the appropriate sinks
func (s subscriptions) publish(e interface{}) {
v := reflect.ValueOf(e)
for _, sink := range s[reflect.TypeOf(e)] {
sink.send(v)
}
}