Skip to content

Commit bae8f9b

Browse files
authored
Merge pull request #360 from frejs/simplify
continue simplify
2 parents dbb4bd8 + 4d12b92 commit bae8f9b

File tree

3 files changed

+11
-33
lines changed

3 files changed

+11
-33
lines changed

demo/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</head>
1111
<body>
1212
<div id="app"></div>
13-
<script type="module" src="/src/ref.tsx"></script>
13+
<script type="module" src="/src/keys.tsx"></script>
1414
<script>
1515
</script>
1616
</body>

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fre",
3-
"version": "2.6.2",
3+
"version": "2.6.3",
44
"type": "module",
55
"main": "dist/fre.js",
66
"unpkg": "dist/fre.umd.js",

src/reconcile.ts

+9-31
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ const getParentNode = (fiber: IFiber): HTMLElement | undefined => {
130130
const diffKids = (fiber: any, children: FreNode): void => {
131131
let aCh = fiber.kids || [],
132132
bCh = (fiber.kids = arrayfy(children) as any)
133-
const actions = idiff(aCh, bCh)
133+
const actions = diff(aCh, bCh)
134134

135135
for (let i = 0, prev = null, len = bCh.length; i < len; i++) {
136136

@@ -165,13 +165,11 @@ const side = (effects: IEffect[]): void => {
165165
effects.length = 0
166166
}
167167

168-
const diff = function (opts) {
168+
const diff = function (a, b) {
169169
var actions = [],
170170
aIdx = {},
171171
bIdx = {},
172-
a = opts.old,
173-
b = opts.cur,
174-
key = opts.key,
172+
key = v => v.key + v.type,
175173
i, j;
176174
for (i = 0; i < a.length; i++) {
177175
aIdx[key(a[i])] = i;
@@ -184,27 +182,27 @@ const diff = function (opts) {
184182
if (aElm === null) {
185183
i++;
186184
} else if (b.length <= j) {
187-
opts.remove(i)
185+
removeElement(a[i])
188186
i++;
189187
} else if (a.length <= i) {
190-
opts.add(bElm, i)
188+
actions.push({ op: TAG.INSERT, elm: bElm, before: a[i] })
191189
j++;
192190
} else if (key(aElm) === key(bElm)) {
193191
clone(aElm, bElm)
194-
opts.update(aElm, bElm)
192+
actions.push({ op: TAG.UPDATE })
195193
i++; j++;
196194
} else {
197195
var curElmInNew = bIdx[key(aElm)]
198196
var wantedElmInOld = aIdx[key(bElm)]
199197
if (curElmInNew === undefined) {
200-
opts.remove(i);
198+
removeElement(a[i])
201199
i++;
202200
} else if (wantedElmInOld === undefined) {
203-
opts.add(bElm, i)
201+
actions.push({ op: TAG.INSERT, elm: bElm, before: a[i] })
204202
j++
205203
} else {
206204
clone(a[wantedElmInOld], bElm)
207-
opts.move(wantedElmInOld, i)
205+
actions.push({ op: TAG.MOVE, elm: a[wantedElmInOld], before: a[i] })
208206
a[wantedElmInOld] = null
209207
j++
210208
}
@@ -213,26 +211,6 @@ const diff = function (opts) {
213211
return actions
214212
}
215213

216-
var idiff = function (a, b) {
217-
var actions = [] // 必须保持和 b 一致的顺序
218-
var extr = (v) => v.key + v.type
219-
var update = (a, b) => actions.push({ op: TAG.UPDATE })
220-
var move = (from, to) => actions.push({ op: TAG.MOVE, elm: a[from], before: a[to] })
221-
var add = (elm, i) => actions.push({ op: TAG.INSERT, elm: elm, before: a[i] })
222-
var remove = (i) => {
223-
const fiber = a[i]
224-
removeElement(fiber)
225-
}
226-
227-
diff({
228-
old: a,
229-
cur: b,
230-
key: extr,
231-
add, move, remove, update
232-
})
233-
return actions
234-
}
235-
236214
export const getCurrentFiber = () => currentFiber || null
237215
export const isFn = (x: any): x is Function => typeof x === 'function'
238216
export const isStr = (s: any): s is number | string =>

0 commit comments

Comments
 (0)