Skip to content

Commit

Permalink
Merge pull request #360 from frejs/simplify
Browse files Browse the repository at this point in the history
continue simplify
  • Loading branch information
yisar authored May 26, 2023
2 parents dbb4bd8 + 4d12b92 commit bae8f9b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 33 deletions.
2 changes: 1 addition & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/ref.tsx"></script>
<script type="module" src="/src/keys.tsx"></script>
<script>
</script>
</body>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fre",
"version": "2.6.2",
"version": "2.6.3",
"type": "module",
"main": "dist/fre.js",
"unpkg": "dist/fre.umd.js",
Expand Down
40 changes: 9 additions & 31 deletions src/reconcile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ const getParentNode = (fiber: IFiber): HTMLElement | undefined => {
const diffKids = (fiber: any, children: FreNode): void => {
let aCh = fiber.kids || [],
bCh = (fiber.kids = arrayfy(children) as any)
const actions = idiff(aCh, bCh)
const actions = diff(aCh, bCh)

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

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

const diff = function (opts) {
const diff = function (a, b) {
var actions = [],
aIdx = {},
bIdx = {},
a = opts.old,
b = opts.cur,
key = opts.key,
key = v => v.key + v.type,
i, j;
for (i = 0; i < a.length; i++) {
aIdx[key(a[i])] = i;
Expand All @@ -184,27 +182,27 @@ const diff = function (opts) {
if (aElm === null) {
i++;
} else if (b.length <= j) {
opts.remove(i)
removeElement(a[i])
i++;
} else if (a.length <= i) {
opts.add(bElm, i)
actions.push({ op: TAG.INSERT, elm: bElm, before: a[i] })
j++;
} else if (key(aElm) === key(bElm)) {
clone(aElm, bElm)
opts.update(aElm, bElm)
actions.push({ op: TAG.UPDATE })
i++; j++;
} else {
var curElmInNew = bIdx[key(aElm)]
var wantedElmInOld = aIdx[key(bElm)]
if (curElmInNew === undefined) {
opts.remove(i);
removeElement(a[i])
i++;
} else if (wantedElmInOld === undefined) {
opts.add(bElm, i)
actions.push({ op: TAG.INSERT, elm: bElm, before: a[i] })
j++
} else {
clone(a[wantedElmInOld], bElm)
opts.move(wantedElmInOld, i)
actions.push({ op: TAG.MOVE, elm: a[wantedElmInOld], before: a[i] })
a[wantedElmInOld] = null
j++
}
Expand All @@ -213,26 +211,6 @@ const diff = function (opts) {
return actions
}

var idiff = function (a, b) {
var actions = [] // 必须保持和 b 一致的顺序
var extr = (v) => v.key + v.type
var update = (a, b) => actions.push({ op: TAG.UPDATE })
var move = (from, to) => actions.push({ op: TAG.MOVE, elm: a[from], before: a[to] })
var add = (elm, i) => actions.push({ op: TAG.INSERT, elm: elm, before: a[i] })
var remove = (i) => {
const fiber = a[i]
removeElement(fiber)
}

diff({
old: a,
cur: b,
key: extr,
add, move, remove, update
})
return actions
}

export const getCurrentFiber = () => currentFiber || null
export const isFn = (x: any): x is Function => typeof x === 'function'
export const isStr = (s: any): s is number | string =>
Expand Down

0 comments on commit bae8f9b

Please sign in to comment.