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

View File

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

View File

@ -113,7 +113,7 @@
(defn dont-bind [f]
(if (ifn? f)
(doto f
(aset "__reactDontBind" true))
(set. :__reactDontBind true))
f))
(defn get-wrapper [key f name]
@ -185,6 +185,6 @@
res (call. React :createClass spec)
f (fn [& args]
(as-component (apply vector res args)))]
(set! (.-cljsReactClass f) res)
(set! (.-cljsReactClass res) res)
(util/cache-react-class f res)
(util/cache-react-class res res)
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)
withrender (assoc spec :component-function f)
res (create-class withrender)
wrapf (.-cljsReactClass res)]
(set! (.-cljsReactClass f) wrapf)
wrapf (util/cached-react-class res)]
(util/cache-react-class f wrapf)
wrapf))
(defn as-class [tag]
(if (hiccup-tag? tag)
(cached-wrapper tag)
(do
(let [cached-class (.-cljsReactClass tag)]
(let [cached-class (util/cached-react-class tag)]
(if-not (nil? cached-class)
cached-class
(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)))))))
(defn get-key [x]
@ -215,13 +215,13 @@
(set. jsprops :key k')))
(c jsprops)))
(def tmp #js {})
(def seq-ctx #js {})
(defn warn-on-deref [x]
(when-not (.-warned tmp)
(when-not (get. seq-ctx :warned)
(log "Warning: Reactive deref not supported in seq in "
(pr-str x))
(set! (.-warned tmp) true)))
(set. seq-ctx :warned true)))
(declare expand-seq)
@ -233,8 +233,8 @@
(expand-seq x level)
(let [s (ratom/capture-derefed
#(expand-seq x level)
tmp)]
(when (ratom/captured tmp)
seq-ctx)]
(when (ratom/captured seq-ctx)
(warn-on-deref x))
s))
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
(:require [reagent.debug :refer-macros [dbg log]]
[reagent.interop :refer-macros [get. set. call.]]
[reagent.impl.reactimport :as reactimport]
[reagent.interop :refer-macros [get. set. call. jval]]
[clojure.string :as string]))
(def is-client (not (nil? (try (.-document js/window)
(catch js/Object e nil)))))
(def is-client (not (nil? (some-> (jval :window)
(get. :document)))))
(def React reactimport/React)
(def React js/React)
;;; Props accessors
@ -33,6 +32,11 @@
(defn reagent-component? [c]
(-> (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