1 // Copyright (c) 2014-2015 The Notify Authors. All rights reserved.
2 // Use of this source code is governed by the MIT license that can be
3 // found in the LICENSE file.
7 // EventDiff describes a change to an event set - EventDiff[0] is an old state,
8 // while EventDiff[1] is a new state. If event set has not changed (old == new),
9 // functions typically return the None value.
10 type eventDiff [2]Event
12 func (diff eventDiff) Event() Event {
13 return diff[1] &^ diff[0]
18 // The nil key holds total event set - logical sum for all registered events.
19 // It speeds up computing EventDiff for Add method.
21 // The rec key holds an event set for a watchpoints created by RecursiveWatch
22 // for a Watcher implementation which is not natively recursive.
23 type watchpoint map[chan<- EventInfo]Event
25 // None is an empty event diff, think null object.
28 // rec is just a placeholder
29 var rec = func() (ch chan<- EventInfo) {
30 ch = make(chan<- EventInfo)
35 func (wp watchpoint) dryAdd(ch chan<- EventInfo, e Event) eventDiff {
36 if e &^= internal; wp[ch]&e == e {
39 total := wp[ch] &^ internal
40 return eventDiff{total, total | e}
43 // Add assumes neither c nor e are nil or zero values.
44 func (wp watchpoint) Add(c chan<- EventInfo, e Event) (diff eventDiff) {
48 wp[nil] = diff[1] &^ omit
49 // Strip diff from internal events.
52 if diff[0] == diff[1] {
58 func (wp watchpoint) Del(c chan<- EventInfo, e Event) (diff eventDiff) {
66 // Recalculate total event set.
67 for _, e := range wp {
70 wp[nil] = diff[1] &^ omit
72 // Strip diff from internal events.
75 if diff[0] == diff[1] {
81 func (wp watchpoint) Dispatch(ei EventInfo, extra Event) {
82 e := eventmask(ei, extra)
83 if !matches(wp[nil], e) {
86 for ch, eset := range wp {
87 if ch != nil && matches(eset, e) {
90 default: // Drop event if receiver is too slow
91 dbgprintf("dropped %s on %q: receiver too slow", ei.Event(), ei.Path())
97 func (wp watchpoint) Total() Event {
98 return wp[nil] &^ internal
101 func (wp watchpoint) IsRecursive() bool {
102 return wp[nil]&recursive != 0