Skip to content

Commit

Permalink
Add clear method for Cache.
Browse files Browse the repository at this point in the history
Change-Id: I7cf64ff04d162225b40e76f6b7abfd56e92e883a
  • Loading branch information
grzm committed Nov 16, 2016
1 parent f9dc813 commit 3929cc0
Show file tree
Hide file tree
Showing 2 changed files with 26 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))
21 changes: 21 additions & 0 deletions src/test/om/next/tests.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,27 @@
(is (not (contains? (get t-err 'this/throws) :result)))
(is (= (get (om/transact! r '[(this/throws) :app/count]) :app/count) 2))))

#?(:cljs
(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 3929cc0

Please sign in to comment.