diff --git a/src/main/om/next.cljc b/src/main/om/next.cljc index 749a2fbd..22808f61 100644 --- a/src/main/om/next.cljc +++ b/src/main/om/next.cljc @@ -8,9 +8,9 @@ [cljs.util]] :cljs [[goog.string :as gstring] [goog.object :as gobj] - [goog.log :as glog] - [om.next.cache :as c]]) + [goog.log :as glog]]) [om.next.impl.parser :as parser] + [om.next.cache :as c] [om.tempid :as tempid] [om.transit :as transit] [om.util :as util] @@ -1282,7 +1282,8 @@ st (:state cfg) id #?(:clj (java.util.UUID/randomUUID) :cljs (random-uuid))] - #?(:cljs (.add (:history cfg) id @st)) + (when-let [h (:history cfg)] + (c/add h id @st)) #?(:cljs (when-let [l (:logger cfg)] (glog/info l @@ -1504,9 +1505,10 @@ {:ref ref})) id #?(:clj (java.util.UUID/randomUUID) :cljs (random-uuid)) + _ (when-let [h (:history cfg)] + (c/add h id @(:state cfg))) #?@(:cljs - [_ (.add (:history cfg) id @(:state cfg)) - _ (when-let [l (:logger cfg)] + [_ (when-let [l (:logger cfg)] (glog/info l (str (when ref (str (pr-str ref) " ")) "transacted '" tx ", " (pr-str id))))]) @@ -2744,7 +2746,7 @@ merge-ident default-merge-ident prune-tree default-extract-errors optimize (fn [cs] (sort-by depth cs)) - history 100 + history #?(:clj nil :cljs 100) root-render #?(:clj (fn [c target] c) :cljs #(js/ReactDOM.render %1 %2)) root-unmount #?(:clj (fn [x]) @@ -2770,7 +2772,7 @@ :prune-tree prune-tree :optimize optimize :normalize (or (not norm?) normalize) - :history #?(:clj [] + :history #?(:clj (when history (c/cache history)) :cljs (c/cache history)) :root-render root-render :root-unmount root-unmount :logger logger :pathopt pathopt @@ -2819,7 +2821,7 @@ may be configured by the :history option when constructing the reconciler." [reconciler uuid] {:pre [(reconciler? reconciler)]} - (.get (-> reconciler :config :history) uuid)) + (c/get (-> reconciler :config :history) uuid)) (defn tempid "Return a temporary id." diff --git a/src/main/om/next/cache.cljc b/src/main/om/next/cache.cljc new file mode 100644 index 00000000..49057721 --- /dev/null +++ b/src/main/om/next/cache.cljc @@ -0,0 +1,30 @@ +(ns om.next.cache + (:refer-clojure :exclude [get])) + +(defprotocol ICache + (add [this id x]) + (get [this id]) + (get-most-recent-id [this])) + +(deftype Cache [state size] + ICache + (add [this id x] + (let [x' (vary-meta x assoc :client-time #?(:cljs (js/Date.) :clj (java.util.Date.))) + append-cache #(-> (update % :queue conj id) + (assoc-in [:index id] x')) + trim-cache #(-> (update % :queue pop) + (update :index dissoc (first (:queue %))))] + (swap! state (cond-> append-cache + (<= size (count (:queue @state))) + (comp trim-cache))))) + (get [this id] + (get-in @state [:index id])) + (get-most-recent-id [this] + (let [q (:queue @state)] + (nth q (dec (count q)) nil)))) + +(defn cache [size] + (Cache. (atom {:index {} + :queue #?(:clj clojure.lang.PersistentQueue/EMPTY + :cljs cljs.core.PersistentQueue.EMPTY)}) + size)) diff --git a/src/main/om/next/cache.cljs b/src/main/om/next/cache.cljs deleted file mode 100644 index 9cc56906..00000000 --- a/src/main/om/next/cache.cljs +++ /dev/null @@ -1,16 +0,0 @@ -(ns om.next.cache) - -(deftype Cache [arr index size] - Object - (add [this id x] - (let [x' (vary-meta x assoc :client-time (js/Date.))] - (if (<= size (alength arr)) - (let [id' (.shift arr)] - (swap! index #(-> % (dissoc id') (assoc id x')))) - (swap! index assoc id x'))) - (.push arr id)) - (get [this id] - (get @index id))) - -(defn cache [size] - (Cache. #js [] (atom {}) size))