From 3929cc017619faa955ed20710fd3971810d5c540 Mon Sep 17 00:00:00 2001 From: Michael Glaesemann Date: Sat, 24 Sep 2016 18:55:58 -0400 Subject: [PATCH] Add clear method for Cache. Change-Id: I7cf64ff04d162225b40e76f6b7abfd56e92e883a --- src/main/om/next/cache.cljs | 6 +++++- src/test/om/next/tests.cljc | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/om/next/cache.cljs b/src/main/om/next/cache.cljs index 9cc56906..c33c2e4b 100644 --- a/src/main/om/next/cache.cljs +++ b/src/main/om/next/cache.cljs @@ -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)) diff --git a/src/test/om/next/tests.cljc b/src/test/om/next/tests.cljc index a8a857d9..ad3351a7 100644 --- a/src/test/om/next/tests.cljc +++ b/src/test/om/next/tests.cljc @@ -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