anoia service :events method behaves as iterator

This commit is contained in:
Daniel Barlow 2023-09-12 20:42:42 +01:00
parent 7904c6bfe9
commit 8f97c5bf3c
2 changed files with 13 additions and 6 deletions

View file

@ -31,14 +31,14 @@
new-addresses)) new-addresses))
(fn run [] (fn run []
(var addresses [])
(let [[state-directory wan-device] arg (let [[state-directory wan-device] arg
dir (svc.open state-directory)] dir (svc.open state-directory)]
(var addresses []) (each [v (dir:events)]
(while true ;; we don't handle unbound or stopped, where we should
(while (not (dir:ready?)) (dir:wait)) ;; take the addresses away
(when (. bound-states (dir:output "state")) (when (. bound-states (v:output "state"))
(set addresses (set addresses
(update-addresses wan-device addresses (dir:output "address")))) (update-addresses wan-device addresses (v:output "address")))))))
(dir:wait))))
{ : update-addresses : changes : run } { : update-addresses : changes : run }

View file

@ -33,6 +33,12 @@
unknown unknown
(error (.. "can't read " pathname " of kind \"" unknown.mode "\"")))) (error (.. "can't read " pathname " of kind \"" unknown.mode "\""))))
(fn events [self]
(coroutine.wrap
#(while true
(while (not (self:ready?)) (self:wait))
(coroutine.yield self)
(self:wait))))
(fn open [directory] (fn open [directory]
(let [watcher (watch-fsevents directory) (let [watcher (watch-fsevents directory)
@ -44,6 +50,7 @@
:output (fn [_ filename] :output (fn [_ filename]
(read-value (.. directory "/" filename))) (read-value (.. directory "/" filename)))
:close #(watcher:close) :close #(watcher:close)
: events
})) }))
{ : open } { : open }