From 0d1e27d487f68d44132e46878a805c3f2405f6c8 Mon Sep 17 00:00:00 2001 From: Robby6Strings Date: Fri, 5 Jul 2024 23:22:41 +1200 Subject: [PATCH] appCtx.setProps() now clears WIP and returns Promise --- packages/lib/src/appContext.ts | 17 +++++++++++------ packages/lib/src/scheduler.ts | 11 +++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/packages/lib/src/appContext.ts b/packages/lib/src/appContext.ts index eef7f915..0ff407c0 100644 --- a/packages/lib/src/appContext.ts +++ b/packages/lib/src/appContext.ts @@ -71,15 +71,20 @@ export class AppContext = {}> { } 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>((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) { diff --git a/packages/lib/src/scheduler.ts b/packages/lib/src/scheduler.ts index 88585465..1bab0874 100644 --- a/packages/lib/src/scheduler.ts +++ b/packages/lib/src/scheduler.ts @@ -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