• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2024 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/**
17 * @file State management API file
18 * @kit ArkUI
19 */
20
21/**
22 * Function that returns default creator.
23 *
24 * @typedef { function } StorageDefaultCreator<T>
25 * @returns { T } default creator
26 * @syscap SystemCapability.ArkUI.ArkUI.Full
27 * @crossplatform
28 * @atomicservice
29 * @since 12
30 */
31export declare type StorageDefaultCreator<T> = () => T;
32
33/**
34 * Define class constructor with arbitrary parameters.
35 * @interface TypeConstructorWithArgs<T>
36 * @syscap SystemCapability.ArkUI.ArkUI.Full
37 * @crossplatform
38 * @atomicservice
39 * @since 12
40 */
41export interface TypeConstructorWithArgs<T> {
42  /**
43   * @param { any } args the arguments of the constructor.
44   * @returns { T } return class instance.
45   * @syscap SystemCapability.ArkUI.ArkUI.Full
46   * @crossplatform
47   * @atomicservice
48   * @since 12
49   */
50  new(...args: any): T;
51}
52
53/**
54 * AppStorageV2 is for UI state of app-wide access, has same life cycle as the app,
55 * and saves database content only in memory.
56 *
57 * @syscap SystemCapability.ArkUI.ArkUI.Full
58 * @crossplatform
59 * @atomicservice
60 * @since 12
61 */
62export declare class AppStorageV2 {
63  /**
64   * If the value for the given key is already available, return the value.
65   * If not, add the key with value generated by calling defaultFunc and return it to caller.
66   *
67   * @param { TypeConstructorWithArgs<T> } type class type of the stored value.
68   * @param { string | StorageDefaultCreator<T> } [keyOrDefaultCreator] alias name of the key, or the function generating the default value.
69   * @param { StorageDefaultCreator<T> } [defaultCreator] the function generating the default value
70   * @returns { T | undefined } the value of the existed key or the default value
71   * @syscap SystemCapability.ArkUI.ArkUI.Full
72   * @crossplatform
73   * @atomicservice
74   * @since 12
75   */
76  static connect<T extends object>(
77    type: TypeConstructorWithArgs<T>,
78    keyOrDefaultCreator?: string | StorageDefaultCreator<T>,
79    defaultCreator?: StorageDefaultCreator<T>
80  ): T | undefined;
81
82  /**
83   * Removes data with the given key or given class type.
84   *
85   * @param { string | TypeConstructorWithArgs<T> } keyOrType key or class type removing
86   * @syscap SystemCapability.ArkUI.ArkUI.Full
87   * @crossplatform
88   * @atomicservice
89   * @since 12
90   */
91  static remove<T>(keyOrType: string | TypeConstructorWithArgs<T>): void;
92
93  /**
94   * Return the array of all keys.
95   *
96   * @returns { Array<string> } the array of all keys
97   * @syscap SystemCapability.ArkUI.ArkUI.Full
98   * @crossplatform
99   * @atomicservice
100   * @since 12
101   */
102  static keys(): Array<string>;
103}
104
105/**
106 * Function that returns reason type when error.
107 *
108 * @typedef { function } PersistenceErrorCallback
109 * @param { string } key persisted key when error
110 * @param { 'quota' | 'serialization' | 'unknown' } reason reason type when error
111 * @param { string } message more message when error
112 * @syscap SystemCapability.ArkUI.ArkUI.Full
113 * @crossplatform
114 * @atomicservice
115 * @since 12
116 */
117export declare type PersistenceErrorCallback = (key: string, reason: 'quota' | 'serialization' | 'unknown', message: string) => void;
118
119/**
120 * PersistenceV2 is for UI state of app-wide access, available on app re-start,
121 * and saves database content in disk.
122 *
123 * @extends AppStorageV2
124 * @syscap SystemCapability.ArkUI.ArkUI.Full
125 * @crossplatform
126 * @atomicservice
127 * @since 12
128 */
129export declare class PersistenceV2 extends AppStorageV2 {
130  /**
131   * Used to manually persist data changes to disks.
132   *
133   * @param { string | TypeConstructorWithArgs<T> } keyOrType key or class type need to persist.
134   * @syscap SystemCapability.ArkUI.ArkUI.Full
135   * @crossplatform
136   * @atomicservice
137   * @since 12
138   */
139  static save<T>(keyOrType: string | TypeConstructorWithArgs<T>): void;
140
141  /**
142   * Be called when persisting has encountered an error.
143   *
144   * @param { PersistenceErrorCallback | undefined } callback called when error
145   * @syscap SystemCapability.ArkUI.ArkUI.Full
146   * @crossplatform
147   * @atomicservice
148   * @since 12
149   */
150  static notifyOnError(callback: PersistenceErrorCallback | undefined): void;
151}
152
153/**
154 * Define TypeConstructor type.
155 *
156 * @interface TypeConstructor<T>
157 * @syscap SystemCapability.ArkUI.ArkUI.Full
158 * @crossplatform
159 * @atomicservice
160 * @since 12
161 */
162export interface TypeConstructor<T> {
163  /**
164   * @returns { T }
165   * @syscap SystemCapability.ArkUI.ArkUI.Full
166   * @crossplatform
167   * @atomicservice
168   * @since 12
169   */
170  new(): T;
171}
172
173/**
174 * Function that returns PropertyDecorator.
175 *
176 * @typedef { function } TypeDecorator
177 * @param { TypeConstructor<T> } type type info
178 * @returns { PropertyDecorator } Type decorator
179 * @syscap SystemCapability.ArkUI.ArkUI.Full
180 * @crossplatform
181 * @atomicservice
182 * @since 12
183 */
184export declare type TypeDecorator = <T>(type: TypeConstructor<T>) => PropertyDecorator;
185
186/**
187 * Define Type PropertyDecorator, adds type information to an object.
188 *
189 * @syscap SystemCapability.ArkUI.ArkUI.Full
190 * @crossplatform
191 * @atomicservice
192 * @since 12
193 */
194export declare const Type: TypeDecorator;
195
196/**
197 * UIUtils is a state management tool class for operating the observed data.
198 *
199 * @syscap SystemCapability.ArkUI.ArkUI.Full
200 * @crossplatform
201 * @atomicservice
202 * @since 12
203 */
204export declare class UIUtils {
205  /**
206   * Get raw object from the Object wrapped with an ObservedObject.
207   * If input parameter is a regular Object without ObservedObject, return Object itself.
208   *
209   * @param { T } source input source Object data.
210   * @returns { T } raw object from the Object wrapped with an ObservedObject.
211   * @syscap SystemCapability.ArkUI.ArkUI.Full
212   * @crossplatform
213   * @atomicservice
214   * @since 12
215   */
216  static getTarget<T extends object>(source: T): T;
217
218   /**
219   * Make non-observed data into observed data.
220   * Support non-observed class, JSON.parse Object and Sendable class.
221   *
222   * @param { T } source input source object data.
223   * @returns { T } proxy object from the source object data.
224   * @syscap SystemCapability.ArkUI.ArkUI.Full
225   * @crossplatform
226   * @atomicservice
227   * @since 12
228   */
229   static makeObserved<T extends object>(source: T): T;
230
231}