Get rid of pesky circular dependency

This commit is contained in:
Dan Holmsand 2014-02-11 15:08:50 +01:00
parent 953404b2cc
commit c18b8c318e
4 changed files with 47 additions and 44 deletions

View File

@ -7,9 +7,9 @@
[reagent.impl.util :as util] [reagent.impl.util :as util]
[reagent.ratom :as ratom])) [reagent.ratom :as ratom]))
(def React tmpl/React) (def React util/React)
(def is-client util/isClient) (def is-client util/is-client)
(defn render-component (defn render-component
"Render a Reagent component into the DOM. The first argument may be either a "Render a Reagent component into the DOM. The first argument may be either a
@ -53,7 +53,7 @@ looking like this:
Everything is optional, except :render. Everything is optional, except :render.
" "
[spec] [spec]
(comp/create-class spec)) (comp/create-class spec tmpl/as-component))
(defn current-component [] (defn current-component []

View File

@ -1,8 +1,6 @@
(ns reagent.impl.component (ns reagent.impl.component
(:require [reagent.impl.template :as tmpl (:require [reagent.impl.util :as util :refer [cljs-level cljs-argv React]]
:refer [cljs-argv cljs-level React]]
[reagent.impl.util :as util :refer [cljs-level]]
[reagent.ratom :as ratom] [reagent.ratom :as ratom]
[reagent.debug :refer-macros [dbg prn]])) [reagent.debug :refer-macros [dbg prn]]))
@ -70,7 +68,7 @@
5 (f (argv 1) (argv 2) (argv 3) (argv 4)) 5 (f (argv 1) (argv 2) (argv 3) (argv 4))
(apply f (subvec argv 1)))))] (apply f (subvec argv 1)))))]
(if (vector? res) (if (vector? res)
(tmpl/as-component res (aget p cljs-level)) (.asComponent C res (aget p cljs-level))
(if (ifn? res) (if (ifn? res)
(do (do
(aset C cljs-render res) (aset C cljs-render res)
@ -134,12 +132,15 @@
(this-as C (apply f C args))) (this-as C (apply f C args)))
f)) f))
(def dont-wrap #{:cljsRender :render}) (def dont-wrap #{:cljsRender :render :componentFunction})
(defn dont-bind [f]
(doto f
(aset "__reactDontBind" true)))
(defn get-wrapper [key f name] (defn get-wrapper [key f name]
(if (dont-wrap key) (if (dont-wrap key)
(doto f (dont-bind f)
(aset "__reactDontBind" true))
(let [wrap (custom-wrapper key f)] (let [wrap (custom-wrapper key f)]
(when (and wrap f) (when (and wrap f)
(assert (ifn? f) (assert (ifn? f)
@ -151,7 +152,7 @@
(defn camelify-map-keys [fun-map] (defn camelify-map-keys [fun-map]
(reduce-kv (fn [m k v] (reduce-kv (fn [m k v]
(assoc m (-> k tmpl/dash-to-camel keyword) v)) (assoc m (-> k util/dash-to-camel keyword) v))
{} fun-map)) {} fun-map))
(defn add-obligatory [fun-map] (defn add-obligatory [fun-map]
@ -160,7 +161,7 @@
(defn add-render [fun-map render-f] (defn add-render [fun-map render-f]
(assoc fun-map (assoc fun-map
:cljsRender render-f :cljsRender render-f
:render (if util/isClient :render (if util/is-client
(fn [] (fn []
(this-as C (this-as C
(util/run-reactively C #(do-render C)))) (util/run-reactively C #(do-render C))))
@ -197,12 +198,13 @@
map-to-js)) map-to-js))
(defn create-class (defn create-class
[body] [body as-component]
(assert (map? body)) (assert (map? body))
(let [spec (cljsify body) (let [spec (cljsify body)
_ (set! (.-asComponent spec) (dont-bind as-component))
res (.createClass React spec) res (.createClass React spec)
f (fn [& args] f (fn [& args]
(tmpl/as-component (apply vector res args)))] (as-component (apply vector res args)))]
(set! (.-cljsReactClass f) res) (set! (.-cljsReactClass f) res)
(set! (.-cljsReactClass res) res) (set! (.-cljsReactClass res) res)
f)) f))

View File

@ -1,22 +1,16 @@
(ns reagent.impl.template (ns reagent.impl.template
(:require [clojure.string :as string] (:require [clojure.string :as string]
[reagent.impl.reactimport :as reactimport] [reagent.impl.util :as util
[reagent.impl.util :as util :refer [cljs-level]] :refer [cljs-level cljs-argv is-client React]]
[reagent.impl.component :as comp]
[reagent.ratom :as ratom] [reagent.ratom :as ratom]
[reagent.debug :refer-macros [dbg prn println log]])) [reagent.debug :refer-macros [dbg prn println log]]))
(def React reactimport/React)
(def debug false) (def debug false)
(assert (set! debug true)) (assert (set! debug true))
(def cljs-argv "cljsArgv")
(def isClient util/isClient)
(def dont-camel-case #{"aria" "data"})
(defn hiccup-tag? [x] (defn hiccup-tag? [x]
(or (keyword? x) (or (keyword? x)
(symbol? x) (symbol? x)
@ -26,30 +20,16 @@
(or (hiccup-tag? x) (or (hiccup-tag? x)
(ifn? x))) (ifn? x)))
(defn capitalize [s]
(if (< (count s) 2)
(string/upper-case s)
(str (string/upper-case (subs s 0 1)) (subs s 1))))
(defn dash-to-camel [dashed]
(if (string? dashed)
dashed
(let [name-str (name dashed)
[start & parts] (string/split name-str #"-")]
(if (dont-camel-case start)
name-str
(apply str start (map capitalize parts))))))
(def attr-aliases {:class "className" (def attr-aliases {:class "className"
:for "htmlFor" :for "htmlFor"
:charset "charSet"}) :charset "charSet"})
(defn undash-prop-name [n] (defn undash-prop-name [n]
(or (attr-aliases n) (or (attr-aliases n)
(dash-to-camel n))) (util/dash-to-camel n)))
(def cached-prop-name (memoize undash-prop-name)) (def cached-prop-name (memoize undash-prop-name))
(def cached-style-name (memoize dash-to-camel)) (def cached-style-name (memoize util/dash-to-camel))
(defn to-js-val [v] (defn to-js-val [v]
(if-not (ifn? v) (if-not (ifn? v)
@ -198,7 +178,7 @@
(defn fn-to-class [f] (defn fn-to-class [f]
(let [spec (meta f) (let [spec (meta f)
withrender (assoc spec :component-function f) withrender (assoc spec :component-function f)
res (reagent.core/create-class withrender) res (comp/create-class withrender as-component)
wrapf (.-cljsReactClass res)] wrapf (.-cljsReactClass res)]
(set! (.-cljsReactClass f) wrapf) (set! (.-cljsReactClass f) wrapf)
wrapf)) wrapf))

View File

@ -1,12 +1,16 @@
(ns reagent.impl.util (ns reagent.impl.util
(:refer-clojure :exclude [flush]) (:refer-clojure :exclude [flush])
(:require [reagent.debug :refer-macros [dbg log]] (:require [reagent.debug :refer-macros [dbg log]]
[reagent.ratom :as ratom])) [reagent.ratom :as ratom]
[reagent.impl.reactimport :as reactimport]
[clojure.string :as string]))
(def isClient (not (nil? (try (.-document js/window) (def is-client (not (nil? (try (.-document js/window)
(catch js/Object e nil))))) (catch js/Object e nil)))))
(def cljs-level "cljsLevel") (def cljs-level "cljsLevel")
(def cljs-argv "cljsArgv")
(def React reactimport/React)
;;; Update batching ;;; Update batching
@ -14,7 +18,7 @@
(js/setTimeout f 16)) (js/setTimeout f 16))
(def next-tick (def next-tick
(if-not isClient (if-not is-client
fake-raf fake-raf
(let [w js/window] (let [w js/window]
(or (.-requestAnimationFrame w) (or (.-requestAnimationFrame w)
@ -92,6 +96,23 @@
;; Misc utilities ;; Misc utilities
(def dont-camel-case #{"aria" "data"})
(defn capitalize [s]
(if (< (count s) 2)
(string/upper-case s)
(str (string/upper-case (subs s 0 1)) (subs s 1))))
(defn dash-to-camel [dashed]
(if (string? dashed)
dashed
(let [name-str (name dashed)
[start & parts] (string/split name-str #"-")]
(if (dont-camel-case start)
name-str
(apply str start (map capitalize parts))))))
(deftype partial-ifn [f args ^:mutable p] (deftype partial-ifn [f args ^:mutable p]
IFn IFn
(-invoke [_ & a] (-invoke [_ & a]