Skip to content

Commit

Permalink
refactor deffactory macro
Browse files Browse the repository at this point in the history
  • Loading branch information
thheller committed Jun 6, 2017
1 parent bc72da5 commit 322d321
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 228 deletions.
13 changes: 9 additions & 4 deletions src/main/shadow/react/component.clj
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
(ns shadow.react.component)

;; wrapping everything in a delay so things can be removed by Closure if not used
(defmacro deffactory [name body]
(defmacro deffactory
[name & kv-pairs]
(assert (even? (count kv-pairs)))
(let [type
(keyword (str *ns*) (str name))

cmp
(-> (str name "$init")
(symbol)
(with-meta {:tag 'not-native}))]
(with-meta {:tag 'not-native}))

cmp-map
(-> (apply array-map kv-pairs)
(assoc ::type type))]

;; FIXME: not proper to introduce a new var but takes care of some DCE issues
`(do (def ~cmp
(-> ~body
(assoc :shadow.react.component/type ~type)
(-> ~cmp-map
(shadow.react.component/make-component)
(delay)))

Expand Down
19 changes: 9 additions & 10 deletions src/main/shadow/react/component.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -354,18 +354,17 @@


(defn make-component
[{::keys
[type
render
rewrite-props]
[{::keys [type mixins] :as config}]
{:pre [(keyword? type)]}

:as
config}]
;; FIXME: spec for config (must have type and render)
{:pre [(keyword? type)
(ifn? render)]}
(let [{::keys [type render rewrite-props] :as config}
(reduce
(fn [config mixin]
(mixin config))
config
mixins)

(let [component-fn
component-fn
(fn component-fn [props context updater]
;; CLJS/shadow/react/component.js:527: ERROR - incorrect use of goog.base: First argument must be 'this'.
;; goog.base(this$,props,context,updater);
Expand Down
23 changes: 10 additions & 13 deletions src/main/shadow/vault/context.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@
(:require [shadow.react.component :as comp :refer (deffactory)]))

(deffactory shadow-context
{::comp/type
::context
::comp/extend
#js {:getChildContext
(fn []
(this-as this
(let [ctx
(-> this (.-props) (comp/get-shadow-props) :context)]
#js {:shadow$context ctx})))}

::comp/extend
#js {:getChildContext
(fn []
(this-as this
(let [ctx
(-> this (.-props) (comp/get-shadow-props) :context)]
#js {:shadow$context ctx})))}

::comp/render
(fn [this]
(get-in this [:props :root]))})
::comp/render
(fn [this]
(get-in this [:props :root])))

162 changes: 82 additions & 80 deletions src/main/shadow/vault/devtools.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns shadow.vault.devtools
(:require [shadow.vault.store :as store :refer (defaction defkey)]
[shadow.vault.dom :as vdom]
[shadow.react.component :as comp]
[shadow.react.component :as comp :refer (deffactory)]
[shadow.markup.react :as html]
[shadow.markup.css :as css :refer (defstyled)]
[shadow.dom :as dom]
Expand Down Expand Up @@ -112,75 +112,76 @@
vault))

(def Inspector
{::comp/type
::inspector

::store/handlers
[inspector-handler]


::comp/will-mount
(fn [{::store/keys [vault] :as this}]
(store/transact! vault [(init)])
this)

::store/read
(fn [this vault props]
(let [{:keys [key] :as settings}
(get vault Settings)]
{:settings settings
:namespaces (get vault Namespaces)
:keys (get vault Keys)
:key-value (when key (get vault key))}))

::store/render
(fn [this vault props
{:keys
[settings
namespaces
keys
key-value]
:as data}]
(if-not (:show? settings)
(fab {:onClick
(fn [e]
(store/transact! vault [(toggle)]))}
"+")

(container {}
(html/div
{:onClick
)

(deffactory inspector
::comp/mixins
[store/mixin]

::comp/type
::inspector

::store/handlers
[inspector-handler]


::comp/will-mount
(fn [{::store/keys [vault] :as this}]
(store/transact! vault [(init)])
this)

::store/read
(fn [this vault props]
(let [{:keys [key] :as settings}
(get vault Settings)]
{:settings settings
:namespaces (get vault Namespaces)
:keys (get vault Keys)
:key-value (when key (get vault key))}))

::store/render
(fn [this vault props
{:keys
[settings
namespaces
keys
key-value]
:as data}]
(if-not (:show? settings)
(fab {:onClick
(fn [e]
(store/transact! vault [(toggle)]))}
(pr-str settings))

(browser {}
(ns-listing
(html/for [ns namespaces]
(html/div
{:onClick
(fn [e]
(store/transact! vault [(set-namespace ns)]))}
ns)))

(key-listing
(html/for [key keys]
(html/div
{:onClick
(fn [e]
(store/transact! vault [(set-key key)]))}
(let [{:keys [tag id]} key]
(pr-str [(name tag) id])))))

)
(when key-value
(render-object key-value))
)))})

(def inspector
(-> Inspector
(store/component)
(comp/factory)))
"+")

(container {}
(html/div
{:onClick
(fn [e]
(store/transact! vault [(toggle)]))}
(pr-str settings))

(browser {}
(ns-listing
(html/for [ns namespaces]
(html/div
{:onClick
(fn [e]
(store/transact! vault [(set-namespace ns)]))}
ns)))

(key-listing
(html/for [key keys]
(html/div
{:onClick
(fn [e]
(store/transact! vault [(set-key key)]))}
(let [{:keys [tag id]} key]
(pr-str [(name tag) id])))))

)
(when key-value
(render-object key-value))
))))

(deftype DevVault [delegate]
store/IBranch
Expand Down Expand Up @@ -326,21 +327,22 @@
))

(def Dump
{::comp/type
::dump
)

(def dump
::comp/mixins
[store/mixin]

::store/read
(fn [this vault {:keys [path] :as props}]
{:obj (get-in vault path)})
::comp/type
::dump

::comp/render
(fn [this]
(render-object (-> this :data :obj)))})
::store/read
(fn [this vault {:keys [path] :as props}]
{:obj (get-in vault path)})

(def dump
(-> Dump
(store/component)
(comp/factory)))
::comp/render
(fn [this]
(render-object (-> this :data :obj))))

(defn dump-into [host context path]
(vdom/mount host (dump {:path path}) context))
Expand Down
22 changes: 11 additions & 11 deletions src/main/shadow/vault/store.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@
(render this vault props data)
)))

(defn component
(defn mixin
[{::comp/keys
[render
will-receive-props
Expand Down Expand Up @@ -619,16 +619,16 @@
))

(deffactory root*
{::comp/constructor
(fn [{::comp/keys [context]
:keys [props]
:as this}
react]
(update this ::comp/context assoc ::vault (:vault props)))

::comp/render
(fn [this]
(get-in this [:props :root-el]))})
::comp/constructor
(fn [{::comp/keys [context]
:keys [props]
:as this}
react]
(update this ::comp/context assoc ::vault (:vault props)))

::comp/render
(fn [this]
(get-in this [:props :root-el])))

(defn root
"prefer to inject context directly"
Expand Down
Loading

0 comments on commit 322d321

Please sign in to comment.