From c18b8c318e57d6780965cd1754c3939c9b5e1c6b Mon Sep 17 00:00:00 2001 From: Dan Holmsand Date: Tue, 11 Feb 2014 15:08:50 +0100 Subject: [PATCH] Get rid of pesky circular dependency --- src/reagent/core.cljs | 6 +++--- src/reagent/impl/component.cljs | 24 +++++++++++++----------- src/reagent/impl/template.cljs | 32 ++++++-------------------------- src/reagent/impl/util.cljs | 29 +++++++++++++++++++++++++---- 4 files changed, 47 insertions(+), 44 deletions(-) diff --git a/src/reagent/core.cljs b/src/reagent/core.cljs index a7d440e..752f450 100644 --- a/src/reagent/core.cljs +++ b/src/reagent/core.cljs @@ -7,9 +7,9 @@ [reagent.impl.util :as util] [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 "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. " [spec] - (comp/create-class spec)) + (comp/create-class spec tmpl/as-component)) (defn current-component [] diff --git a/src/reagent/impl/component.cljs b/src/reagent/impl/component.cljs index 4f4d5a8..e72302a 100644 --- a/src/reagent/impl/component.cljs +++ b/src/reagent/impl/component.cljs @@ -1,8 +1,6 @@ (ns reagent.impl.component - (:require [reagent.impl.template :as tmpl - :refer [cljs-argv cljs-level React]] - [reagent.impl.util :as util :refer [cljs-level]] + (:require [reagent.impl.util :as util :refer [cljs-level cljs-argv React]] [reagent.ratom :as ratom] [reagent.debug :refer-macros [dbg prn]])) @@ -70,7 +68,7 @@ 5 (f (argv 1) (argv 2) (argv 3) (argv 4)) (apply f (subvec argv 1)))))] (if (vector? res) - (tmpl/as-component res (aget p cljs-level)) + (.asComponent C res (aget p cljs-level)) (if (ifn? res) (do (aset C cljs-render res) @@ -134,12 +132,15 @@ (this-as C (apply f C args))) 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] (if (dont-wrap key) - (doto f - (aset "__reactDontBind" true)) + (dont-bind f) (let [wrap (custom-wrapper key f)] (when (and wrap f) (assert (ifn? f) @@ -151,7 +152,7 @@ (defn camelify-map-keys [fun-map] (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)) (defn add-obligatory [fun-map] @@ -160,7 +161,7 @@ (defn add-render [fun-map render-f] (assoc fun-map :cljsRender render-f - :render (if util/isClient + :render (if util/is-client (fn [] (this-as C (util/run-reactively C #(do-render C)))) @@ -197,12 +198,13 @@ map-to-js)) (defn create-class - [body] + [body as-component] (assert (map? body)) (let [spec (cljsify body) + _ (set! (.-asComponent spec) (dont-bind as-component)) res (.createClass React spec) f (fn [& args] - (tmpl/as-component (apply vector res args)))] + (as-component (apply vector res args)))] (set! (.-cljsReactClass f) res) (set! (.-cljsReactClass res) res) f)) diff --git a/src/reagent/impl/template.cljs b/src/reagent/impl/template.cljs index 15de24d..c618e48 100644 --- a/src/reagent/impl/template.cljs +++ b/src/reagent/impl/template.cljs @@ -1,22 +1,16 @@ (ns reagent.impl.template (:require [clojure.string :as string] - [reagent.impl.reactimport :as reactimport] - [reagent.impl.util :as util :refer [cljs-level]] + [reagent.impl.util :as util + :refer [cljs-level cljs-argv is-client React]] + [reagent.impl.component :as comp] [reagent.ratom :as ratom] [reagent.debug :refer-macros [dbg prn println log]])) -(def React reactimport/React) (def debug false) (assert (set! debug true)) -(def cljs-argv "cljsArgv") - -(def isClient util/isClient) - -(def dont-camel-case #{"aria" "data"}) - (defn hiccup-tag? [x] (or (keyword? x) (symbol? x) @@ -26,30 +20,16 @@ (or (hiccup-tag? 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" :for "htmlFor" :charset "charSet"}) (defn undash-prop-name [n] (or (attr-aliases n) - (dash-to-camel n))) + (util/dash-to-camel n))) (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] (if-not (ifn? v) @@ -198,7 +178,7 @@ (defn fn-to-class [f] (let [spec (meta f) withrender (assoc spec :component-function f) - res (reagent.core/create-class withrender) + res (comp/create-class withrender as-component) wrapf (.-cljsReactClass res)] (set! (.-cljsReactClass f) wrapf) wrapf)) diff --git a/src/reagent/impl/util.cljs b/src/reagent/impl/util.cljs index ab31cde..88aa1ed 100644 --- a/src/reagent/impl/util.cljs +++ b/src/reagent/impl/util.cljs @@ -1,12 +1,16 @@ (ns reagent.impl.util (:refer-clojure :exclude [flush]) (: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) - (catch js/Object e nil))))) +(def is-client (not (nil? (try (.-document js/window) + (catch js/Object e nil))))) (def cljs-level "cljsLevel") +(def cljs-argv "cljsArgv") +(def React reactimport/React) ;;; Update batching @@ -14,7 +18,7 @@ (js/setTimeout f 16)) (def next-tick - (if-not isClient + (if-not is-client fake-raf (let [w js/window] (or (.-requestAnimationFrame w) @@ -92,6 +96,23 @@ ;; 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] IFn (-invoke [_ & a]