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