Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

5-10% speed improvement with protocols

Signed-off-by: Chris Granger <ibdknox@gmail.com>
  • Loading branch information...
commit 16f285bacaf96a7ffbcb8c31d3375adf69788399 1 parent 8e04f14
@ibdknox ibdknox authored
Showing with 25 additions and 20 deletions.
  1. +25 −20 src/dommy/template.cljs
View
45 src/dommy/template.cljs
@@ -1,6 +1,9 @@
(ns dommy.template
(:require [clojure.string :as str]))
+(defprotocol element
+ (-elem [this] "return the element representation of this"))
+
(defn add-class! [node c]
(.setAttribute node "class"
(if-let [cur-c (.getAttribute node "class")]
@@ -54,14 +57,6 @@
(recur (.substring str next-idx))))))
node))
-(defn element? [data]
- (or (keyword? data)
- (and (coll? data) (keyword? (first data)))
- (instance? js/HTMLElement data)))
-
-(defn node? [data]
- (or (element? data) (string? data) (number? data) (instance? js/Text data)))
-
(declare node)
(defn compound-element
@@ -71,26 +66,36 @@
attrs (when (map? (second data)) (second data))
tail (drop (if attrs 2 1) data)
;; Remove one level of nesting for cases like [:div [[:span][:span]]]
- tail (mapcat (fn [group] (if (node? group) [group] group)) tail)]
+ tail (mapcat (fn [group] (if (satisfies? element group) [group] group)) tail)]
(when attrs
(add-attrs! n attrs))
(doseq [child tail]
(.appendChild n (node child)))
n))
-(defn element [data]
- (cond
- (keyword? data) (base-element data)
- (and (coll? data) (keyword? (first data))) (compound-element data)
- (instance? js/HTMLElement data) data
- :else (throw (str "Don't know how to make element from " (pr-str data)))))
+(extend-protocol element
+ js/HTMLElement
+ (-elem [this] this)
+
+ Keyword
@mpenet
mpenet added a note

I dont think this would work, keywords are just strings in cljs at the moment.

an old gist that shows the issue: https://gist.github.com/4104635

edit: oh nevermind, I just noticed you fixed that :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ (-elem [this] (base-element this))
+
+ PersistentVector
+ (-elem [this] (compound-element this))
+
+ js/Text
+ (-elem [this] this)
+
+ number
+ (-elem [this] (.createTextNode js/document (str this)))
+
+ string
+ (-elem [this] (.createTextNode js/document (str this))))
(defn node [data]
- (cond
- (element? data) (element data)
- (or (number? data) (string? data)) (.createTextNode js/document (str data))
- (instance? js/Text data) data
- :else (throw (str "Don't know how to make node from " (pr-str data)))))
+ (if (satisfies? element data)
+ (-elem data)
+ (throw (str "Don't know how to make node from " (pr-str data)))))
(defn html->nodes [html]
(let [parent (.createElement js/document "div")]
@mpenet

I dont think this would work, keywords are just strings in cljs at the moment.

an old gist that shows the issue: https://gist.github.com/4104635

edit: oh nevermind, I just noticed you fixed that :)

Please sign in to comment.
Something went wrong with that request. Please try again.