Skip to content

Commit

Permalink
dom.commitWork - re-implement fast-path for list rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
LankyMoose committed Apr 2, 2024
1 parent d4fb30c commit 09bdc8a
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions packages/lib/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,15 @@ function updateDom(node: VNode, dom: HTMLElement | SVGElement | Text) {
return dom
}

function placeDom(node: VNode, dom: HTMLElement | SVGElement | Text) {
function placeDom(
node: VNode,
dom: HTMLElement | SVGElement | Text,
prevSiblingDom: HTMLElement | SVGElement | Text | undefined
) {
if (prevSiblingDom) {
prevSiblingDom.after(dom)
return
}
// find mountable parent dom
let domParentNode: VNode | undefined = node.parent ?? node.prev?.parent
let domParent = domParentNode?.instance?.rootDom ?? domParentNode?.dom
Expand Down Expand Up @@ -189,13 +197,15 @@ function placeDom(node: VNode, dom: HTMLElement | SVGElement | Text) {

function commitWork(ctx: GlobalContext, vNode: VNode) {
let commitSibling = false
const stack: VNode[] = [vNode]
const stack: [VNode, HTMLElement | SVGElement | Text | undefined][] = [
[vNode, undefined],
]
while (stack.length) {
const n = stack.pop()!
const [n, prevSiblingDom] = stack.pop()!
const dom = n.dom
if (dom) {
if (!dom.isConnected || n.effectTag === EffectTag.PLACEMENT) {
placeDom(n, dom)
placeDom(n, dom, prevSiblingDom)
} else if (n.effectTag === EffectTag.UPDATE) {
updateDom(n, dom)
}
Expand All @@ -206,7 +216,7 @@ function commitWork(ctx: GlobalContext, vNode: VNode) {
}

if (commitSibling && n.sibling) {
stack.push(n.sibling)
stack.push([n.sibling, dom])
}
commitSibling = true

Expand All @@ -216,7 +226,7 @@ function commitWork(ctx: GlobalContext, vNode: VNode) {
}

if (n.child) {
stack.push(n.child)
stack.push([n.child, undefined])
}

const instance = n.instance
Expand Down

0 comments on commit 09bdc8a

Please sign in to comment.