Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

om/next/cache.cljc #832

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/main/om/next.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))))])
Expand Down Expand Up @@ -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])
Expand All @@ -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
Expand Down Expand Up @@ -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."
Expand Down
30 changes: 30 additions & 0 deletions src/main/om/next/cache.cljc
Original file line number Diff line number Diff line change
@@ -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))
16 changes: 0 additions & 16 deletions src/main/om/next/cache.cljs

This file was deleted.