Skip to content

Commit 16e891a

Browse files
authored
Merge pull request #385 from wooloo26/master
Ensure that memoized fibers behave consistently with reconcile functions during the commit phase.
2 parents 8d3e375 + 636fcfc commit 16e891a

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

src/commit.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { FiberFinish, FiberHost, HTMLElementEx, Fiber, Ref, TAG } from './type'
22
import { updateElement } from './dom'
33
import { isFn } from './reconcile'
44

5-
export const commit = (fiber: FiberFinish) => {
5+
export const commit = (fiber?: FiberFinish) => {
66
if (!fiber) {
77
return
88
}
@@ -30,8 +30,16 @@ export const commit = (fiber: FiberFinish) => {
3030

3131
fiber.action = null
3232

33-
commit(fiber.child)
34-
commit(fiber.sibling)
33+
commitSibling(fiber.child)
34+
commitSibling(fiber.sibling)
35+
}
36+
37+
function commitSibling(fiber?: FiberFinish) {
38+
if (fiber?.memo) {
39+
commitSibling(fiber.sibling)
40+
} else {
41+
commit(fiber)
42+
}
3543
}
3644

3745
const refer = (ref?: Ref<HTMLElementEx>, dom?: HTMLElementEx) => {

src/reconcile.ts

+21-18
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,17 @@ const reconcile = (fiber?: Fiber) => {
3838
return null
3939
}
4040

41-
const memo = (fiber: Fiber) => {
42-
if (
43-
(fiber.type as FC).memo &&
44-
fiber.type === fiber.old?.type &&
45-
fiber.old?.props
46-
) {
47-
let scu = (fiber.type as FC).shouldUpdate || shouldUpdate
48-
if (!scu(fiber.props, fiber.old.props)) {
49-
// fast-fix
50-
return getSibling(fiber)
51-
}
52-
}
53-
return null
54-
}
55-
5641
const capture = (fiber: Fiber) => {
5742
fiber.isComp = isFn(fiber.type)
5843
if (fiber.isComp) {
59-
const memoFiber = memo(fiber)
60-
if (memoFiber) {
61-
return memoFiber
44+
if (isMemo(fiber)) {
45+
fiber.memo = true
46+
// fast-fix
47+
return getSibling(fiber)
48+
} else if (fiber.memo) {
49+
fiber.memo = false
6250
}
51+
6352
updateHook(fiber)
6453
} else {
6554
updateHost(fiber as FiberHost)
@@ -69,6 +58,20 @@ const capture = (fiber: Fiber) => {
6958
return sibling
7059
}
7160

61+
export const isMemo = (fiber: Fiber) => {
62+
if (
63+
(fiber.type as FC).memo &&
64+
fiber.type === fiber.old?.type &&
65+
fiber.old?.props
66+
) {
67+
let scu = (fiber.type as FC).shouldUpdate || shouldUpdate
68+
if (!scu(fiber.props, fiber.old.props)) {
69+
return true
70+
}
71+
}
72+
return false
73+
}
74+
7275
const getSibling = (fiber?: Fiber) => {
7376
while (fiber) {
7477
bubble(fiber)

src/type.ts

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export type PropsOf<T extends FC | string> = T extends FC<infer P>
8484
interface FiberBase {
8585
key?: string
8686
type?: string | FC
87+
memo?: boolean
8788
props?: PropsOf<FC | string>
8889
isComp?: boolean
8990
parentNode?: HTMLElementEx | {}

0 commit comments

Comments
 (0)