Skip to content

Commit

Permalink
Add clear method for Cache.
Browse files Browse the repository at this point in the history
Change-Id: Ic9407795088cc063a8be0bdbd75e70316346c19f
  • Loading branch information
grzm committed Sep 24, 2016
1 parent b90fdb6 commit 3730085
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/main/om/next/cache.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
(swap! index assoc id x')))
(.push arr id))
(get [this id]
(get @index id)))
(get @index id))
(clear [this]
(reset! (.-index this) {})
(set! (.-arr this) #js [])
this))

(defn cache [size]
(Cache. #js [] (atom {}) size))
20 changes: 20 additions & 0 deletions src/test/om/next/tests.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,26 @@
(is (not (contains? (get t-err 'this/throws) :result)))
(is (= (get (om/transact! r '[(this/throws) :app/count]) :app/count) 2))))

(deftest test-cache-clear
(let [r (om/reconciler {:state (atom {:app/count 0})
:parser p})
h (get-in r [:config :history])]
(is (= 0 (count @(.-index h))))
(is (= 0 (count (.-arr h))))
(om/transact! r '[(app/inc!)])
(let [h (get-in r [:config :history])]
(is (= 1 (count @(.-index h))))
(is (= 1 (count (.-arr h)))))
(let [r' (update-in r [:config :history] #(.clear %))
h' (get-in r' [:config :history])]
(is (= {} @(.-index h')))
(is (= 0 (count (.-arr h'))))
(is (= [] (js->clj (.-arr h')))))
(om/transact! r '[(app/inc!)])
(let [h (get-in r [:config :history])]
(is (= 1 (count @(.-index h))))
(is (= 1 (count (.-arr h)))))))

;; -----------------------------------------------------------------------------
;; Recursive Parsing

Expand Down

0 comments on commit 3730085

Please sign in to comment.