1/*
2 * Copyright (c) 2021-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 { Logger } from "../util/HiLogger"
17
18/**
19 * [系统全局globalThis管理类,挂载全局变量]
20 */
21
22const logger: Logger = new Logger("GlobalHelper")
23
24// 支持注册的全局对象名称
25export const globalKeys = {
26  // 全局设备属性对象
27  deviceInfo: 'deviceInfo',
28  // ability对象
29  app: 'app',
30  // sp管理对象
31  preferenceManager: 'preferenceManager',
32  // 安全提示
33  audioPickerPreference: 'audioPickerPreference'
34}
35
36/**
37 * 设置或者已经创建就返回全局的global对象
38 *
39 * @param objectClass 类型
40 * @param storageKey key
41 * @param params 参数数组
42 * @return storageObject
43 */
44export function createOrGet<T>(objectClass: { new(...params: any[]): T }, storageKey: string, params?: any[]): T {
45  if (!globalKeys[storageKey]) {
46    logger.error(`Cannot create key of ${storageKey}`)
47    return undefined
48  }
49  if (!globalThis[storageKey]) {
50    if (params) {
51      globalThis[storageKey] = new objectClass(...params)
52    } else {
53      globalThis[storageKey] = new objectClass()
54    }
55    logger.info(`Create key of ${storageKey}${JSON.stringify(params)}`)
56  }
57  return globalThis[storageKey]
58}
59
60/**
61 * 设置并返回全局的global对象
62 *
63 * @param objectClass 类型
64 * @param storageKey key
65 * @param params 参数数组
66 * @return storageObject
67 */
68export function create<T>(objectClass: { new(...params: any[]): T }, storageKey: string, params?: any[]): T {
69  if (!globalKeys[storageKey]) {
70    logger.error(`Cannot create key of ${storageKey}`)
71    return undefined
72  }
73  if (params) {
74    globalThis[storageKey] = new objectClass(...params)
75  } else {
76    globalThis[storageKey] = new objectClass()
77  }
78  logger.info(`Create key of ${storageKey}${JSON.stringify(params)}`)
79  return globalThis[storageKey]
80}
81
82/**
83 * 设置全局的global对象
84 *
85 * @param object 类型
86 * @param storageKey key
87 * @return storageObject
88 */
89export function setGlobalObj<T>(object: T, storageKey: string): T {
90  globalThis[storageKey] = object;
91  logger.info(`Set key of ${storageKey}`)
92  return globalThis[storageKey]
93}
94
95/**
96 * 删除全局的global对象
97 *
98 * @param storageKey key
99 */
100export function delGlobalObj(storageKey: string): void {
101  if (!globalKeys[storageKey]) {
102    logger.error(`Cannot set key of ${storageKey}`)
103    return
104  }
105  logger.info(`delGlobalObj key of ${storageKey}`)
106  delete globalThis[storageKey]
107}
108
109/**
110 * 获取设置过的global对象
111 *
112 * @param object 类型
113 * @param storageKey key
114 * @return storageObject
115 */
116export function getGlobalObj<T>(storageKey: string): T {
117  if (!globalKeys[storageKey]) {
118    logger.error(`Cannot get key of ${storageKey}`)
119    return undefined
120  }
121  return globalThis[storageKey]
122}
123