Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multiple context types mixins #137

Open
wants to merge 2 commits into
base: gh-pages
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/rum/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
[comp]
(aget (.-state comp) ":rum/state"))

(defn- assert-no-overrides [maps name]
(let [conflicting-keys (->> (mapcat keys maps)
frequencies
(filter (fn [[k v]] (> v 1)))
(map (fn [[k _]] k)))]
(when-not (empty? conflicting-keys)
(throw (js/Error. (str "Conflicting " name " in mixins for keys: " conflicting-keys))))))

(defn- build-class [render mixins display-name]
(let [init (collect :init mixins) ;; state props -> state
Expand All @@ -33,6 +40,11 @@
:after-render] mixins) ;; state -> state
will-unmount (collect :will-unmount mixins) ;; state -> state
child-context (collect :child-context mixins) ;; state -> child-context

;; Maps of keys to React.PropTypes
child-context-types (collect :child-context-types mixins) ;; when injecting context
context-types (collect :context-types mixins) ;; when consuming context

class-props (reduce merge (collect :class-properties mixins))] ;; custom properties and methods

(-> {:displayName display-name
Expand Down Expand Up @@ -98,7 +110,16 @@
(fn []
(this-as this
(let [state @(state this)]
(clj->js (transduce (map #(% state)) merge {} child-context))))))}
(clj->js (transduce (map #(% state)) merge {} child-context))))))
:contextTypes
(when-not (empty? context-types)
(assert-no-overrides context-types "context-types")
(clj->js (apply merge context-types)))
:childContextTypes
(when-not (empty? child-context-types)
(assert-no-overrides child-context-types "child-context-types")
(clj->js (apply merge child-context-types)))}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a merge here that warns (or throws even) on conflicts would be good I think. This way using 3rd party mixins is "safe" and free from surprises.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you are right, it should probably throw in there. If the user really wants to override the context types put there by mixins, he can still do it with a {:class-properties {:contextTypes ... }} map that will override everything.

Also, thanks a lot for the derivatives library!

(merge class-props)
(->> (util/filter-vals some?))
(clj->js)
Expand Down