From 96b3bc13beff68fe93d16de9f58d700ea1863fa3 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Tue, 18 Feb 2025 13:23:49 +0300 Subject: [PATCH 1/4] Application refactor Signed-off-by: Nikolay Igotti --- arkoala-arkts/arkui/src/Application.ts | 62 +++++++++++++++----------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/arkoala-arkts/arkui/src/Application.ts b/arkoala-arkts/arkui/src/Application.ts index df0f3e773..e810dd3e9 100644 --- a/arkoala-arkts/arkui/src/Application.ts +++ b/arkoala-arkts/arkui/src/Application.ts @@ -97,23 +97,6 @@ export function destroyUiDetachedRoot(node: PeerNode): void { root.dispose() } -function createMemoRoot(manager: StateManager, - /** @memo */ - builder: UserViewBuilder -): ComputableState { - const peer = PeerNode.generateRootPeer() - rootPointer = peer.peer.ptr - - const node = manager.updatableNode(peer, (context: StateContext) => { - const frozen = manager.frozen - manager.frozen = true - memoEntry(context, 0, builder) - manager.frozen = frozen - }) - node.value - return node -} - function measureLayoutAndDraw(peerNode: PeerNode) { ArkUINativeModule._MeasureLayoutAndDraw(peerNode.peer.ptr) } @@ -154,7 +137,7 @@ function drawCurrentCrash(crash: Object) { export class Application { private manager: StateManager | undefined = undefined - private root: ComputableState | undefined = undefined + private rootState: ComputableState | undefined = undefined private timer: MutableState | undefined = undefined private currentCrash: Object | undefined = undefined private enableDumpTree = false @@ -169,22 +152,47 @@ export class Application { this.useNativeLog = useNativeLog } + static createMemoRootState(manager: StateManager, + /** @memo */ + builder: UserViewBuilder + ): ComputableState { + const peer = PeerNode.generateRootPeer() + return manager.updatableNode(peer, (context: StateContext) => { + const frozen = manager.frozen + manager.frozen = true + memoEntry(context, 0, builder) + manager.frozen = frozen + }) + } + + private computeRoot(): PeerNode { + // let handle = ArkUINativeModule._SystemAPI_StartFrame() + let result: PeerNode + try { + result = this.rootState!.value + } finally { + // ArkUINativeModule._SystemAPI_EndFrame(handle) + } + return result + } + start(): pointer { if (this.withLog) UserView.startNativeLog(1) + let root: PeerNode| undefined = undefined try { this.manager = GlobalStateManager.instance this.timer = createAnimationTimer(this.manager!) - let /** @memo */ - builder = this.userView!.getBuilder() - this.root = createMemoRoot(this.manager!, builder) + let builder = this.userView!.getBuilder() + this.rootState = Application.createMemoRootState(this.manager!, builder) + root = this.computeRoot() } catch (e) { if (e instanceof Error) { const stack = e.stack if (stack) { InteropNativeModule._NativeLog("ArkTS Application.start stack trace: " + stack) - return nullptr } + return nullptr } } if (this.withLog) { @@ -198,7 +206,7 @@ export class Application { } } } - return rootPointer! + return root!.peer.ptr } private checkEvents(what: int32) { @@ -209,9 +217,9 @@ export class Application { private updateState() { // NativeModule._NativeLog("ARKTS: updateState") - this.updateStates(this.manager!, this.root!) + this.updateStates(this.manager!, this.rootState!) // Here we request to draw a frame and call custom components JS callbacks. - measureLayoutAndDraw(this.root!.value as PeerNode) + measureLayoutAndDraw(this.rootState!.value as PeerNode) // Call callbacks and sync callScheduledCallbacks() } @@ -220,7 +228,7 @@ export class Application { // Ensure all current state updates took effect. manager.syncChanges() manager.updateSnapshot() - root.value + this.computeRoot() for (const detachedRoot of detachedRoots.values()) detachedRoot.value if (partialUpdates.length > 0) { @@ -267,7 +275,7 @@ export class Application { try { this.timer!.value = Date.now() as int64 this.loopIteration(arg0, arg1) - if (this.enableDumpTree) dumpTree(this.root!.value) + if (this.enableDumpTree) dumpTree(this.rootState!.value) } catch (error) { if (error instanceof Error) { if (error.stack) { -- Gitee From d76f30cde9453962dd4af6d2d94615367844f230 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Tue, 18 Feb 2025 13:54:10 +0300 Subject: [PATCH 2/4] Small cleanup Signed-off-by: Nikolay Igotti --- arkoala-arkts/arkui/src/Application.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/arkoala-arkts/arkui/src/Application.ts b/arkoala-arkts/arkui/src/Application.ts index e810dd3e9..58880d1a1 100644 --- a/arkoala-arkts/arkui/src/Application.ts +++ b/arkoala-arkts/arkui/src/Application.ts @@ -97,10 +97,6 @@ export function destroyUiDetachedRoot(node: PeerNode): void { root.dispose() } -function measureLayoutAndDraw(peerNode: PeerNode) { - ArkUINativeModule._MeasureLayoutAndDraw(peerNode.peer.ptr) -} - function dumpTree(node: IncrementalNode, indent: int32 = 0) { const indentToString = (indent: number) => { let str = "" @@ -219,7 +215,8 @@ export class Application { this.updateStates(this.manager!, this.rootState!) // Here we request to draw a frame and call custom components JS callbacks. - measureLayoutAndDraw(this.rootState!.value as PeerNode) + let root = this.rootState!.value + ArkUINativeModule._MeasureLayoutAndDraw(root.peer.ptr) // Call callbacks and sync callScheduledCallbacks() } -- Gitee From d52740b69e0ee68f0ef0fa94bdffd87e0b3c8ba1 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Tue, 18 Feb 2025 13:58:32 +0300 Subject: [PATCH 3/4] Update Signed-off-by: Nikolay Igotti --- arkoala-arkts/arkui/src/Application.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/arkoala-arkts/arkui/src/Application.ts b/arkoala-arkts/arkui/src/Application.ts index 58880d1a1..7b70af9d5 100644 --- a/arkoala-arkts/arkui/src/Application.ts +++ b/arkoala-arkts/arkui/src/Application.ts @@ -70,6 +70,7 @@ export function currentPartialUpdateContext(): T | undefined { return _currentPartialUpdateContext as (T | undefined) } +// TODO: move to Application class. let detachedRoots: Map> = new Map>() export function createUiDetachedRoot( -- Gitee From 2021ee1ee139d44052a165068b0aad48c110ccf9 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Tue, 18 Feb 2025 14:26:35 +0300 Subject: [PATCH 4/4] Remove Signed-off-by: Nikolay Igotti --- arkoala-arkts/arkui/src/Application.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/arkoala-arkts/arkui/src/Application.ts b/arkoala-arkts/arkui/src/Application.ts index 7b70af9d5..a60c065a0 100644 --- a/arkoala-arkts/arkui/src/Application.ts +++ b/arkoala-arkts/arkui/src/Application.ts @@ -28,8 +28,6 @@ import { enterForeignContext, leaveForeignContext } from "./handwritten" setCustomEventsChecker(checkArkoalaCallbacks) -let rootPointer: pointer = nullptr - enum EventType { Click, Text, -- Gitee