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.
12 "github.com/rjeczalik/notify"
15 // This is a basic example showing how to work with notify.Watch function.
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 on events within current working directory.
22 // Dispatch each create and remove events separately to c.
23 if err := notify.Watch(".", c, notify.Create, notify.Remove); err != nil {
28 // Block until an event is received.
30 log.Println("Got event:", ei)
33 // This example shows how to set up a recursive watchpoint.
34 func ExampleWatch_recursive() {
35 // Make the channel buffered to ensure no event is dropped. Notify will drop
36 // an event if the receiver is not able to keep up the sending pace.
37 c := make(chan notify.EventInfo, 1)
39 // Set up a watchpoint listening for events within a directory tree rooted
40 // at current working directory. Dispatch remove events to c.
41 if err := notify.Watch("./...", c, notify.Remove); err != nil {
46 // Block until an event is received.
48 log.Println("Got event:", ei)
51 // This example shows why it is important to not create leaks by stoping
52 // a channel when it's no longer being used.
54 waitfor := func(path string, e notify.Event, timeout time.Duration) bool {
55 dir, file := filepath.Split(path)
56 c := make(chan notify.EventInfo, 1)
58 if err := notify.Watch(dir, c, e); err != nil {
61 // Clean up watchpoint associated with c. If Stop was not called upon
62 // return the channel would be leaked as notify holds the only reference
63 // to it and does not release it on its own.
66 t := time.After(timeout)
71 if filepath.Base(ei.Path()) == file {
80 if waitfor("index.lock", notify.Create, 5*time.Second) {
81 log.Println("The git repository was locked")
84 if waitfor("index.lock", notify.Remove, 5*time.Second) {
85 log.Println("The git repository was unlocked")