Skip to content

Commit

Permalink
turn DOM ops into numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyong committed Dec 22, 2017
1 parent 81c422e commit fe99c67
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 48 deletions.
2 changes: 1 addition & 1 deletion coir.edn

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"dependencies": {},
"devDependencies": {
"http-server": "^0.10.0",
"shadow-cljs": "^2.0.117",
"shadow-cljs": "^2.0.122",
"source-map-support": "^0.5.0",
"ws": "^3.3.2"
"ws": "^3.3.3"
}
}
43 changes: 23 additions & 20 deletions src/respo/render/diff.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
(:require [clojure.string :as string]
[respo.util.format :refer [purify-element]]
[respo.util.detect :refer [component?]]
[clojure.set :refer [difference]]))
[clojure.set :refer [difference]]
[respo.schema.op :as op]))

(declare find-element-diffs)

Expand All @@ -17,11 +18,11 @@
(and was-empty? now-empty?) nil
(and was-empty? (not now-empty?))
(let [entry (first new-style), follows (rest new-style)]
(collect! [:add-style coord entry])
(collect! [op/add-style coord entry])
(recur collect! coord old-style follows))
(and (not was-empty?) now-empty?)
(let [entry (first old-style), follows (rest old-style)]
(collect! [:rm-style coord (key entry)])
(collect! [op/rm-style coord (key entry)])
(recur collect! coord follows new-style))
:else
(let [old-entry (first old-style)
Expand All @@ -31,15 +32,15 @@
(case (compare (key old-entry) (key new-entry))
-1
(do
(collect! [:rm-style coord (key old-entry)])
(collect! [op/rm-style coord (key old-entry)])
(recur collect! coord old-follows new-style))
1
(do
(collect! [:add-style coord new-entry])
(collect! [op/add-style coord new-entry])
(recur collect! coord old-style new-follows))
(do
(if (not (identical? (val old-entry) (val new-entry)))
(collect! [:replace-style coord new-entry]))
(collect! [op/replace-style coord new-entry]))
(recur collect! coord old-follows new-follows))))))))

(defn find-props-diffs [collect! coord old-props new-props]
Expand All @@ -57,11 +58,11 @@
(and was-empty? now-empty?) nil
(and was-empty? (not now-empty?))
(do
(collect! [:add-prop coord (first new-props)])
(collect! [op/add-prop coord (first new-props)])
(recur collect! coord old-props (rest new-props)))
(and (not was-empty?) now-empty?)
(do
(collect! [:rm-prop coord (key (first old-props))])
(collect! [op/rm-prop coord (key (first old-props))])
(recur collect! coord (rest old-props) new-props))
:else
(let [old-entry (first old-props)
Expand All @@ -74,14 +75,14 @@
(case (compare old-k new-k)
-1
(do
(collect! [:rm-prop coord old-k])
(collect! [op/rm-prop coord old-k])
(recur collect! coord old-follows new-props))
1
(do
(collect! [:add-prop coord new-entry])
(collect! [op/add-prop coord new-entry])
(recur collect! coord old-props new-follows))
(do
(if (not= old-v new-v) (collect! [:replace-prop coord new-entry]))
(if (not= old-v new-v) (collect! [op/replace-prop coord new-entry]))
(recur collect! coord old-follows new-follows)))))))

(defn find-children-diffs [collect! n-coord index old-children new-children]
Expand All @@ -91,11 +92,11 @@
(and was-empty? now-empty?) nil
(and was-empty? (not now-empty?))
(let [element (last (first new-children))]
(collect! [:append n-coord (purify-element element)])
(collect! [op/append-element n-coord (purify-element element)])
(recur collect! n-coord (inc index) [] (rest new-children)))
(and (not was-empty?) now-empty?)
(do
(collect! [:rm (conj n-coord index)])
(collect! [op/rm-element (conj n-coord index)])
(recur collect! n-coord index (rest old-children) []))
:else
(let [old-keys (map first (take 16 old-children))
Expand All @@ -119,11 +120,11 @@
(do
(collect!
(let [element (last (first new-children))]
[:add (conj n-coord index) (purify-element element)]))
[op/add-element (conj n-coord index) (purify-element element)]))
(recur collect! n-coord (inc index) old-children new-follows))
(and (not x1-remains?) y1-existed?)
(do
(collect! [:rm (conj n-coord index)])
(collect! [op/rm-element (conj n-coord index)])
(recur collect! n-coord index old-follows new-children))
:else
(let [xi (.indexOf new-keys x1)
Expand All @@ -133,10 +134,11 @@
(comment println "index:" xi yi)
(if (<= xi yi)
(let [new-element (last (first new-children))]
(collect! [:add (conj n-coord index) (purify-element new-element)])
(collect!
[op/add-element (conj n-coord index) (purify-element new-element)])
(recur collect! n-coord (inc index) old-children new-follows))
(do
(collect! [:rm (conj n-coord index)])
(collect! [op/rm-element (conj n-coord index)])
(recur collect! n-coord index old-follows new-children)))))))))

