Add NewWatcher function (#28)

This commit is contained in:
Rafal Jeczalik
2014-09-27 19:20:08 +02:00
parent 1304b59044
commit bd7fb37ac0
7 changed files with 32 additions and 14 deletions
+3 -3
View File
@@ -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)
}
+5 -1
View File
@@ -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
}
+6
View File
@@ -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.
//
+4 -8
View File
@@ -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.
+1 -1
View File
@@ -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) {
+5 -1
View File
@@ -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 {
+8
View File
@@ -0,0 +1,8 @@
// +build !linux,!fsnotify
package notify
// NewWatcher stub.
func newWatcher() Watcher {
panic("notify: no watcher implementation available on this platform")
}