Use interop macros everywhere, delete unnecessary hacks

This commit is contained in:
Dan Holmsand 2014-03-14 13:47:10 +01:00
parent 58dd46f4ae
commit 56e11f99c7
7 changed files with 30 additions and 64 deletions

View File

@ -1,12 +1,13 @@
(ns reagent.core (ns reagent.core
(:refer-clojure :exclude [partial atom flush]) (:refer-clojure :exclude [partial atom flush])
(:require-macros [reagent.debug :refer [dbg prn]])
(:require [reagent.impl.template :as tmpl] (:require [reagent.impl.template :as tmpl]
[reagent.impl.component :as comp] [reagent.impl.component :as comp]
[reagent.impl.util :as util] [reagent.impl.util :as util]
[reagent.impl.batching :as batch] [reagent.impl.batching :as batch]
[reagent.ratom :as ratom])) [reagent.ratom :as ratom]
[reagent.debug :refer-macros [dbg prn]]
[reagent.interop :refer-macros [set. get. call.]]))
(def React util/React) (def React util/React)
@ -27,17 +28,18 @@ Returns the mounted component instance."
([comp container] ([comp container]
(render-component comp container nil)) (render-component comp container nil))
([comp container callback] ([comp container callback]
(.renderComponent React (as-component comp) container callback))) (call. React :renderComponent
(as-component comp) container callback)))
(defn unmount-component-at-node (defn unmount-component-at-node
"Remove a component from the given DOM node." "Remove a component from the given DOM node."
[container] [container]
(.unmountComponentAtNode React container)) (call. React :unmountComponentAtNode container))
(defn render-component-to-string (defn render-component-to-string
"Turns a component into an HTML string." "Turns a component into an HTML string."
([component] ([component]
(.renderComponentToString React (as-component component)))) (call. React :renderComponentToString (as-component component))))
(defn create-class (defn create-class
"Create a component, React style. Should be called with a map, "Create a component, React style. Should be called with a map,
@ -107,7 +109,7 @@ Everything is optional, except :render.
(defn dom-node (defn dom-node
"Returns the root DOM node of a mounted component." "Returns the root DOM node of a mounted component."
[this] [this]
(.getDOMNode this)) (call. this :getDOMNode))
(defn merge-props (defn merge-props

View File

@ -5,7 +5,7 @@
(defmacro log (defmacro log
"Print with console.log, if it exists." "Print with console.log, if it exists."
[& forms] [& forms]
`(when (not (nil? (.-log js/console))) `(when (clojure.core/exists? js/console)
(.log js/console ~@forms))) (.log js/console ~@forms)))
(defmacro println (defmacro println

View File

@ -113,7 +113,7 @@
(defn dont-bind [f] (defn dont-bind [f]
(if (ifn? f) (if (ifn? f)
(doto f (doto f
(aset "__reactDontBind" true)) (set. :__reactDontBind true))
f)) f))
(defn get-wrapper [key f name] (defn get-wrapper [key f name]
@ -185,6 +185,6 @@
res (call. React :createClass spec) res (call. React :createClass spec)
f (fn [& args] f (fn [& args]
(as-component (apply vector res args)))] (as-component (apply vector res args)))]
(set! (.-cljsReactClass f) res) (util/cache-react-class f res)
(set! (.-cljsReactClass res) res) (util/cache-react-class res res)
f)) f))

View File

@ -1,24 +0,0 @@
(ns reagent.impl.reactimport
(:require-macros [reagent.impl.util :refer [import-js expose-vars]]))
;; (import-js "reagent/impl/react.min.js")
(def React js/React)
;; TODO: Check event names as well
(expose-vars [:createClass
:isValidClass
:setProps
:setState
:replaceState
:forceUpdate
:renderComponent
:unmountComponentAtNode
:renderComponentToString
:getDOMNode
:initializeTouchEvents
:addons
:TransitionGroup])

View File

@ -182,19 +182,19 @@
(let [spec (meta f) (let [spec (meta f)
withrender (assoc spec :component-function f) withrender (assoc spec :component-function f)
res (create-class withrender) res (create-class withrender)
wrapf (.-cljsReactClass res)] wrapf (util/cached-react-class res)]
(set! (.-cljsReactClass f) wrapf) (util/cache-react-class f wrapf)
wrapf)) wrapf))
(defn as-class [tag] (defn as-class [tag]
(if (hiccup-tag? tag) (if (hiccup-tag? tag)
(cached-wrapper tag) (cached-wrapper tag)
(do (do
(let [cached-class (.-cljsReactClass tag)] (let [cached-class (util/cached-react-class tag)]
(if-not (nil? cached-class) (if-not (nil? cached-class)
cached-class cached-class
(if (call. React :isValidClass tag) (if (call. React :isValidClass tag)
(set! (.-cljsReactClass tag) (wrap-component tag nil nil)) (util/cache-react-class tag (wrap-component tag nil nil))
(fn-to-class tag))))))) (fn-to-class tag)))))))
(defn get-key [x] (defn get-key [x]
@ -215,13 +215,13 @@
(set. jsprops :key k'))) (set. jsprops :key k')))
(c jsprops))) (c jsprops)))
(def tmp #js {}) (def seq-ctx #js {})
(defn warn-on-deref [x] (defn warn-on-deref [x]
(when-not (.-warned tmp) (when-not (get. seq-ctx :warned)
(log "Warning: Reactive deref not supported in seq in " (log "Warning: Reactive deref not supported in seq in "
(pr-str x)) (pr-str x))
(set! (.-warned tmp) true))) (set. seq-ctx :warned true)))
(declare expand-seq) (declare expand-seq)
@ -233,8 +233,8 @@
(expand-seq x level) (expand-seq x level)
(let [s (ratom/capture-derefed (let [s (ratom/capture-derefed
#(expand-seq x level) #(expand-seq x level)
tmp)] seq-ctx)]
(when (ratom/captured tmp) (when (ratom/captured seq-ctx)
(warn-on-deref x)) (warn-on-deref x))
s)) s))
true x))) true x)))

