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 { pageMap, PageLinkedMap } from './map'; 17 18/** 19 * This class defines the information of a application. 20 */ 21export class App { 22 public static pageMap: PageLinkedMap = pageMap; 23 private _packageName: string; 24 private _appInstanceId: string; 25 private _events: object; 26 private _globalKeys: any[]; 27 private _appGlobal: ProxyConstructor; 28 29 constructor(packageName: string, appInstanceId: string) { 30 this._packageName = packageName || 'notset'; 31 this._appInstanceId = appInstanceId; 32 this._events = {}; 33 this._globalKeys = []; 34 bindGlobal(this); 35 } 36 37 /** 38 * PackageName of this App. 39 * @type {string} 40 */ 41 public get packageName() { 42 return this._packageName; 43 } 44 45 public set packageName(packageName: string) { 46 this._packageName = packageName; 47 } 48 49 /** 50 * AppInstanceId of this App. 51 * @type {string} 52 */ 53 public get appInstanceId() { 54 return this._appInstanceId; 55 } 56 57 public set appInstanceId(appInstanceId: string) { 58 this._appInstanceId = appInstanceId; 59 } 60 61 /** 62 * GlobalKeys of this App. 63 * @type {*} 64 */ 65 public get globalKeys() { 66 return this._globalKeys; 67 } 68 69 public set globalKeys(key: any) { 70 this._globalKeys.push(key); 71 } 72 73 /** 74 * AppGlobal of this App. 75 * @type {ProxyConstructor} 76 */ 77 public get appGlobal() { 78 return this._appGlobal; 79 } 80 81 public set appGlobal(appGlobal: ProxyConstructor) { 82 this._appGlobal = appGlobal; 83 } 84 85 /** 86 * Bind life cycle of App. 87 * @param {string} type - Function name of life cycle. 88 * @param {Function} handler - Function of life cycle. 89 */ 90 public onEvent(type: string, handler: Function): void { 91 if (!type || typeof handler !== 'function') { 92 return; 93 } 94 const events: object = this._events; 95 const handlerList: Function[] = events[type] || []; 96 handlerList.push(handler); 97 events[type] = handlerList; 98 } 99 100 /** 101 * Emit event. 102 * @param {string} type - Event of type. 103 * @param {*} errors 104 */ 105 public emitEvent(type: string, errors?: any): void { 106 const events: object = this._events; 107 const handlerList: Function[] = events[type]; 108 109 if (handlerList) { 110 handlerList.forEach((handler) => { 111 handler.call(global.aceapp, errors); 112 }); 113 } 114 } 115 116 /** 117 * Delete globalKeys of App. 118 */ 119 public deleteGlobalKeys(): void { 120 if (this._globalKeys) { 121 let i: number = this._globalKeys.length; 122 while (i--) { 123 const key: any = this._globalKeys.splice(i, 1)[0]; 124 if (key === 'setTimeout') { 125 global[key] = undefined; 126 } else { 127 delete global[key]; 128 } 129 } 130 } 131 } 132 133 /** 134 * Assign timerAPIs to appGlobal. 135 * @param {Object} timerAPIs - TimerAPI. 136 */ 137 public setTimer(timerAPIs: object): void { 138 const that = this; 139 Object.keys(timerAPIs).forEach((api) => { 140 that._appGlobal[api] = timerAPIs[api]; 141 }); 142 } 143} 144 145/** 146 * Assign appGlobal of App. 147 * @param {App} app - App instance. 148 */ 149function bindGlobal(app: App): void { 150 app.appGlobal = new Proxy(Object.create(global), { 151 set(target, key, value, receiver) { 152 const ret: boolean = Reflect.set(target, key, value, receiver); 153 if (receiver[key] === target[key]) { 154 // set in app Global 155 global[key] = value; 156 app.globalKeys = key; 157 } 158 return ret; 159 } 160 }); 161} 162