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.
5 // +build darwin,!kqueue
12 "github.com/rjeczalik/notify"
15 // This example shows how to use FSEvents-specifc event values.
16 func ExampleWatch_darwin() {
17 // Make the channel buffered to ensure no event is dropped. Notify will drop
18 // an event if the receiver is not able to keep up the sending pace.
19 c := make(chan notify.EventInfo, 1)
21 // Set up a watchpoint listening for FSEvents-specific events within a
22 // current working directory. Dispatch each FSEventsChangeOwner and FSEventsMount
23 // events separately to c.
24 if err := notify.Watch(".", c, notify.FSEventsChangeOwner, notify.FSEventsMount); err != nil {
29 // Block until an event is received.
30 switch ei := <-c; ei.Event() {
31 case notify.FSEventsChangeOwner:
32 log.Println("The owner of", ei.Path(), "has changed.")
33 case notify.FSEventsMount:
34 log.Println("The path", ei.Path(), "has been mounted.")
38 // This example shows how to work with EventInfo's underlying FSEvent struct.
39 // Investigating notify.(*FSEvent).Flags field we are able to say whether
40 // the event's path is a file or a directory and many more.
41 func ExampleWatch_darwinDirFileSymlink() {
42 var must = func(err error) {
47 var stop = func(c ...chan<- notify.EventInfo) {
53 // Make the channels buffered to ensure no event is dropped. Notify will drop
54 // an event if the receiver is not able to keep up the sending pace.
55 dir := make(chan notify.EventInfo, 1)
56 file := make(chan notify.EventInfo, 1)
57 symlink := make(chan notify.EventInfo, 1)
58 all := make(chan notify.EventInfo, 1)
60 // Set up a single watchpoint listening for FSEvents-specific events on
61 // multiple user-provided channels.
62 must(notify.Watch(".", dir, notify.FSEventsIsDir))
63 must(notify.Watch(".", file, notify.FSEventsIsFile))
64 must(notify.Watch(".", symlink, notify.FSEventsIsSymlink))
65 must(notify.Watch(".", all, notify.All))
66 defer stop(dir, file, symlink, all)
68 // Block until an event is received.
71 log.Println("The directory", ei.Path(), "has changed")
73 log.Println("The file", ei.Path(), "has changed")
75 log.Println("The symlink", ei.Path(), "has changed")
79 // Investigate underlying *notify.FSEvent struct to access more
80 // information about the event.
81 switch flags := ei.Sys().(*notify.FSEvent).Flags; {
82 case flags¬ify.FSEventsIsFile != 0:
84 case flags¬ify.FSEventsIsDir != 0:
86 case flags¬ify.FSEventsIsSymlink != 0:
90 log.Printf("The %s under path %s has been %sd\n", kind, ei.Path(), ei.Event())
94 // FSEvents may report multiple filesystem actions with one, coalesced event.
95 // Notify unscoalesces such event and dispatches series of single events
98 // This example shows how to coalesce events by investigating notify.(*FSEvent).ID
99 // field, for the science.
100 func ExampleWatch_darwinCoalesce() {
101 // Make the channels buffered to ensure no event is dropped. Notify will drop
102 // an event if the receiver is not able to keep up the sending pace.
103 c := make(chan notify.EventInfo, 4)
105 // Set up a watchpoint listetning for events within current working directory.
106 // Dispatch all platform-independent separately to c.
107 if err := notify.Watch(".", c, notify.All); err != nil {
113 var coalesced []notify.EventInfo
116 switch n := ei.Sys().(*notify.FSEvent).ID; {
119 coalesced = []notify.EventInfo{ei}
121 coalesced = append(coalesced, ei)
123 log.Printf("FSEvents reported a filesystem action with the following"+
124 " coalesced events %v groupped by %d ID\n", coalesced, id)