Skip to content

Commit

Permalink
appCtx.setProps() now clears WIP and returns Promise<AppContext>
Browse files Browse the repository at this point in the history
  • Loading branch information
LankyMoose committed Jul 5, 2024
1 parent 686b698 commit 0d1e27d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
17 changes: 11 additions & 6 deletions packages/lib/src/appContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,20 @@ export class AppContext<T extends Record<string, unknown> = {}> {
}

setProps(fn: (oldProps: T) => T) {
if (!this.mounted || !this.rootNode?.child)
const rootChild = this.rootNode?.child
const scheduler = this.scheduler
if (!this.mounted || !rootChild || !scheduler)
return console.error(
"[kaioken]: failed to apply new props - ensure the app is mounted"
)

const { children, ref, key, ...rest } = this.rootNode.child.props
const args = rest as T
Object.assign(this.rootNode.child.props, fn(args))
this.requestUpdate(this.rootNode.child)
return new Promise<AppContext<T>>((resolve) => {
scheduler.clear()
const { children, ref, key, ...rest } = rootChild.props
const args = rest as T
Object.assign(rootChild.props, fn(args))
scheduler.queueUpdate(rootChild)
scheduler.nextIdle(() => resolve(this))
})
}

requestUpdate(node: VNode) {
Expand Down
11 changes: 11 additions & 0 deletions packages/lib/src/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ export class Scheduler {
}
}

clear() {
this.nextUnitOfWork = undefined
this.treesInProgress = []
this.currentTreeIndex = 0
this.queuedNodeEffectSets = []
this.nextIdleEffects = []
this.deletions = []
this.frameDeadline = 0
this.pendingCallback = undefined
}

wake() {
if (this.isRunning) return
this.isRunning = true
Expand Down

0 comments on commit 0d1e27d

Please sign in to comment.