summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-03-27 17:44:20 +0700
committerShulhan <ms@kilabit.info>2022-03-27 17:44:20 +0700
commit0c288a5880fbc0fceb792dcfd0747d95163a90dc (patch)
treef8d30b2f1d9b0abd535080bed34cb41043f95f5b
parent3a206219f1a20712ae456ec313c365317d334684 (diff)
downloadpakakeh.go-0c288a5880fbc0fceb792dcfd0747d95163a90dc.tar.xz
lib/memfs: add method to stop the Watch
The StopWatch method stop watching for update, from calling Watch.
-rw-r--r--lib/memfs/dirwatcher_test.go3
-rw-r--r--lib/memfs/memfs.go24
2 files changed, 21 insertions, 6 deletions
diff --git a/lib/memfs/dirwatcher_test.go b/lib/memfs/dirwatcher_test.go
index a62f6001..84ac33ce 100644
--- a/lib/memfs/dirwatcher_test.go
+++ b/lib/memfs/dirwatcher_test.go
@@ -74,8 +74,7 @@ func TestDirWatcher_renameDirectory(t *testing.T) {
<-dw.C
<-dw.C
- // Wait for all watcher finished.
- time.Sleep(400 * time.Millisecond)
+ dw.Stop()
var expDirs = []string{
"/newsubdir",
diff --git a/lib/memfs/memfs.go b/lib/memfs/memfs.go
index 12b2d5d9..461faa4d 100644
--- a/lib/memfs/memfs.go
+++ b/lib/memfs/memfs.go
@@ -393,6 +393,17 @@ func (mfs *MemFS) Search(words []string, snippetLen int) (results []SearchResult
}
//
+// StopWatch stop watching for update, from calling Watch.
+//
+func (mfs *MemFS) StopWatch() {
+ if mfs.dw == nil {
+ return
+ }
+ mfs.dw.Stop()
+ mfs.dw = nil
+}
+
+//
// Update the node content and information in memory based on new file
// information.
// This method only check if the node name is equal with file name, but it's
@@ -420,27 +431,32 @@ func (mfs *MemFS) Update(node *Node, newInfo os.FileInfo) {
// directory.
// The MemFS will update the tree and node content automatically if the file
// get deleted or updated.
-// The returned channel nsq is ready to be consumed.
+// The returned DirWatcher is ready to use.
+// To stop watching for update call the StopWatch.
//
func (mfs *MemFS) Watch(d time.Duration) (dw *DirWatcher, err error) {
var (
logp = "Watch"
)
- dw = &DirWatcher{
+ if mfs.dw != nil {
+ return mfs.dw, nil
+ }
+
+ mfs.dw = &DirWatcher{
fs: mfs,
Delay: d,
Options: *mfs.Opts,
}
- err = dw.Start()
+ err = mfs.dw.Start()
if err != nil {
// There should be no error here, since we already check and
// filled the required fields for DirWatcher.
return nil, fmt.Errorf("%s: %w", logp, err)
}
- return dw, nil
+ return mfs.dw, nil
}
func (mfs *MemFS) createRoot() error {