/* * Copyright (c) 2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * @file * @kit ArkUI * @arkts 1.2 */ import { int32, KoalaCallsiteKey, uint32 } from '@koalaui.runtime.common' import { Disposable } from '@koalaui.runtime.states.Disposable' import { IncrementalNode } from '@koalaui.runtime.tree.IncrementalNode' export declare const CONTEXT_ROOT_SCOPE: string export declare const CONTEXT_ROOT_NODE: string export type Equivalent = (oldV: Value, newV: Value) => boolean export declare function createStateManager(): StateManager export interface StateManager extends StateContext { syncChanges(): void isUpdateNeeded(): boolean updateSnapshot(): uint32 updatableNode( node: Node, update: (context: StateContext) => void, cleanup?: () => void): ComputableState scheduleCallback(callback: () => void): void callCallbacks(): void frozen: boolean reset(): void } export interface State { readonly modified: boolean readonly value: Value } export interface MutableState extends Disposable, State { value: Value } export interface ArrayState extends State> { length: number at(index: number): Item get(index: number): Item set(index: number, item: Item): void copyWithin(target: number, start: number, end?: number): Array fill(value: Item, start?: number, end?: number): Array pop(): Item | undefined push(...items: Item[]): number reverse(): Array shift(): Item | undefined sort(comparator?: (a: Item, b: Item) => number): Array splice(start: number, deleteCount: number | undefined, ...items: Item[]): Array unshift(...items: Item[]): number } export interface ComputableState extends Disposable, State { readonly recomputeNeeded: boolean } export interface StateContext { readonly node: IncrementalNode | undefined attach( id: KoalaCallsiteKey, create: () => Node, update: () => void, cleanup?: () => void): void compute( id: KoalaCallsiteKey, compute: () => Value, cleanup?: (value: Value | undefined) => void, once?: boolean): Value computableState( compute: (context: StateContext) => Value, cleanup?: (context: StateContext, value: Value | undefined) => void): ComputableState mutableState( initial: Value, global?: boolean, equivalent?: Equivalent, tracker?: ValueTracker): MutableState arrayState( initial?: ReadonlyArray, global?: boolean, equivalent?: Equivalent): ArrayState namedState( name: string, create: () => Value, global?: boolean, equivalent?: Equivalent, tracker?: ValueTracker): MutableState stateBy(name: string, global?: boolean): MutableState | undefined valueBy(name: string, global?: boolean): Value scope( id: KoalaCallsiteKey, paramCount?: int32, create?: () => IncrementalNode, compute?: () => Value, cleanup?: (value: Value | undefined) => void, once?: boolean, reuseKey?: string ): InternalScope controlledScope(id: KoalaCallsiteKey, invalidate: () => void): ControlledScope } export interface ValueTracker { onCreate(value: Value): Value onUpdate(value: Value): Value } export interface InternalScope { readonly unchanged: boolean readonly cached: Value recache(newValue?: Value): Value param(index: int32, value: V, equivalent?: Equivalent, name?: string, contextLocal?: boolean): State } export interface ControlledScope { enter(): void leave(): void }