Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added remove-cell-watcher to dataflow #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 18 additions & 3 deletions modules/dataflow/src/main/clojure/clojure/contrib/dataflow.clj
Expand Up @@ -63,7 +63,7 @@
:name ; Always ::validator
:dependents ; The names of cells on which this depends, a collection
:fun ; A clojure that can throw an exception
:display ; The original exprssion for display
:display ; The original expression for display
:cell-type) ; Should be ::validator-cell

(derive ::validator-cell ::dependent-cell)
Expand Down Expand Up @@ -459,15 +459,22 @@
;;; Watchers

(defn add-cell-watcher
"Adds a watcher to a cell to respond to changes of value. The is a
"Adds a watcher to a cell to respond to changes of value. fun is a
function of 4 values: a key, the cell, its old value, its new
value. This is implemented using Clojure's add-watch to the
underlying ref, and shared its sematics"
underlying ref, and shares its semantics, i.e. keys must be unique
per cell. Keys can be used with remove-cell-watcher to remove the
watcher"
[cell key fun]
(let [val (:value cell)]
(add-watch val key (fn [key _ old-v new-v]
(fun key cell old-v new-v)))))

(defn remove-cell-watcher
"Removes a watcher (set by add-cell-watcher) from a cell"
[cell key]
(let [val (:value cell)]
(remove-watch val key)))

(comment

Expand All @@ -491,7 +498,15 @@
(fn [key cell o n]
(printf "sally changed from %s to %s\n" o n)))

(add-cell-watcher (get-cell df 'mary)
"mary-watcher"
(fn [key cell o n]
(printf "mary changed from %s to %s\n" o n)))

(update-values df {'fred 1 'mary 1})

(remove-cell-watcher (get-cell df 'mary) "mary-watcher")

(update-values df {'fred 5 'mary 1})
(update-values df {'fred 0 'mary 0})

Expand Down