From 8f97c5bf3c0afd9593db7e3a4c09e5f00863d9a2 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Tue, 12 Sep 2023 20:42:42 +0100 Subject: [PATCH] anoia service :events method behaves as iterator --- examples/acquire-wan-address.fnl | 12 ++++++------ pkgs/anoia/svc.fnl | 7 +++++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/examples/acquire-wan-address.fnl b/examples/acquire-wan-address.fnl index faf20d0..07a1da0 100644 --- a/examples/acquire-wan-address.fnl +++ b/examples/acquire-wan-address.fnl @@ -31,14 +31,14 @@ new-addresses)) (fn run [] + (var addresses []) (let [[state-directory wan-device] arg dir (svc.open state-directory)] - (var addresses []) - (while true - (while (not (dir:ready?)) (dir:wait)) - (when (. bound-states (dir:output "state")) + (each [v (dir:events)] + ;; we don't handle unbound or stopped, where we should + ;; take the addresses away + (when (. bound-states (v:output "state")) (set addresses - (update-addresses wan-device addresses (dir:output "address")))) - (dir:wait)))) + (update-addresses wan-device addresses (v:output "address"))))))) { : update-addresses : changes : run } diff --git a/pkgs/anoia/svc.fnl b/pkgs/anoia/svc.fnl index 468434e..d980e7f 100644 --- a/pkgs/anoia/svc.fnl +++ b/pkgs/anoia/svc.fnl @@ -33,6 +33,12 @@ unknown (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] (let [watcher (watch-fsevents directory) @@ -44,6 +50,7 @@ :output (fn [_ filename] (read-value (.. directory "/" filename))) :close #(watcher:close) + : events })) { : open }