Skip to content

Commit ead7260

Browse files
committed
save
1 parent 3674d63 commit ead7260

10 files changed

Lines changed: 469 additions & 452 deletions

dist/react-lite.common.js

Lines changed: 150 additions & 151 deletions
Large diffs are not rendered by default.

dist/react-lite.js

Lines changed: 150 additions & 151 deletions
Large diffs are not rendered by default.

dist/react-lite.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/react-lite.min.js.gz

-20 Bytes
Binary file not shown.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-lite",
3-
"version": "0.0.26",
3+
"version": "0.0.27",
44
"description": "an implementation of React that optimizes for small script size",
55
"main": "dist/react-lite.common.js",
66
"jsnext:main": "src/index.js",

src/Component.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as _ from './util'
2-
import { renderComponent, clearPendingComponents } from './virtual-dom'
2+
import { renderComponent, clearPendingComponents, compareTwoTrees } from './virtual-dom'
33

44
export let updateQueue = {
55
updaters: [],
@@ -8,6 +8,9 @@ export let updateQueue = {
88
this.updaters.push(updater)
99
},
1010
batchUpdate() {
11+
if (this.isPending) {
12+
return
13+
}
1114
this.isPending = true
1215
/*
1316
each updater.update may add new updater to updateQueue
@@ -19,7 +22,7 @@ export let updateQueue = {
1922
let { updaters } = this
2023
let updater
2124
while (updater = updaters.pop()) {
22-
updater.update()
25+
updater.updateComponent()
2326
}
2427
this.isPending = false
2528
}
@@ -40,10 +43,10 @@ Updater.prototype = {
4043
this.nextContext = nextContext
4144
// receive nextProps!! should update immediately
4245
nextProps || !updateQueue.isPending
43-
? this.update()
46+
? this.updateComponent()
4447
: updateQueue.add(this)
4548
},
46-
update() {
49+
updateComponent() {
4750
let { instance, pendingStates, nextProps, nextContext } = this
4851
if (nextProps || pendingStates.length > 0) {
4952
nextProps = nextProps || instance.props
@@ -141,7 +144,7 @@ Component.prototype = {
141144
this.props = nextProps
142145
this.context = nextContext
143146
let nextVtree = renderComponent(this, parentContext)
144-
let newNode = vtree.updateTree(node, nextVtree, node.parentNode, nextVtree.context)
147+
let newNode = compareTwoTrees(vtree, nextVtree, node, node.parentNode, nextVtree.context)
145148
if (newNode !== node) {
146149
newNode.cache = newNode.cache || {}
147150
_.extend(newNode.cache, node.cache)

src/ReactDOM.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as _ from './util'
22
import { COMPONENT_ID, VNODE_TYPE, TRUE } from './constant'
3-
import { clearPendingComponents } from './virtual-dom'
4-
import { updateQueue } from './Component'
3+
import { clearPendingComponents, compareTwoTrees } from './virtual-dom'
4+
import { updateQueue } from './Component'
55

66
let pendingRendering = {}
77
let vtreeStore = {}
@@ -29,14 +29,15 @@ let renderTreeIntoContainer = (vtree, container, callback, parentContext) => {
2929

3030
pendingRendering[id] = TRUE
3131
if (vtreeStore[id]) {
32-
vtreeStore[id].updateTree(container.firstChild, vtree, container, parentContext)
32+
compareTwoTrees(vtreeStore[id], vtree, container.firstChild, container, parentContext)
3333
} else {
3434
container.innerHTML = ''
3535
vtree.initTree(container, parentContext)
3636
}
3737
vtreeStore[id] = vtree
38+
let isPending = updateQueue.isPending
3839
updateQueue.isPending = true
39-
clearPendingComponents()
40+
clearPendingComponents(true)
4041
argsCache = pendingRendering[id]
4142
delete pendingRendering[id]
4243

@@ -48,8 +49,11 @@ let renderTreeIntoContainer = (vtree, container, callback, parentContext) => {
4849
} else if (vtree.vtype === VNODE_TYPE.COMPONENT) {
4950
result = container.firstChild.cache[vtree.id]
5051
}
51-
52-
updateQueue.batchUpdate()
52+
53+
if (!isPending) {
54+
updateQueue.isPending = false
55+
updateQueue.batchUpdate()
56+
}
5357

5458
if (callback) {
5559
callback.call(result)
@@ -58,6 +62,10 @@ let renderTreeIntoContainer = (vtree, container, callback, parentContext) => {
5862
return result
5963
}
6064

65+
let updateComponents = component => {
66+
component.$updater.updateComponent()
67+
}
68+
6169
export let render = (vtree, container, callback) => {
6270
return renderTreeIntoContainer(vtree, container, callback)
6371
}

src/event-system.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ let dispatchEvent = event => {
6666
}
6767
target = target.parentNode
6868
}
69+
updateQueue.isPending = false
6970
updateQueue.batchUpdate()
7071
}
7172

src/util.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,22 @@ export let pipe = (fn1, fn2) => {
2828
}
2929
}
3030

31-
export let flattenChildren = (list, iteratee) => flat(list, iteratee, [])
32-
33-
let flat = (list, iteratee, res) => {
31+
export let flattenChildren = (list, iteratee, record) => {
3432
let len = list.length
3533
let i = -1
34+
record = record || []
3635

3736
while (len--) {
38-
let cur = list[++i]
39-
if (isArr(cur)) {
40-
flat(cur, iteratee, res)
41-
} else if (!isUndefined(cur) && !isBln(cur)) {
42-
res.push(iteratee(cur, res.length) || cur)
37+
let item = list[++i]
38+
if (isArr(item)) {
39+
flattenChildren(item, iteratee, record)
40+
} else if (!isUndefined(item) && !isBln(item)) {
41+
record.push(iteratee(item, record.length) || item)
4342
}
4443
}
45-
return res
44+
return record
4645
}
4746

48-
4947
export let eachItem = (list, iteratee) => {
5048
for (let i = 0, len = list.length; i < len; i++) {
5149
iteratee(list[i], i)

0 commit comments

Comments
 (0)