track renames better by manually keeping watch key path
We had an issue where watching a directory, then renaming it, causes incorrect file events to be generated since the WatchKey does not have the correct path in its Watchable. It seems like a known issue according to the java docs for WatchKey.watchable():
When watching a directory for changes for example, and the directory is moved or renamed in the file system, there is no guarantee that the watch key will be cancelled and so the object returned by this method may no longer be a valid path to the directory.
So, instead of trusting the Watchable object, I stored an atom that keeps track of WatchKey -> Path, updating as necessary. It sort of takes from the example Oracle has here which also keeps its own path map: https://docs.oracle.com/javase/tutorial/essential/io/examples/WatchDir.java , which funny enough, also doesn't use its Watchable.
This is a great lightweight wrapper of the WatchService and hope we can keep using it in the future : )