View File

@ -1,16 +0,0 @@
(ns reagent.impl.util
(:require [clojure.java.io :as io]
[clojure.string :as string]))
(defmacro import-js [srcfile]
(let [src# (slurp (io/resource srcfile))]
`(js/eval ~(str src#
"\n//@ sourceURL=" srcfile "\n"))))
(defmacro expose-vars [vars]
(let [exp# (map #(str "\n/** @expose */\nX." (name %) " = false;\n")
vars)]
(list 'js* (str "(function () {\nvar X = {};"
(apply str exp#)
"})"))))

View File

@ -1,13 +1,12 @@
(ns reagent.impl.util (ns reagent.impl.util
(:require [reagent.debug :refer-macros [dbg log]] (:require [reagent.debug :refer-macros [dbg log]]
[reagent.interop :refer-macros [get. set. call.]] [reagent.interop :refer-macros [get. set. call. jval]]
[reagent.impl.reactimport :as reactimport]
[clojure.string :as string])) [clojure.string :as string]))
(def is-client (not (nil? (try (.-document js/window) (def is-client (not (nil? (some-> (jval :window)
(catch js/Object e nil))))) (get. :document)))))
(def React reactimport/React) (def React js/React)
;;; Props accessors ;;; Props accessors
@ -33,6 +32,11 @@
(defn reagent-component? [c] (defn reagent-component? [c]
(-> (get. c [:props :argv]) nil? not)) (-> (get. c [:props :argv]) nil? not))
(defn cached-react-class [c]
(get. c :cljsReactClass))
(defn cache-react-class [c constructor]
(set. c :cljsReactClass constructor))
;; Misc utilities ;; Misc utilities