watcher.go 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. package store
  2. type watcher struct {
  3. eventChan chan *Event
  4. recursive bool
  5. sinceIndex uint64
  6. }
  7. // notify function notifies the watcher. If the watcher interests in the given path,
  8. // the function will return true.
  9. func (w *watcher) notify(e *Event, originalPath bool, deleted bool) bool {
  10. // watcher is interested the path in three cases and under one condition
  11. // the condition is that the event happens after the watcher's sinceIndex
  12. // 1. the path at which the event happens is the path the watcher is watching at.
  13. // For example if the watcher is watching at "/foo" and the event happens at "/foo",
  14. // the watcher must be interested in that event.
  15. // 2. the watcher is a recursive watcher, it interests in the event happens after
  16. // its watching path. For example if watcher A watches at "/foo" and it is a recursive
  17. // one, it will interest in the event happens at "/foo/bar".
  18. // 3. when we delete a directory, we need to force notify all the watchers who watches
  19. // at the file we need to delete.
  20. // For example a watcher is watching at "/foo/bar". And we deletes "/foo". The watcher
  21. // should get notified even if "/foo" is not the path it is watching.
  22. if (w.recursive || originalPath || deleted) && e.Index >= w.sinceIndex {
  23. w.eventChan <- e
  24. return true
  25. }
  26. return false
  27. }