• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/**
16 * @file
17 * @kit ArkUI
18 * @arkts 1.2
19 */
20
21import { int32, KoalaCallsiteKey, uint32 } from '@koalaui.runtime.common'
22import { Disposable } from '@koalaui.runtime.states.Disposable'
23import { IncrementalNode } from '@koalaui.runtime.tree.IncrementalNode'
24export declare const CONTEXT_ROOT_SCOPE: string
25export declare const CONTEXT_ROOT_NODE: string
26export type Equivalent<Value> = (oldV: Value, newV: Value) => boolean
27export declare function createStateManager(): StateManager
28export interface StateManager extends StateContext {
29    syncChanges(): void
30    isUpdateNeeded(): boolean
31    updateSnapshot(): uint32
32    updatableNode<Node extends IncrementalNode>(
33        node: Node,
34        update: (context: StateContext) => void,
35        cleanup?: () => void): ComputableState<Node>
36    scheduleCallback(callback: () => void): void
37    callCallbacks(): void
38    frozen: boolean
39    reset(): void
40}
41export interface State<Value> {
42    readonly modified: boolean
43    readonly value: Value
44}
45export interface MutableState<Value> extends Disposable, State<Value> {
46    value: Value
47}
48export interface ArrayState<Item> extends State<ReadonlyArray<Item>> {
49    length: number
50    at(index: number): Item
51    get(index: number): Item
52    set(index: number, item: Item): void
53    copyWithin(target: number, start: number, end?: number): Array<Item>
54    fill(value: Item, start?: number, end?: number): Array<Item>
55    pop(): Item | undefined
56    push(...items: Item[]): number
57    reverse(): Array<Item>
58    shift(): Item | undefined
59    sort(comparator?: (a: Item, b: Item) => number): Array<Item>
60    splice(start: number, deleteCount: number | undefined, ...items: Item[]): Array<Item>
61    unshift(...items: Item[]): number
62}
63export interface ComputableState<Value> extends Disposable, State<Value> {
64    readonly recomputeNeeded: boolean
65}
66export interface StateContext {
67    readonly node: IncrementalNode | undefined
68    attach<Node extends IncrementalNode>(
69        id: KoalaCallsiteKey, create: () => Node, update: () => void, cleanup?: () => void): void
70    compute<Value>(
71        id: KoalaCallsiteKey,
72        compute: () => Value,
73        cleanup?: (value: Value | undefined) => void,
74        once?: boolean): Value
75    computableState<Value>(
76        compute: (context: StateContext) => Value,
77        cleanup?: (context: StateContext,
78        value: Value | undefined) => void): ComputableState<Value>
79    mutableState<Value>(
80        initial: Value,
81        global?: boolean,
82        equivalent?: Equivalent<Value>,
83        tracker?: ValueTracker<Value>): MutableState<Value>
84    arrayState<Value>(
85        initial?: ReadonlyArray<Value>,
86        global?: boolean,
87        equivalent?: Equivalent<Value>): ArrayState<Value>
88    namedState<Value>(
89        name: string, create: () => Value,
90        global?: boolean,
91        equivalent?: Equivalent<Value>,
92        tracker?: ValueTracker<Value>): MutableState<Value>
93    stateBy<Value>(name: string, global?: boolean): MutableState<Value> | undefined
94    valueBy<Value>(name: string, global?: boolean): Value
95    scope<Value>(
96        id: KoalaCallsiteKey,
97        paramCount?: int32,
98        create?: () => IncrementalNode,
99        compute?: () => Value,
100        cleanup?: (value: Value | undefined) => void,
101        once?: boolean,
102        reuseKey?: string
103    ): InternalScope<Value>
104    controlledScope(id: KoalaCallsiteKey, invalidate: () => void): ControlledScope
105}
106export interface ValueTracker<Value> {
107    onCreate(value: Value): Value
108    onUpdate(value: Value): Value
109}
110export interface InternalScope<Value> {
111    readonly unchanged: boolean
112    readonly cached: Value
113    recache(newValue?: Value): Value
114    param<V>(index: int32, value: V, equivalent?: Equivalent<V>, name?: string, contextLocal?: boolean): State<V>
115}
116export interface ControlledScope {
117    enter(): void
118    leave(): void
119}
120