commit cf9cadd212b6053cf73875616c14af3cfb9d198e parent a116fe084af552981faebd94aa18e11dd2c8e56a Author: Daniel Barlow <dan@telent.net> Date: Fri, 26 Apr 2024 17:03:30 +0100 devout: replay relevant events to new subscriber Diffstat:
| M | pkgs/devout/devout.fnl | | | 3 | +++ |
| M | pkgs/devout/test.fnl | | | 10 | ++++++++++ |
2 files changed, 13 insertions(+), 0 deletions(-)
diff --git a/pkgs/devout/devout.fnl b/pkgs/devout/devout.fnl @@ -53,6 +53,9 @@ :add (fn [_ event-string] (record-event db subscribers event-string)) :at-path (fn [_ path] (. db path)) :subscribe (fn [_ id callback terms] + (let [past-events (find-in-database db terms)] + (each [_ e (pairs past-events)] + (callback e))) (tset subscribers id {: callback : terms })) :unsubscribe (fn [_ id] (tset subscribers id nil)) })) diff --git a/pkgs/devout/test.fnl b/pkgs/devout/test.fnl @@ -127,6 +127,16 @@ MINOR=17") (expect= (# received) 2))) (example + "Subscribers get notifications of prior events for present devices" + (var received []) + (let [db (database) + subscriber (fn [e] (table.insert received e))] + (db:add sdb1-insert) + (db:add sda-uevent) + (db:subscribe :me subscriber {:devname "/dev/sdb1"}) + (expect= (# received) 1))) + +(example "I can unsubscribe after subscribing" (var received []) (let [db (database)