1e484b35bSopenharmony_ci/*
2e484b35bSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3e484b35bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e484b35bSopenharmony_ci * you may not use this file except in compliance with the License.
5e484b35bSopenharmony_ci * You may obtain a copy of the License at
6e484b35bSopenharmony_ci *
7e484b35bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e484b35bSopenharmony_ci *
9e484b35bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e484b35bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e484b35bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e484b35bSopenharmony_ci * See the License for the specific language governing permissions and
13e484b35bSopenharmony_ci * limitations under the License.
14e484b35bSopenharmony_ci */
15e484b35bSopenharmony_ci
16e484b35bSopenharmony_ciimport { pageMap, PageLinkedMap } from './map';
17e484b35bSopenharmony_ci
18e484b35bSopenharmony_ci/**
19e484b35bSopenharmony_ci * This class defines the information of a application.
20e484b35bSopenharmony_ci */
21e484b35bSopenharmony_ciexport class App {
22e484b35bSopenharmony_ci  public static pageMap: PageLinkedMap = pageMap;
23e484b35bSopenharmony_ci  private _packageName: string;
24e484b35bSopenharmony_ci  private _appInstanceId: string;
25e484b35bSopenharmony_ci  private _events: object;
26e484b35bSopenharmony_ci  private _globalKeys: any[];
27e484b35bSopenharmony_ci  private _appGlobal: ProxyConstructor;
28e484b35bSopenharmony_ci
29e484b35bSopenharmony_ci  constructor(packageName: string, appInstanceId: string) {
30e484b35bSopenharmony_ci    this._packageName = packageName || 'notset';
31e484b35bSopenharmony_ci    this._appInstanceId = appInstanceId;
32e484b35bSopenharmony_ci    this._events = {};
33e484b35bSopenharmony_ci    this._globalKeys = [];
34e484b35bSopenharmony_ci    bindGlobal(this);
35e484b35bSopenharmony_ci  }
36e484b35bSopenharmony_ci
37e484b35bSopenharmony_ci  /**
38e484b35bSopenharmony_ci   * PackageName of this App.
39e484b35bSopenharmony_ci   * @type {string}
40e484b35bSopenharmony_ci   */
41e484b35bSopenharmony_ci  public get packageName() {
42e484b35bSopenharmony_ci    return this._packageName;
43e484b35bSopenharmony_ci  }
44e484b35bSopenharmony_ci
45e484b35bSopenharmony_ci  public set packageName(packageName: string) {
46e484b35bSopenharmony_ci    this._packageName = packageName;
47e484b35bSopenharmony_ci  }
48e484b35bSopenharmony_ci
49e484b35bSopenharmony_ci  /**
50e484b35bSopenharmony_ci   * AppInstanceId of this App.
51e484b35bSopenharmony_ci   * @type {string}
52e484b35bSopenharmony_ci   */
53e484b35bSopenharmony_ci  public get appInstanceId() {
54e484b35bSopenharmony_ci    return this._appInstanceId;
55e484b35bSopenharmony_ci  }
56e484b35bSopenharmony_ci
57e484b35bSopenharmony_ci  public set appInstanceId(appInstanceId: string) {
58e484b35bSopenharmony_ci    this._appInstanceId = appInstanceId;
59e484b35bSopenharmony_ci  }
60e484b35bSopenharmony_ci
61e484b35bSopenharmony_ci  /**
62e484b35bSopenharmony_ci   * GlobalKeys of this App.
63e484b35bSopenharmony_ci   * @type {*}
64e484b35bSopenharmony_ci   */
65e484b35bSopenharmony_ci  public get globalKeys() {
66e484b35bSopenharmony_ci    return this._globalKeys;
67e484b35bSopenharmony_ci  }
68e484b35bSopenharmony_ci
69e484b35bSopenharmony_ci  public set globalKeys(key: any) {
70e484b35bSopenharmony_ci    this._globalKeys.push(key);
71e484b35bSopenharmony_ci  }
72e484b35bSopenharmony_ci
73e484b35bSopenharmony_ci  /**
74e484b35bSopenharmony_ci   * AppGlobal of this App.
75e484b35bSopenharmony_ci   * @type {ProxyConstructor}
76e484b35bSopenharmony_ci   */
77e484b35bSopenharmony_ci  public get appGlobal() {
78e484b35bSopenharmony_ci    return this._appGlobal;
79e484b35bSopenharmony_ci  }
80e484b35bSopenharmony_ci
81e484b35bSopenharmony_ci  public set appGlobal(appGlobal: ProxyConstructor) {
82e484b35bSopenharmony_ci    this._appGlobal = appGlobal;
83e484b35bSopenharmony_ci  }
84e484b35bSopenharmony_ci
85e484b35bSopenharmony_ci  /**
86e484b35bSopenharmony_ci    * Bind life cycle of App.
87e484b35bSopenharmony_ci    * @param {string} type - Function name of life cycle.
88e484b35bSopenharmony_ci    * @param {Function} handler - Function of life cycle.
89e484b35bSopenharmony_ci  */
90e484b35bSopenharmony_ci  public onEvent(type: string, handler: Function): void {
91e484b35bSopenharmony_ci    if (!type || typeof handler !== 'function') {
92e484b35bSopenharmony_ci      return;
93e484b35bSopenharmony_ci    }
94e484b35bSopenharmony_ci    const events: object = this._events;
95e484b35bSopenharmony_ci    const handlerList: Function[] = events[type] || [];
96e484b35bSopenharmony_ci    handlerList.push(handler);
97e484b35bSopenharmony_ci    events[type] = handlerList;
98e484b35bSopenharmony_ci  }
99e484b35bSopenharmony_ci
100e484b35bSopenharmony_ci  /**
101e484b35bSopenharmony_ci   * Emit event.
102e484b35bSopenharmony_ci   * @param {string} type - Event of type.
103e484b35bSopenharmony_ci   * @param {*} errors
104e484b35bSopenharmony_ci   */
105e484b35bSopenharmony_ci  public emitEvent(type: string, errors?: any): void {
106e484b35bSopenharmony_ci    const events: object = this._events;
107e484b35bSopenharmony_ci    const handlerList: Function[] = events[type];
108e484b35bSopenharmony_ci
109e484b35bSopenharmony_ci    if (handlerList) {
110e484b35bSopenharmony_ci      handlerList.forEach((handler) => {
111e484b35bSopenharmony_ci        handler.call(global.aceapp, errors);
112e484b35bSopenharmony_ci      });
113e484b35bSopenharmony_ci    }
114e484b35bSopenharmony_ci  }
115e484b35bSopenharmony_ci
116e484b35bSopenharmony_ci  /**
117e484b35bSopenharmony_ci   * Delete globalKeys of App.
118e484b35bSopenharmony_ci   */
119e484b35bSopenharmony_ci  public deleteGlobalKeys(): void {
120e484b35bSopenharmony_ci    if (this._globalKeys) {
121e484b35bSopenharmony_ci      let i: number = this._globalKeys.length;
122e484b35bSopenharmony_ci      while (i--) {
123e484b35bSopenharmony_ci        const key: any = this._globalKeys.splice(i, 1)[0];
124e484b35bSopenharmony_ci        if (key === 'setTimeout') {
125e484b35bSopenharmony_ci          global[key] = undefined;
126e484b35bSopenharmony_ci        } else {
127e484b35bSopenharmony_ci          delete global[key];
128e484b35bSopenharmony_ci        }
129e484b35bSopenharmony_ci      }
130e484b35bSopenharmony_ci    }
131e484b35bSopenharmony_ci  }
132e484b35bSopenharmony_ci
133e484b35bSopenharmony_ci  /**
134e484b35bSopenharmony_ci   * Assign timerAPIs to appGlobal.
135e484b35bSopenharmony_ci   * @param {Object} timerAPIs - TimerAPI.
136e484b35bSopenharmony_ci   */
137e484b35bSopenharmony_ci  public setTimer(timerAPIs: object): void {
138e484b35bSopenharmony_ci    const that = this;
139e484b35bSopenharmony_ci    Object.keys(timerAPIs).forEach((api) => {
140e484b35bSopenharmony_ci      that._appGlobal[api] = timerAPIs[api];
141e484b35bSopenharmony_ci    });
142e484b35bSopenharmony_ci  }
143e484b35bSopenharmony_ci}
144e484b35bSopenharmony_ci
145e484b35bSopenharmony_ci/**
146e484b35bSopenharmony_ci * Assign appGlobal of App.
147e484b35bSopenharmony_ci * @param {App} app - App instance.
148e484b35bSopenharmony_ci */
149e484b35bSopenharmony_cifunction bindGlobal(app: App): void {
150e484b35bSopenharmony_ci  app.appGlobal = new Proxy(Object.create(global), {
151e484b35bSopenharmony_ci    set(target, key, value, receiver) {
152e484b35bSopenharmony_ci      const ret: boolean = Reflect.set(target, key, value, receiver);
153e484b35bSopenharmony_ci      if (receiver[key] === target[key]) {
154e484b35bSopenharmony_ci        // set in app Global
155e484b35bSopenharmony_ci        global[key] = value;
156e484b35bSopenharmony_ci        app.globalKeys = key;
157e484b35bSopenharmony_ci      }
158e484b35bSopenharmony_ci      return ret;
159e484b35bSopenharmony_ci    }
160e484b35bSopenharmony_ci  });
161e484b35bSopenharmony_ci}
162