diff --git a/notify.go b/notify.go index 5b5722e..a469fc9 100644 --- a/notify.go +++ b/notify.go @@ -1,13 +1,13 @@ package notify -var notifier *Runtime +var r = NewRuntime() // Watch TODO func Watch(name string, c chan<- EventInfo, events ...Event) { - notifier.Watch(name, c, events...) + r.Watch(name, c, events...) } // Stop TODO func Stop(c chan<- EventInfo) { - notifier.Stop(c) + r.Stop(c) } diff --git a/runtime.go b/runtime.go index cd395d2..f032b17 100644 --- a/runtime.go +++ b/runtime.go @@ -19,7 +19,8 @@ type Runtime struct { } // NewRuntime TODO -func NewRuntime(w Watcher) *Runtime { +func NewRuntime() *Runtime { + w, ch := NewWatcher(), make(chan EventInfo) r := &Runtime{ Tree: make(map[string]interface{}), } @@ -32,6 +33,9 @@ func NewRuntime(w Watcher) *Runtime { Runtime: r, } } + // TODO(rjeczalik): Uncomment after #5. + // r.Watcher.Fanin(ch) + go r.loop() return r } diff --git a/watcher.go b/watcher.go index a74496f..dca3571 100644 --- a/watcher.go +++ b/watcher.go @@ -1,5 +1,11 @@ package notify +// NewWatcher TODO +func NewWatcher() Watcher { + // newWatcher is implemented per supported platform + return newWatcher() +} + // Watcher is a temporary interface for wrapping inotify, ReadDirChangesW, // FSEvents, kqueue, poller and fsnotify implementations. // diff --git a/watcher_fsnotify.go b/watcher_fsnotify.go index 1d74318..e2884ac 100644 --- a/watcher_fsnotify.go +++ b/watcher_fsnotify.go @@ -9,10 +9,6 @@ import ( fsnotifyv1 "gopkg.in/fsnotify.v1" ) -func init() { - notifier = NewRuntime(newFsnotify()) -} - type event struct { name string ev Event @@ -44,15 +40,15 @@ type fsnotify struct { w *fsnotifyv1.Watcher } -// NewFsnotify creates new non-recursive watcher backed by fsnotifyv1 package. -func newFsnotify() (fs *fsnotify) { +// NewWatcher creates new non-recursive watcher backed by fsnotifyv1 package. +func newWatcher() Watcher { w, err := fsnotifyv1.NewWatcher() if err != nil { panic(err) } - fs = &fsnotify{w: w} + fs := &fsnotify{w: w} runtime.SetFinalizer(fs, func(fs *fsnotify) { fs.w.Close() }) - return + return fs } // Watch implements notify.Watcher interface. diff --git a/watcher_fsnotify_test.go b/watcher_fsnotify_test.go index de55c40..4f9de5a 100644 --- a/watcher_fsnotify_test.go +++ b/watcher_fsnotify_test.go @@ -32,7 +32,7 @@ func TestFsnotify(t *testing.T) { EI("file", Create), EI("dir/", Create), } - test(t, newFsnotify(), All, ei, time.Second) + test(t, newWatcher(), All, ei, time.Second) } func TestIssue16(t *testing.T) { diff --git a/watcher_inotify.go b/watcher_inotify.go index d1cf969..914046b 100644 --- a/watcher_inotify.go +++ b/watcher_inotify.go @@ -45,7 +45,6 @@ type watched struct { // TODO(ppknap) : doc. func init() { handlers = newInotify() - notifier = NewRuntime(handlers) go loop() } @@ -64,6 +63,11 @@ func newInotify() *handlersType { return h } +// NewWatcher creates new non-recursive watcher backed by inotify. +func newWatcher() Watcher { + return handlers +} + // TODO(ppknap) : doc. func loop() { for { diff --git a/watcher_stub.go b/watcher_stub.go new file mode 100644 index 0000000..0584c75 --- /dev/null +++ b/watcher_stub.go @@ -0,0 +1,8 @@ +// +build !linux,!fsnotify + +package notify + +// NewWatcher stub. +func newWatcher() Watcher { + panic("notify: no watcher implementation available on this platform") +}