(path string, stopChan chan bool)
| 26 | } |
| 27 | |
| 28 | func SetupWatcher(path string, stopChan chan bool) { |
| 29 | watcher, err := fsnotify.NewWatcher() |
| 30 | if err != nil { |
| 31 | log.Println("Watcher creation error:", err) |
| 32 | return |
| 33 | } |
| 34 | defer watcher.Close() |
| 35 | defer close(stopChan) |
| 36 | |
| 37 | err = watcher.Add(path) |
| 38 | if err != nil { |
| 39 | log.Println("Watcher add error:", err) |
| 40 | return |
| 41 | } |
| 42 | |
| 43 | // On file changed by the host computer |
| 44 | for { |
| 45 | select { |
| 46 | case event, ok := <-watcher.Events: |
| 47 | if !ok { |
| 48 | return |
| 49 | } |
| 50 | watcherCache, ok := cache.EditorWatcherCache.Get(path) |
| 51 | if !ok { |
| 52 | return |
| 53 | } |
| 54 | onFileChanged(event, path, watcherCache) |
| 55 | |
| 56 | case _, ok := <-watcher.Errors: |
| 57 | if !ok { |
| 58 | return |
| 59 | } |
| 60 | |
| 61 | case <-stopChan: |
| 62 | // Watcher received stop signal |
| 63 | return |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | } |
| 68 | |
| 69 | func onFileChanged(event fsnotify.Event, path string, watcherCache types.EditorWatcherCache) { |
| 70 | if !event.Has(fsnotify.Write) { |
no test coverage detected