Skip to content

Commit

Permalink
lib/appCtx - requesting update/delete for a node during hydration wil…
Browse files Browse the repository at this point in the history
…l postpone the request for next idle
  • Loading branch information
LankyMoose committed Jul 15, 2024
1 parent d3cdb52 commit 94fa564
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
12 changes: 11 additions & 1 deletion packages/lib/src/appContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,21 @@ export class AppContext<T extends Record<string, unknown> = {}> {

requestUpdate(node: VNode) {
if (node.effectTag === EffectTag.DELETION) return
return this.scheduler?.queueUpdate(node)
if (renderMode.current === "hydrate") {
return this.scheduler?.nextIdle((s) => {
node.effectTag !== EffectTag.DELETION && s.queueUpdate(node)
})
}
this.scheduler?.queueUpdate(node)
}

requestDelete(node: VNode) {
if (node.effectTag === EffectTag.DELETION) return
if (renderMode.current === "hydrate") {
return this.scheduler?.nextIdle((s) => {
node.effectTag !== EffectTag.DELETION && s.queueDelete(node)
})
}
this.scheduler?.queueDelete(node)
}

Expand Down
6 changes: 3 additions & 3 deletions packages/lib/src/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class Scheduler {
private currentTreeIndex = 0
private isRunning = false
private queuedNodeEffectSets: Function[][] = []
private nextIdleEffects: Function[] = []
private nextIdleEffects: ((scheduler: this) => void)[] = []
private deletions: VNode[] = []
private frameDeadline = 0
private pendingCallback: IdleRequestCallback | undefined
Expand Down Expand Up @@ -143,7 +143,7 @@ export class Scheduler {
this.nodeEffects = []
}

nextIdle(fn: () => void) {
nextIdle(fn: (scheduler: this) => void) {
this.nextIdleEffects.push(fn)
}

Expand Down Expand Up @@ -186,7 +186,7 @@ export class Scheduler {
if (!this.nextUnitOfWork) {
this.sleep()
while (this.nextIdleEffects.length) {
this.nextIdleEffects.shift()!()
this.nextIdleEffects.shift()!(this)
}
return
}
Expand Down

0 comments on commit 94fa564

Please sign in to comment.