From a99d7749155917224a84767f111292605bf7bcff Mon Sep 17 00:00:00 2001 From: Sergey Malenkov Date: Thu, 3 Jul 2025 16:38:44 +0300 Subject: [PATCH 1/2] introduce CoroutineLocalValue --- incremental/common/src/index.ts | 1 + incremental/compat/src/arkts/utils.ts | 16 +++++++++++++++- incremental/compat/src/index.ts | 1 + incremental/compat/src/ohos/index.ts | 1 + incremental/compat/src/typescript/utils.ts | 12 +++++++++++- 5 files changed, 29 insertions(+), 2 deletions(-) diff --git a/incremental/common/src/index.ts b/incremental/common/src/index.ts index 11136c33c8..b55a601ef0 100644 --- a/incremental/common/src/index.ts +++ b/incremental/common/src/index.ts @@ -42,6 +42,7 @@ export { int8Array, errorAsString, unsafeCast, + CoroutineLocalValue, scheduleCoroutine, memoryStats, launchJob diff --git a/incremental/compat/src/arkts/utils.ts b/incremental/compat/src/arkts/utils.ts index 0523e835a3..75c8e744f6 100644 --- a/incremental/compat/src/arkts/utils.ts +++ b/incremental/compat/src/arkts/utils.ts @@ -34,4 +34,18 @@ export function memoryStats(): string { export function launchJob(job: () => void): Promise { throw new Error("unsupported yet: return launch job()") -} \ No newline at end of file +} + +export class CoroutineLocalValue { + private map = new containers.ConcurrentHashMap + get(): T | undefined { + return this.map.get(CoroutineExtras.getWorkerId()) + } + set(value: T | undefined) { + if (value) { + this.map.set(CoroutineExtras.getWorkerId(), value) + } else { + this.map.delete(CoroutineExtras.getWorkerId()) + } + } +} diff --git a/incremental/compat/src/index.ts b/incremental/compat/src/index.ts index 24e868eac1..e4a0a9c4dd 100644 --- a/incremental/compat/src/index.ts +++ b/incremental/compat/src/index.ts @@ -61,6 +61,7 @@ export { int8Array, errorAsString, unsafeCast, + CoroutineLocalValue, scheduleCoroutine, memoryStats, launchJob diff --git a/incremental/compat/src/ohos/index.ts b/incremental/compat/src/ohos/index.ts index cafcfb1cd6..3edbe8fe57 100644 --- a/incremental/compat/src/ohos/index.ts +++ b/incremental/compat/src/ohos/index.ts @@ -59,6 +59,7 @@ export { int8Array, errorAsString, unsafeCast, + CoroutineLocalValue, scheduleCoroutine, memoryStats, launchJob diff --git a/incremental/compat/src/typescript/utils.ts b/incremental/compat/src/typescript/utils.ts index 1d72692be8..c51ff17f4c 100644 --- a/incremental/compat/src/typescript/utils.ts +++ b/incremental/compat/src/typescript/utils.ts @@ -33,4 +33,14 @@ export function launchJob(job: () => void): Promise { job() }, 0) ) -} \ No newline at end of file +} + +export class CoroutineLocalValue { + private value: T | undefined = undefined + get(): T | undefined { + return this.value + } + set(value: T | undefined) { + this.value = value + } +} -- Gitee From f3ba3fd845b6046f67c61b0abc8382c9fc2deb10 Mon Sep 17 00:00:00 2001 From: Sergey Malenkov Date: Thu, 3 Jul 2025 17:06:00 +0300 Subject: [PATCH 2/2] synchronize changes with arkui_ace_engine --- .../runtime/src/states/GlobalStateManager.ts | 34 +++++++++++++++++-- incremental/runtime/src/states/State.ts | 4 ++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/incremental/runtime/src/states/GlobalStateManager.ts b/incremental/runtime/src/states/GlobalStateManager.ts index fed60f1cf5..7c1285cf61 100644 --- a/incremental/runtime/src/states/GlobalStateManager.ts +++ b/incremental/runtime/src/states/GlobalStateManager.ts @@ -13,6 +13,7 @@ * limitations under the License. */ +import { CoroutineLocalValue, KoalaCallsiteKey } from "@koalaui/common" import { ArrayState, Equivalent, MutableState, StateManager, ValueTracker, createStateManager } from "./State" /** @@ -20,7 +21,12 @@ import { ArrayState, Equivalent, MutableState, StateManager, ValueTracker, creat * @internal */ export class GlobalStateManager { - private static current: StateManager | undefined = undefined + private static localManager = new CoroutineLocalValue() + private static sharedManager: StateManager | undefined = undefined + + private static get current(): StateManager | undefined { + return GlobalStateManager.GetLocalManager() ?? GlobalStateManager.sharedManager + } /** * The current instance of a global state manager. @@ -30,7 +36,7 @@ export class GlobalStateManager { let current = GlobalStateManager.current if (current === undefined) { current = createStateManager() - GlobalStateManager.current = current + GlobalStateManager.sharedManager = current } return current } @@ -42,6 +48,30 @@ export class GlobalStateManager { static reset() { GlobalStateManager.current?.reset() } + + /** + * Get state manager by coroutine id. + * @internal + */ + static GetLocalManager(): StateManager | undefined { + return GlobalStateManager.localManager.get() + } + + /** + * Store state manager by coroutine id. + * @internal + */ + static SetLocalManager(manager: StateManager | undefined): void { + GlobalStateManager.localManager.set(manager) + } + + /** + * @return callsite key for a current context or `undefined` for global context + * @internal + */ + public static getCurrentScopeId(): KoalaCallsiteKey | undefined { + return GlobalStateManager.instance.currentScopeId + } } /** diff --git a/incremental/runtime/src/states/State.ts b/incremental/runtime/src/states/State.ts index f5afc5290c..3c924bc244 100644 --- a/incremental/runtime/src/states/State.ts +++ b/incremental/runtime/src/states/State.ts @@ -44,6 +44,7 @@ export function createStateManager(): StateManager { * applications. */ export interface StateManager extends StateContext { + readonly currentScopeId: KoalaCallsiteKey | undefined syncChanges(): void isUpdateNeeded(): boolean updateSnapshot(): uint32 @@ -547,7 +548,8 @@ class StateManagerImpl implements StateManager { private readonly callbacks: MarkableQueue = markableQueue() readonly journal: Journal = new Journal() - constructor() { + get currentScopeId(): KoalaCallsiteKey | undefined { + return this.current?.id } reset(): void { -- Gitee