• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023-2023 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 {Log} from '../utils/Log';
17import {GlobalThisStorageKey} from './GlobalThisStorageKey';
18
19const TAG = 'GlobalThisHelper';
20
21export class GlobalThisHelper {
22  private static registerKeys = [
23    GlobalThisStorageKey.KEY_PRINT_ADAPTER,
24    GlobalThisStorageKey.KEY_MEDIA_SIZE_UTIL,
25    GlobalThisStorageKey.KEY_MAIN_ABILITY_CONTEXT,
26    GlobalThisStorageKey.KEY_MAIN_ABILITY_WINDOW_STAGE,
27    GlobalThisStorageKey.KEY_JOB_MANAGER_ABILITY_CONTEXT,
28    GlobalThisStorageKey.KEY_NOTIFICATION_PRINTER_NAME,
29    GlobalThisStorageKey.KEY_SERVICE_FIRST_START,
30    GlobalThisStorageKey.KEY_PRINTER_SELECT_DIALOG_OPEN,
31    GlobalThisStorageKey.KEY_CURRENT_PIXELMAP,
32    GlobalThisStorageKey.KEY_PREFERENCES_ADAPTER,
33    GlobalThisStorageKey.KEY_SECURITY_GUARD,
34    GlobalThisStorageKey.KEY_IMAGE_ERROR_COUNT,
35    GlobalThisStorageKey.KEY_IMAGE_ERROR_NAME,
36    GlobalThisStorageKey.KEY_ABILITY_CONTEXT,
37    GlobalThisStorageKey.KEY_FILES_DIR
38  ];
39
40  /**
41   * 将变量value挂载到globalThis实例上,key需要先注册到registerKeys,否则挂载失败
42   * 注意本接口可保存原对象
43   *
44   * @param value 变量值
45   * @param storageKey 保存的key值
46   * @param forceChange
47   * @returns 挂载成功返回value,失败返回undefined
48   */
49  public static createValue<T>(value: T, storageKey: string, forceChange: boolean = false): T {
50    const element = GlobalThisHelper.registerKeys.find((ele) => ele === storageKey);
51    if (element === undefined) {
52      Log.error(TAG, 'Can not find register storageKey: ' + JSON.stringify(storageKey));
53      return undefined;
54    }
55    if ((globalThis[storageKey] === undefined) || forceChange) {
56      globalThis[storageKey] = value;
57      Log.error(TAG, 'GlobalThisHelper Create key of ' + JSON.stringify(storageKey));
58    }
59    return <T>(globalThis[storageKey]);
60  }
61
62  /**
63   * 将变量value挂载到AppStorage实例上,key需要先注册到registerKeys,否则挂载失败
64   * 注意本接口重新new了一个新对象,如需保存原对象不要使用本接口
65   *
66   * @returns 挂载成功返回value,失败返回undefined
67   */
68  public static createObject<T>(objectClass: { new(...input): T }, storageKey: string, ...input): T {
69    const element = GlobalThisHelper.registerKeys.find((ele) => ele === storageKey);
70    if (element === undefined) {
71      Log.error(TAG, 'Can not find register storageKey: ' + JSON.stringify(storageKey));
72      return undefined;
73    }
74    if (globalThis[storageKey] === undefined) {
75      globalThis[storageKey] = new objectClass(...input);
76      Log.error(TAG, 'GlobalThisHelper Create key of ' + JSON.stringify(storageKey));
77    }
78
79    // 通过globalThis存取值
80    return (globalThis[storageKey] as T);
81  }
82
83  /**
84   * 获取挂载到globalThis上的全局变量
85   *
86   * @returns 成功返回value,若之前未挂载则返回undefined
87   */
88  public static getValue<T>(storageKey: string): T {
89    if (globalThis[storageKey] === undefined) {
90      Log.error(TAG, 'The storageKey is not exist, key = ' + storageKey);
91      return undefined;
92    }
93    return (globalThis[storageKey] as T);
94  }
95}