implement unsubscribe
and add ids to subscribe so that there's a unique identifier to pass to unsubscribe
This commit is contained in:
parent
cbb1de804e
commit
52eb283a26
2 changed files with 16 additions and 4 deletions
|
@ -28,7 +28,7 @@
|
||||||
;; should we do something for bind?
|
;; should we do something for bind?
|
||||||
:remove (tset db e.path nil)
|
:remove (tset db e.path nil)
|
||||||
)
|
)
|
||||||
(each [_ { : terms : callback } (ipairs subscribers)]
|
(each [_ { : terms : callback } (pairs subscribers)]
|
||||||
(if (event-matches? e terms) (callback e)))
|
(if (event-matches? e terms) (callback e)))
|
||||||
e))
|
e))
|
||||||
|
|
||||||
|
@ -39,8 +39,9 @@
|
||||||
:find (fn [_ terms] (find-in-database db terms))
|
:find (fn [_ terms] (find-in-database db terms))
|
||||||
: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 [_ callback terms]
|
:subscribe (fn [_ id callback terms]
|
||||||
(table.insert subscribers {: callback : terms }))
|
(tset subscribers id {: callback : terms }))
|
||||||
|
:unsubscribe (fn [_ id] (tset subscribers id nil))
|
||||||
|
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
|
|
@ -119,11 +119,22 @@ MINOR=17")
|
||||||
(var received [])
|
(var received [])
|
||||||
(let [db (database)
|
(let [db (database)
|
||||||
subscriber (fn [e] (table.insert received e))]
|
subscriber (fn [e] (table.insert received e))]
|
||||||
(db:subscribe subscriber {:devname "/dev/sdb1"})
|
(db:subscribe :me subscriber {:devname "/dev/sdb1"})
|
||||||
(db:add sdb1-insert)
|
(db:add sdb1-insert)
|
||||||
(db:add sda-uevent)
|
(db:add sda-uevent)
|
||||||
(db:add sdb1-remove)
|
(db:add sdb1-remove)
|
||||||
(expect= (# received) 2)))
|
(expect= (# received) 2)))
|
||||||
|
|
||||||
|
(example
|
||||||
|
"I can unsubscribe after subscribing"
|
||||||
|
(var received [])
|
||||||
|
(let [db (database)
|
||||||
|
subscriber (fn [e] (table.insert received e))]
|
||||||
|
(db:subscribe :me subscriber {:devname "/dev/sdb1"})
|
||||||
|
(db:unsubscribe :me)
|
||||||
|
(db:add sdb1-insert)
|
||||||
|
(db:add sda-uevent)
|
||||||
|
(db:add sdb1-remove)
|
||||||
|
(expect= (# received) 0)))
|
||||||
|
|
||||||
(if failed (os.exit 1) (print "OK"))
|
(if failed (os.exit 1) (print "OK"))
|
||||||
|
|
Loading…
Reference in a new issue