devout: replay relevant events to new subscriber

This commit is contained in:
Daniel Barlow 2024-04-26 17:03:30 +01:00
parent a116fe084a
commit cf9cadd212
2 changed files with 13 additions and 0 deletions

View file

@ -53,6 +53,9 @@
:add (fn [_ event-string] (record-event db subscribers event-string)) :add (fn [_ event-string] (record-event db subscribers event-string))
:at-path (fn [_ path] (. db path)) :at-path (fn [_ path] (. db path))
:subscribe (fn [_ id callback terms] :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 })) (tset subscribers id {: callback : terms }))
:unsubscribe (fn [_ id] (tset subscribers id nil)) :unsubscribe (fn [_ id] (tset subscribers id nil))
})) }))

View file

@ -126,6 +126,16 @@ MINOR=17")
(db:add sdb1-remove) (db:add sdb1-remove)
(expect= (# received) 2))) (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 (example
"I can unsubscribe after subscribing" "I can unsubscribe after subscribing"
(var received []) (var received [])