(def barbary-watcher-impl
{:register! (fn [this ^Path path events]
(let [file (-> path .toFile WatchableFile.)
events (into-array (map barbary-watch-event-kinds events))]
(.register file this events)))
:take! (fn [this]
(try (take-events
this
#(.take ^com.barbarysoftware.watchservice.WatchService %)
#(.pollEvents ^com.barbarysoftware.watchservice.WatchKey %)
(fn [_ ^com.barbarysoftware.watchservice.WatchEvent event]
(let [kind (.kind event)
context (.context event)]
{:file (-> context str io/file .getCanonicalFile)
:kind ((map-invert barbary-watch-event-kinds) kind)}))
#(.reset ^com.barbarysoftware.watchservice.WatchService %))
(catch com.barbarysoftware.watchservice.ClosedWatchServiceException _ _ nil)))
:stop! (fn [^com.barbarysoftware.watchservice.WatchService this]
(.close this))})