Skip to content

Commit 3929cc0

Browse files
committed
Add clear method for Cache.
Change-Id: I7cf64ff04d162225b40e76f6b7abfd56e92e883a
1 parent f9dc813 commit 3929cc0

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/main/om/next/cache.cljs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
(swap! index assoc id x')))
1111
(.push arr id))
1212
(get [this id]
13-
(get @index id)))
13+
(get @index id))
14+
(clear [this]
15+
(reset! (.-index this) {})
16+
(set! (.-arr this) #js [])
17+
this))
1418

1519
(defn cache [size]
1620
(Cache. #js [] (atom {}) size))

src/test/om/next/tests.cljc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,27 @@
10571057
(is (not (contains? (get t-err 'this/throws) :result)))
10581058
(is (= (get (om/transact! r '[(this/throws) :app/count]) :app/count) 2))))
10591059

1060+
#?(:cljs
1061+
(deftest test-cache-clear
1062+
(let [r (om/reconciler {:state (atom {:app/count 0})
1063+
:parser p})
1064+
h (get-in r [:config :history])]
1065+
(is (= 0 (count @(.-index h))))
1066+
(is (= 0 (count (.-arr h))))
1067+
(om/transact! r '[(app/inc!)])
1068+
(let [h (get-in r [:config :history])]
1069+
(is (= 1 (count @(.-index h))))
1070+
(is (= 1 (count (.-arr h)))))
1071+
(let [r' (update-in r [:config :history] #(.clear %))
1072+
h' (get-in r' [:config :history])]
1073+
(is (= {} @(.-index h')))
1074+
(is (= 0 (count (.-arr h'))))
1075+
(is (= [] (js->clj (.-arr h')))))
1076+
(om/transact! r '[(app/inc!)])
1077+
(let [h (get-in r [:config :history])]
1078+
(is (= 1 (count @(.-index h))))
1079+
(is (= 1 (count (.-arr h))))))))
1080+
10601081
;; -----------------------------------------------------------------------------
10611082
;; Recursive Parsing
10621083

0 commit comments

Comments
 (0)