• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-2024 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
16class EventHub {
17  constructor() {
18    this.eventMap = {};
19  }
20
21  on(event, callback) {
22    if ((typeof (event) !== 'string') || (typeof (callback) !== 'function')) {
23      return;
24    }
25    if (!this.eventMap[event]) {
26      this.eventMap[event] = [];
27    }
28    if (this.eventMap[event].indexOf(callback) === -1) {
29      this.eventMap[event].push(callback);
30    }
31  }
32
33  off(event, callback) {
34    if (typeof (event) !== 'string') {
35      return;
36    }
37    if (this.eventMap[event]) {
38      if (callback) {
39        let cbArray = this.eventMap[event];
40        let index = cbArray.indexOf(callback);
41        if (index > -1) {
42          for (; index + 1 < cbArray.length; index++) {
43            cbArray[index] = cbArray[index + 1];
44          }
45          cbArray.pop();
46        }
47      } else {
48        delete this.eventMap[event];
49      }
50    }
51  }
52
53  emit(event, ...args) {
54    if (typeof (event) !== 'string') {
55      return;
56    }
57    if (this.eventMap[event]) {
58      const cloneArray = [...this.eventMap[event]];
59      const len = cloneArray.length;
60      for (let i = 0; i < len; ++i) {
61        cloneArray[i].apply(this, args);
62      }
63    }
64  }
65}
66
67class Context {
68  constructor(obj) {
69    this.__context_impl__ = obj;
70    this.__context_impl__.eventHub = new EventHub();
71  }
72
73  createBundleContext(bundleName) {
74    return this.__context_impl__.createBundleContext(bundleName);
75  }
76
77  createModuleContext(moduleName) {
78    return this.__context_impl__.createModuleContext(moduleName);
79  }
80
81  createModuleContext(bundleName, moduleName) {
82    return this.__context_impl__.createModuleContext(bundleName, moduleName);
83  }
84
85  createSystemHspModuleResourceManager(bundleName, moduleName) {
86    return this.__context_impl__.createSystemHspModuleResourceManager(bundleName, moduleName);
87  }
88
89  createModuleResourceManager(bundleName, moduleName) {
90    return this.__context_impl__.createModuleResourceManager(bundleName, moduleName);
91  }
92
93  getApplicationContext() {
94    return this.__context_impl__.getApplicationContext();
95  }
96
97  getGroupDir(groupId, callback) {
98    return this.__context_impl__.getGroupDir(groupId, callback);
99  }
100
101  createAreaModeContext(areaMode) {
102    return this.__context_impl__.createAreaModeContext(areaMode);
103  }
104
105  createDisplayContext(displayId) {
106    return this.__context_impl__.createDisplayContext(displayId);
107  }
108
109  set area(mode) {
110    return this.__context_impl__.switchArea(mode);
111  }
112
113  get area() {
114    return this.__context_impl__.getArea();
115  }
116
117  get resourceManager() {
118    return this.__context_impl__.resourceManager;
119  }
120
121  get applicationInfo() {
122    return this.__context_impl__.applicationInfo;
123  }
124
125  get cacheDir() {
126    return this.__context_impl__.cacheDir;
127  }
128
129  get tempDir() {
130    return this.__context_impl__.tempDir;
131  }
132
133  get resourceDir() {
134    return this.__context_impl__.resourceDir;
135  }
136
137  get filesDir() {
138    return this.__context_impl__.filesDir;
139  }
140
141  get distributedFilesDir() {
142    return this.__context_impl__.distributedFilesDir;
143  }
144
145  get databaseDir() {
146    return this.__context_impl__.databaseDir;
147  }
148
149  get preferencesDir() {
150    return this.__context_impl__.preferencesDir;
151  }
152
153  get bundleCodeDir() {
154    return this.__context_impl__.bundleCodeDir;
155  }
156
157  get cloudFileDir() {
158    return this.__context_impl__.cloudFileDir;
159  }
160
161  get eventHub() {
162    return this.__context_impl__.eventHub;
163  }
164
165  get processName() {
166    return this.__context_impl__.processName;
167  }
168
169  get stageMode() {
170    return true;
171  }
172}
173
174export default Context;
175