-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwatch.go
84 lines (70 loc) · 2.2 KB
/
watch.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
package whatnot
import (
"time"
"github.com/databeast/whatnot/access"
)
type changeType int
const (
ChangeUnknown changeType = iota + 1
ChangeLocked
ChangeUnlocked
ChangeAdded
ChangeEdited
ChangeDeleted
ChangePruned
ChangeReleased
)
// elementChange is a notification channel structure
// for communicating changes to individual elements to subscribed watchers
type elementChange struct {
id uint64
elem *PathElement
change changeType
actor access.Role
}
// ElementWatchSubscription is a contract to be notified
// of all events on a given Path Element, and optionally
// all its child elements
type ElementWatchSubscription struct {
logsupport
onElement *PathElement // the Path Element this is a subscription to
isRecursive bool // is this subscription for this element alone, or its children as well?
events chan WatchEvent
}
type WatchEvents chan WatchEvent
// WatchEvent describes an event on a Path Element or optionally
// any of its children, obtained and consumed via an ElementWatchSubscription
type WatchEvent struct {
id uint64
elem *PathElement
TS time.Time
Change changeType
Actor access.Role
Note string
}
func (e WatchEvent) OnElement() *PathElement {
return e.elem
}
// SubscribeToEvents generates a Watch Subscription that produces a single channel
// of notification events on the accompanying Path Element, and optionally all of its
// child path elements
func (p *PathElement) SubscribeToEvents(prefix bool) *ElementWatchSubscription {
sub := &ElementWatchSubscription{
onElement: p,
events: make(chan WatchEvent),
isRecursive: prefix,
}
p.subscriberNotify.Register(sub.events, prefix) // this is the part that will allow us to receive channel messages
return sub
}
// UnSubscribeFromEvents will unregister the notification channel
// and then nil out the watch subscription that is passed to it.
// preventing any further reception of events
func (p *PathElement) UnSubscribeFromEvents(sub *ElementWatchSubscription) {
p.subscriberNotify.Unregister(sub.events)
sub = nil
}
// Events returns a channel of subscriberNotify occurring to this Key (or its subKeys
func (m *ElementWatchSubscription) Events() <-chan WatchEvent {
return m.events
}