(defn find-element-diffs [collect! n-coord old-tree new-tree]
Expand All @@ -151,7 +153,7 @@
(if (or (not= (:coord old-tree) (:coord new-tree))
(not= (:name old-tree) (:name new-tree))
(not= (:c-name old-tree) (:c-name new-tree)))
(do (collect! [:replace n-coord (purify-element new-tree)]) nil)
(do (collect! [op/replace-element n-coord (purify-element new-tree)]) nil)
(do
(find-props-diffs collect! n-coord (:attrs old-tree) (:attrs new-tree))
(let [old-style (:style old-tree), new-style (:style new-tree)]
Expand All @@ -162,6 +164,7 @@
added-events (difference new-events old-events)
removed-events (difference old-events new-events)]
(doseq [event-name added-events]
(collect! [:add-event n-coord [event-name (:coord new-tree)]]))
(doseq [event-name removed-events] (collect! [:rm-event n-coord event-name])))
(collect! [op/set-event n-coord [event-name (:coord new-tree)]]))
(doseq [event-name removed-events]
(collect! [op/rm-event n-coord event-name])))
(find-children-diffs collect! n-coord 0 old-children new-children)))))))
31 changes: 16 additions & 15 deletions src/respo/render/patch.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
(ns respo.render.patch
(:require [clojure.string :as string]
[respo.util.format :refer [dashed->camel event->prop ensure-string]]
[respo.render.dom :refer [make-element style->string]]))
[respo.render.dom :refer [make-element style->string]]
[respo.schema.op :as op]))

(defn rm-event [target event-name]
(let [event-prop (event->prop event-name)] (aset target event-prop nil)))
Expand Down Expand Up @@ -69,17 +70,17 @@
(doseq [op changes]
(let [[op-type coord op-data] op, target (find-target root coord)]
(comment println op-type target op-data)
(case op-type
:replace-prop (replace-prop target op-data)
:add-prop (add-prop target op-data)
:rm-prop (rm-prop target op-data)
:add-style (add-style target op-data)
:replace-style (replace-style target op-data)
:rm-style (rm-style target op-data)
:add-event (add-event target op-data listener-builder)
:rm-event (rm-event target op-data)
:add (add-element target op-data listener-builder)
:rm (rm-element target op-data)
:replace (replace-element target op-data listener-builder)
:append (append-element target op-data listener-builder)
(println "not implemented:" op-type))))))
(cond
(= op-type op/replace-prop) (replace-prop target op-data)
(= op-type op/add-prop) (add-prop target op-data)
(= op-type op/rm-prop) (rm-prop target op-data)
(= op-type op/add-style) (add-style target op-data)
(= op-type op/replace-style) (replace-style target op-data)
(= op-type op/rm-style) (rm-style target op-data)
(= op-type op/set-event) (add-event target op-data listener-builder)
(= op-type op/rm-event) (rm-event target op-data)
(= op-type op/add-element) (add-element target op-data listener-builder)
(= op-type op/rm-element) (rm-element target op-data)
(= op-type op/replace-element) (replace-element target op-data listener-builder)
(= op-type op/append-element) (append-element target op-data listener-builder)
:else (println "not implemented:" op-type))))))
26 changes: 26 additions & 0 deletions src/respo/schema/op.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

(ns respo.schema.op )

(def rm-event 32)

(def replace-style 21)

(def replace-element 1)

(def append-element 3)

(def set-event 30)

(def rm-prop 12)

(def add-prop 10)

(def replace-prop 11)

(def add-style 20)

(def rm-style 22)

(def rm-element 2)

(def add-element 0)
20 changes: 10 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1190,20 +1190,20 @@ sha.js@^2.4.0, sha.js@^2.4.8:
inherits "^2.0.1"
safe-buffer "^5.0.1"

[email protected].0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/shadow-cljs-jar/-/shadow-cljs-jar-1.1.0.tgz#17fee311c5832b12eec65072b5d265f1c35ff67a"
[email protected].1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/shadow-cljs-jar/-/shadow-cljs-jar-1.1.1.tgz#5d74383f5aff8fba25a5d00ee3c3ad035a1bd7a3"

shadow-cljs@^2.0.102:
version "2.0.102"
resolved "https://registry.yarnpkg.com/shadow-cljs/-/shadow-cljs-2.0.102.tgz#d2c4ae3a8d5250759d9c45ce4937450fc189fe9d"
shadow-cljs@^2.0.122:
version "2.0.122"
resolved "https://registry.yarnpkg.com/shadow-cljs/-/shadow-cljs-2.0.122.tgz#85c5e1a5dfdedb10b68e7ce68135e33468bb45a1"
dependencies:
babel-core "^6.26.0"
babel-preset-env "^1.6.0"
mkdirp "^0.5.1"
node-libs-browser "^2.0.0"
readline-sync "^1.4.7"
shadow-cljs-jar "1.1.0"
shadow-cljs-jar "1.1.1"
signal-exit "^3.0.2"
source-map-support "^0.4.15"
ws "^3.0.0"
Expand Down Expand Up @@ -1344,9 +1344,9 @@ ws@^3.0.0:
safe-buffer "~5.1.0"
ultron "~1.1.0"

ws@^3.3.2:
version "3.3.2"
resolved "https://registry.yarnpkg.com/ws/-/ws-3.3.2.tgz#96c1d08b3fefda1d5c1e33700d3bfaa9be2d5608"
ws@^3.3.3:
version "3.3.3"
resolved "https://registry.yarnpkg.com/ws/-/ws-3.3.3.tgz#f1cf84fe2d5e901ebce94efaece785f187a228f2"
dependencies:
async-limiter "~1.0.0"
safe-buffer "~5.1.0"
Expand Down

0 comments on commit fe99c67

Please sign in to comment.