-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsubscription.go
More file actions
68 lines (54 loc) · 1.67 KB
/
subscription.go
File metadata and controls
68 lines (54 loc) · 1.67 KB
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
package subscribe
import (
"strings"
"time"
)
/****************************
* Subscription Methods *
****************************/
// Subscribe adds an event subscription to a subscriber.
// Returns an error only if the event subscription already exists.
func (s *Subscriber) Subscribe(event string) error {
return s.Events.New(event, &Rules{Pause: time.Now()})
}
// GetSubscribers returns a list of valid event subscribers.
// This is the main method that should be triggered when an event occurs.
// Call this method when your event fires, collect the subscribers and send
// them notifications in your app. Subscribers can be people. Or functions.
func (s *Subscribe) GetSubscribers(eventName string) []*Subscriber {
s.mu.RLock()
defer s.mu.RUnlock()
subscribers := make([]*Subscriber, 0, len(s.Subscribers))
for _, sub := range s.Subscribers {
if !sub.Ignored && s.checkAPILocked(sub.API) && !sub.Events.IsPaused(eventName) {
subscribers = append(subscribers, sub)
}
}
return subscribers
}
// checkAPI just looks for a string in a slice of strings with a twist.
func (s *Subscribe) checkAPI(api string) bool {
s.mu.RLock()
defer s.mu.RUnlock()
return s.checkAPILocked(api)
}
func (s *Subscribe) checkAPILocked(api string) bool {
if len(s.EnableAPIs) < 1 {
return true
}
for _, a := range s.EnableAPIs {
if a == api || strings.HasPrefix(api, a) || a == "all" || a == "any" {
return true
}
}
return false
}
// EventRemove obliterates an event and all subscriptions for it.
func (s *Subscribe) EventRemove(event string) {
s.mu.RLock()
defer s.mu.RUnlock()
s.Events.Remove(event)
for _, sub := range s.Subscribers {
sub.Events.Remove(event)
}
}