core/memoize is very slow, so use our own instead

This commit is contained in:
Dan Holmsand 2014-03-06 12:24:07 +01:00
parent 6302c8ecd6
commit c189bd4460
1 changed files with 25 additions and 13 deletions

View File

@ -22,6 +22,16 @@
;;; Common utilities ;;; Common utilities
(defn memoize-1 [f]
(let [mem (atom {})]
(fn [arg]
(let [v (get @mem arg)]
(if-not (nil? v)
v
(let [ret (f arg)]
(swap! mem assoc arg ret)
ret))))))
(defn hiccup-tag? [x] (defn hiccup-tag? [x]
(or (keyword? x) (or (keyword? x)
(symbol? x) (symbol? x)
@ -52,8 +62,8 @@
;;; Props conversion ;;; Props conversion
(def cached-prop-name (memoize undash-prop-name)) (def cached-prop-name (memoize-1 undash-prop-name))
(def cached-style-name (memoize util/dash-to-camel)) (def cached-style-name (memoize-1 util/dash-to-camel))
(defn convert-prop-value [val] (defn convert-prop-value [val]
(if (map? val) (if (map? val)
@ -61,15 +71,17 @@
(doto res (doto res
(aset (cached-prop-name k) (aset (cached-prop-name k)
(to-js-val v)))) (to-js-val v))))
(js-obj) val) #js {} val)
(to-js-val val))) (to-js-val val)))
(defn set-id-class [props [id class]] (defn set-id-class [props [id class]]
(aset props "id" (or (aget props "id") id)) (let [pid (aget props "id")]
(when class (aset props "id" (if-not (nil? pid) pid id))
(aset props "className" (if-let [old (aget props "className")] (when-not (nil? class)
(str class " " old) (aset props "className" (let [old (aget props "className")]
class)))) (if-not (nil? old)
(str class " " old)
class))))))
(defn convert-props [props id-class] (defn convert-props [props id-class]
(cond (cond
@ -129,7 +141,7 @@
(defn wrapped-render [this comp id-class input-setup] (defn wrapped-render [this comp id-class input-setup]
(let [inprops (util/js-props this) (let [inprops (util/js-props this)
argv (aget inprops cljs-argv) argv (aget inprops cljs-argv)
props (get argv 1) props (nth argv 1 nil)
hasprops (or (nil? props) (map? props)) hasprops (or (nil? props) (map? props))
first-child (if hasprops 2 1) first-child (if hasprops 2 1)
children (if (> (count argv) first-child) children (if (> (count argv) first-child)
@ -185,7 +197,7 @@
(let [[comp id-class] (parse-tag tag)] (let [[comp id-class] (parse-tag tag)]
(wrap-component comp id-class (str tag)))) (wrap-component comp id-class (str tag))))
(def cached-wrapper (memoize get-wrapper)) (def cached-wrapper (memoize-1 get-wrapper))
(defn fn-to-class [f] (defn fn-to-class [f]
(let [spec (meta f) (let [spec (meta f)
@ -211,14 +223,14 @@
(defn vec-to-comp [v level] (defn vec-to-comp [v level]
(assert (pos? (count v)) "Hiccup form should not be empty") (assert (pos? (count v)) "Hiccup form should not be empty")
(assert (valid-tag? (v 0)) (assert (valid-tag? (nth v 0))
(str "Invalid Hiccup form: " (pr-str v))) (str "Invalid Hiccup form: " (pr-str v)))
(let [c (as-class (v 0)) (let [c (as-class (nth v 0))
jsprops (js-obj cljs-argv v jsprops (js-obj cljs-argv v
cljs-level level)] cljs-level level)]
(let [k (-> v meta get-key) (let [k (-> v meta get-key)
k' (if (nil? k) k' (if (nil? k)
(-> v (get 1) get-key) (-> v (nth 1 nil) get-key)
k)] k)]
(when-not (nil? k') (when-not (nil? k')
(aset jsprops "key" k'))) (aset jsprops "key" k')))