Last active
June 26, 2018 22:37
-
-
Save jjttjj/64933897fd45985a3642980fda57fac5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Unstaged | |
modified src/hoplon/core.cljs | |
@@ -23,6 +23,8 @@ | |
(enable-console-print!) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Declare Variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
(declare elem! do! on! ->node $text add-children! attribute?) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
@@ -33,22 +35,24 @@ | |
(let [x (.-childNodes this)] | |
(areduce x i ret [] (conj ret (.item x i))))) | |
+(defn- child-array [elem] (into-array (array-seq (.-childNodes elem)))) | |
+ | |
(defn- vflatten | |
"Takes a sequential collection and returns a flattened vector of any nested | |
sequential collections." | |
- ([x] (vflatten [] x)) | |
- ([acc x] (if (sequential? x) (reduce vflatten acc x) (conj acc x)))) | |
+ ([x] (vflatten (array) x)) | |
+ ([acc x] | |
+ (if (or (sequential? x) (array? x)) (reduce vflatten acc x) (doto acc (.push x))))) | |
(defn- remove-nil [nodes] | |
- (reduce #(if %2 (conj %1 %2) %1) [] nodes)) | |
+ ) | |
(defn- compact-kids | |
"Flattens nested sequencences of elements, removing nil values." | |
[kids] | |
(->> | |
(vflatten kids) | |
- (remove-nil) | |
- (mapv ->node))) | |
+ (reduce #(if %2 (doto %1 (.push (->node %2))) %1) (array)))) | |
(defn- set-dom-children! | |
"Sets a DOM element's children to the sequence of children given." | |
@@ -56,7 +60,7 @@ | |
(let [new-kids (compact-kids new-kids) | |
new? (set new-kids)] | |
(loop [[new-kid & nks] new-kids | |
- [old-kid & oks :as old-kids] (child-vec elem)] | |
+ [old-kid & oks :as old-kids] (child-array elem)] | |
(when (or new-kid old-kid) | |
(cond | |
(= new-kid old-kid) (recur nks oks) | |
@@ -275,37 +279,51 @@ | |
(-hoplon-kids | |
([this] | |
(if-let [hl-kids (.-hoplonKids this)] hl-kids | |
- (with-let [kids (atom (child-vec this))] | |
+ (with-let [kids (atom (child-array this))] | |
(set! (.-hoplonKids this) kids) | |
- (do-watch kids #(set-dom-children! this %2)))))) | |
+ (do-watch kids #(when (not (array? %2)) | |
+ (set-dom-children! this %2))))))) | |
(-append-child! | |
([this child] | |
(with-let [child child] | |
(let [kids (-hoplon-kids this) | |
+ _ (js/console.log "kids" @kids) | |
i (count @kids)] | |
(if (cell? child) | |
- (do-watch child #(swap! kids assoc i %2)) | |
- (swap! kids assoc i child)))))) | |
+ (do-watch child #(swap! kids (fn [kids-arr] (doto kids-arr (aset i %2))))) | |
+ (swap! kids aset i child)))))) | |
(-remove-child! | |
([this child] | |
(with-let [child child] | |
(let [kids (-hoplon-kids this) | |
before-count (count @kids)] | |
(if (cell? child) | |
- (swap! kids #(vec (remove (partial = @child) %))) | |
- (swap! kids #(vec (remove (partial = child) %)))) | |
+ (swap! kids #(reduce (fn [acc x] (if (= child @x) acc (doto acc (.push x)))) %)) | |
+ (swap! kids #(reduce (fn [acc x] (if (= child x) acc (doto acc (.push x)))) %))) | |
(when-not (= (count @kids) (dec before-count)) | |
(throw (js/Error. "Attempted to remove a node that is not a child of parent."))))))) | |
(-replace-child! | |
([this new existing] | |
(with-let [existing existing] | |
- (swap! (-hoplon-kids this) #(mapv (fn [el] (if (= el existing) new el)) %))))) | |
+ (swap! (-hoplon-kids this) #(reduce (fn [el acc] | |
+ (doto acc | |
+ (.push (if (= el existing) new el)))) (array) %))))) | |
(-insert-before! | |
([this new existing] | |
(with-let [new new] | |
(cond | |
- (not existing) (swap! (-hoplon-kids this) conj new) | |
- (not= new existing) (swap! (-hoplon-kids this) #(vec (mapcat (fn [el] (if (= el existing) [new el] [el])) %))))))))) | |
+ (not existing) (swap! (-hoplon-kids this) #(doto % (.push new))) | |
+ (not= new existing) (swap! (-hoplon-kids this) | |
+ #(reduce (fn [el acc] | |
+ (if (= el existing) | |
+ (doto acc (.push new el)) | |
+ (doto acc (.push el)))) | |
+ (array) | |
+ %) | |
+ | |
+ | |
+ ;;#(vec (mapcat (fn [el] (if (= el existing) [new el] [el])) %)) | |
+ ))))))) | |
(defn ->hoplon [elem] | |
(if (element? elem) elem | |
@@ -352,7 +370,7 @@ | |
(-append-child! this x))))) | |
(defn- remove-all-kids! [this] | |
- (swap! (-hoplon-kids this) empty)) | |
+ (reset! (-hoplon-kids this) (array))) | |
(defn- invoke! | |
[this & args] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment