• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2* Copyright (c) 2021 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
16import { AsyncCallback, Callback } from './@ohos.base';
17
18/**
19 * Provides interfaces to obtain and modify storage data.
20 *
21 * @name storage
22 * @since 6
23 * @deprecated since 9
24 * @useinstead ohos.preferences.preferences
25 * @syscap SystemCapability.DistributedDataManager.Preferences.Core
26 *
27 */
28declare namespace storage {
29  /**
30   * Obtains a {@link Storage} instance matching a specified storage file name.
31   *
32   * <p>The {@link references} instance loads all data of the storage file and
33   * resides in the memory. You can use removeStorageFromCache to remove the instance from the memory.
34   *
35   * @param path Indicates the path of storage file stored.
36   * @returns Returns the {@link Storage} instance matching the specified storage file name.
37   * @throws BusinessError if invoked failed
38   * @since 6
39   * @deprecated since 9
40   * @useinstead ohos.preferences.preferences.getPreferences
41   */
42
43  function getStorageSync(path: string): Storage;
44  function getStorage(path: string, callback: AsyncCallback<Storage>): void;
45  function getStorage(path: string): Promise<Storage>;
46
47  /**
48   * Deletes a {@link Storage} instance matching a specified storage file name
49   * from the cache which is performed by removeStorageFromCache and deletes the
50   * storage file.
51   *
52   * <p>When deleting the {@link Storage} instance, you must release all references
53   * of the instance. In addition, do not use the instance to perform data operations. Otherwise, inconsistent data
54   * will occur.
55   *
56   * @param path Indicates the path of storage file
57   * @throws BusinessError if invoked failed
58   * @since 6
59   * @deprecated since 9
60   * @useinstead ohos.preferences.preferences.deletePreferences
61   */
62  function deleteStorageSync(path: string): void;
63  function deleteStorage(path: string, callback: AsyncCallback<void>): void;
64  function deleteStorage(path: string): Promise<void>;
65
66  /**
67   * Deletes a {@link Storage} instance matching a specified storage file name
68   * from the cache.
69   *
70   * <p>When deleting the {@link Storage} instance, you must release all references
71   * of the instance. In addition, do not use the instance to perform data operations. Otherwise, inconsistent data
72   * will occur.
73   *
74   * @param path Indicates the path of storage file.
75   * @throws BusinessError if invoked failed
76   * @since 6
77   * @deprecated since 9
78   * @useinstead ohos.preferences.preferences.removePreferencesFromCache
79   */
80  function removeStorageFromCacheSync(path: string): void;
81  function removeStorageFromCache(path: string, callback: AsyncCallback<void>): void;
82  function removeStorageFromCache(path: string): Promise<void>;
83
84  /**
85   * Provides interfaces to obtain and modify storage data.
86   *
87   * <p>The storage data is stored in a file, which matches only one {@link Storage} instance in the memory.
88   * You can use getStorage to obtain the {@link Storage} instance matching
89   * the file that stores storage data, and use removeStorageFromCache
90   * to remove the {@link Storage} instance from the memory.
91   *
92   * @syscap SystemCapability.DistributedDataManager.Preferences.Core
93   *
94   * @since 6
95   * @deprecated since 9
96   * @useinstead ohos.preferences.preferences
97   */
98  interface Storage {
99    /**
100    * Obtains the value of a storage in the int format.
101    *
102    * <p>If the value is {@code null} or not in the int format, the default value is returned.
103    *
104    * @param key Indicates the key of the storage. It cannot be {@code null} or empty.
105    * @param defValue Indicates the default value to return.
106    * @returns Returns the value matching the specified key if it is found; returns the default value otherwise.
107    * @throws BusinessError if invoked failed
108    * @since 6
109    * @deprecated since 9
110    * @useinstead ohos.preferences.preferences.get
111    */
112    getSync(key: string, defValue: ValueType): ValueType;
113    get(key: string, defValue: ValueType, callback: AsyncCallback<ValueType>): void;
114    get(key: string, defValue: ValueType): Promise<ValueType>;
115
116    /**
117     * Checks whether the {@link Storage} object contains a storage matching a specified key.
118     *
119     * @param key Indicates the key of the storage to check for.
120     * @returns Returns {@code true} if the {@link Storage} object contains a storage with the specified key;
121     * returns {@code false} otherwise.
122     * @throws BusinessError if invoked failed
123     * @since 6
124     * @deprecated since 9
125     * @useinstead ohos.preferences.preferences.has
126     */
127    hasSync(key: string): boolean;
128    has(key: string, callback: AsyncCallback<boolean>): boolean;
129    has(key: string): Promise<boolean>;
130
131    /**
132     * Sets an int value for the key in the {@link Storage} object.
133     *
134     * <p>You can call the {@link #flush} or {@link #flushSync} method to save the {@link Storage} object to the
135     * file.
136     *
137     * @param key Indicates the key of the storage to modify. It cannot be {@code null} or empty.
138     * @param value Indicates the value of the storage.
139     * <tt>MAX_KEY_LENGTH</tt>.
140     * @throws BusinessError if invoked failed
141     * @since 6
142     * @deprecated since 9
143     * @useinstead ohos.preferences.preferences.put
144     */
145    putSync(key: string, value: ValueType): void;
146    put(key: string, value: ValueType, callback: AsyncCallback<void>): void;
147    put(key: string, value: ValueType): Promise<void>;
148
149    /**
150     * Deletes the storage with a specified key from the {@link Storage} object.
151     *
152     * <p>You can call the {@link #flush} method to save the {@link Storage} object to the
153     * file.
154     *
155     * @param key Indicates the key of the storage to delete. It cannot be {@code null} or empty.
156     * <tt>MAX_KEY_LENGTH</tt>.
157     * @throws BusinessError if invoked failed
158     * @since 6
159     * @deprecated since 9
160     * @useinstead ohos.preferences.preferences.delete
161     */
162    deleteSync(key: string): void;
163    delete(key: string, callback: AsyncCallback<void>): void;
164    delete(key: string): Promise<void>;
165
166    /**
167     * Clears all storage from the {@link Storage} object.
168     *
169     * <p>You can call the {@link #flush} method to save the {@link Storage} object to the
170     * file.
171     *
172     * @throws BusinessError if invoked failed
173     * @since 6
174     * @deprecated since 9
175     * @useinstead ohos.preferences.preferences.clear
176     */
177    clearSync(): void;
178    clear(callback: AsyncCallback<void>): void;
179    clear(): Promise<void>;
180
181    /**
182     * Asynchronously saves the {@link Storage} object to the file.
183     *
184     * @throws BusinessError if invoked failed
185     * @since 6
186     * @deprecated since 9
187     * @useinstead ohos.preferences.preferences.flush
188     */
189    flushSync(): void;
190    flush(callback: AsyncCallback<void>): void;
191    flush(): Promise<void>;
192
193    /**
194     * Registers an observer to listen for the change of a {@link Storage} object.
195     *
196     * @param callback Indicates the callback when storage changes.
197     * @throws BusinessError if invoked failed
198     * @since 6
199     * @deprecated since 9
200     * @useinstead ohos.preferences.preferences.on
201     */
202    on(type: 'change', callback: Callback<StorageObserver>): void;
203
204    /**
205     * Unregister an existing observer.
206     *
207     * @param callback Indicates the registered callback.
208     * @throws BusinessError if invoked failed
209     * @since 6
210     * @deprecated since 9
211     * @useinstead ohos.preferences.preferences.off
212     */
213    off(type: 'change', callback: Callback<StorageObserver>): void;
214  }
215
216  /**
217   * Indicates possible value types
218   */
219  type ValueType = number | string | boolean;
220
221  /**
222   * Define the change data information object.
223   *
224   * @syscap SystemCapability.DistributedDataManager.Preferences.Core
225   *
226   * @since 6
227   * @deprecated since 9
228   */
229  interface StorageObserver {
230    /**
231     * Indicates which key changes
232     */
233    key: string;
234  }
235
236  /**
237   * Indicates the maximum length of a key (80 characters).
238   */
239  const MAX_KEY_LENGTH: 80;
240
241  /**
242   * Indicates the maximum length of a string (8192 characters).
243   */
244  const MAX_VALUE_LENGTH: 8192;
245}
246
247export default storage;
248