Skip to content

Ensure that memoized fibers behave consistently with reconcile functions during the commit phase. #385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/commit.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import { FiberFinish, FiberHost, HTMLElementEx, Fiber, Ref, TAG } from './type'
import { updateElement } from './dom'
import { isFn } from './reconcile'

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

fiber.action = null

commit(fiber.child)
commit(fiber.sibling)
commitSibling(fiber.child)
commitSibling(fiber.sibling)
}

function commitSibling(fiber?: FiberFinish) {
if (fiber?.memo) {
commitSibling(fiber.sibling)
} else {
commit(fiber)
}
}

const refer = (ref?: Ref<HTMLElementEx>, dom?: HTMLElementEx) => {
39 changes: 21 additions & 18 deletions src/reconcile.ts
Original file line number Diff line number Diff line change
@@ -38,28 +38,17 @@ const reconcile = (fiber?: Fiber) => {
return null
}

const memo = (fiber: Fiber) => {
if (
(fiber.type as FC).memo &&
fiber.type === fiber.old?.type &&
fiber.old?.props
) {
let scu = (fiber.type as FC).shouldUpdate || shouldUpdate
if (!scu(fiber.props, fiber.old.props)) {
// fast-fix
return getSibling(fiber)
}
}
return null
}

const capture = (fiber: Fiber) => {
fiber.isComp = isFn(fiber.type)
if (fiber.isComp) {
const memoFiber = memo(fiber)
if (memoFiber) {
return memoFiber
if (isMemo(fiber)) {
fiber.memo = true
// fast-fix
return getSibling(fiber)
} else if (fiber.memo) {
fiber.memo = false
}

updateHook(fiber)
} else {
updateHost(fiber as FiberHost)
@@ -69,6 +58,20 @@ const capture = (fiber: Fiber) => {
return sibling
}

export const isMemo = (fiber: Fiber) => {
if (
(fiber.type as FC).memo &&
fiber.type === fiber.old?.type &&
fiber.old?.props
) {
let scu = (fiber.type as FC).shouldUpdate || shouldUpdate
if (!scu(fiber.props, fiber.old.props)) {
return true
}
}
return false
}

const getSibling = (fiber?: Fiber) => {
while (fiber) {
bubble(fiber)
1 change: 1 addition & 0 deletions src/type.ts
Original file line number Diff line number Diff line change
@@ -84,6 +84,7 @@ export type PropsOf<T extends FC | string> = T extends FC<infer P>
interface FiberBase {
key?: string
type?: string | FC
memo?: boolean
props?: PropsOf<FC | string>
isComp?: boolean
parentNode?: HTMLElementEx | {}