Skip to content

Commit

Permalink
Add meta to cells
Browse files Browse the repository at this point in the history
- Add a new arity to cell so we can add metadata when creating the cell (same as atoms)
- Implement cljs.core/IWithMeta to be able to create cells with meta
- Tests
  • Loading branch information
mynomoto committed Apr 24, 2015
1 parent 58890bd commit 763773f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/tailrecursion/javelin.cljs
Expand Up @@ -71,6 +71,9 @@
cljs.core/IPrintWithWriter
(-pr-writer [this w _] (write-all w "#<Cell: " (pr-str state) ">"))

cljs.core/IWithMeta
(-with-meta [this meta] (Cell. meta state rank prev sources sinks thunk watches update))

cljs.core/IMeta
(-meta [this] meta)

Expand Down Expand Up @@ -102,7 +105,9 @@
(defn set-cell! [c x] (set! (.-state c) x) (set-formula! c))
(defn formula [f] (fn [& sources] (set-formula! (cell ::none) f sources)))
(defn lens [c f] (let [c ((formula identity) c)] (set! (.-update c) f) c))
(defn cell [x] (set-formula! (Cell. {} x (next-rank) x [] #{} nil {} nil)))
(defn cell
([x] (set-formula! (Cell. nil x (next-rank) x [] #{} nil {} nil)))
([x & {:keys [meta]}] (set-formula! (Cell. meta x (next-rank) x [] #{} nil {} nil))))

(def ^:deprecated lift formula)

Expand Down
9 changes: 8 additions & 1 deletion test/tailrecursion/javelin/test.cljs
Expand Up @@ -801,5 +801,12 @@
(fn [_ _ old new]
(swap! log conj {:old old :new new})))
(swap! x inc)
(is (= @log [{:old [["b" 1] 1] :new [["b" 2] 2]}]))))))
(is (= @log [{:old [["b" 1] 1] :new [["b" 2] 2]}]))))

(testing "adding meta"
(let [x (cell 1 :meta {:foo :bar})
y (cell 1)]
(is (= (meta x) {:foo :bar}))
(is (nil? (meta y)))
(is (= {:bar :baz} (meta (with-meta y {:bar :baz}))))
(is (= {:a :b} (meta (vary-meta y assoc :a :b))))))))

0 comments on commit 763773f

Please sign in to comment.