• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
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 index = this.eventMap[event].indexOf(callback);
40        if (index > -1) {
41          this.eventMap[event].splice(index, 1);
42        }
43      } else {
44        this.eventMap[event].length = 0;
45      }
46    }
47  }
48
49  emit(event, ...args) {
50    if (typeof (event) !== 'string') {
51      return;
52    }
53    if (this.eventMap[event]) {
54      this.eventMap[event].map((callback) => {
55        callback(...args);
56      });
57    }
58  }
59}
60
61class Context {
62  constructor(obj) {
63    this.__context_impl__ = obj;
64    this.__context_impl__.eventHub = new EventHub();
65  }
66
67  createBundleContext(bundleName) {
68    return this.__context_impl__.createBundleContext(bundleName);
69  }
70
71  createModuleContext(moduleName) {
72    return this.__context_impl__.createModuleContext(moduleName);
73  }
74
75  createModuleContext(bundleName, moduleName) {
76    return this.__context_impl__.createModuleContext(bundleName, moduleName);
77  }
78
79  getApplicationContext() {
80    return this.__context_impl__.getApplicationContext();
81  }
82
83  getGroupDir(groupId, callback) {
84    return this.__context_impl__.getGroupDir(groupId, callback);
85  }
86
87  set area(mode) {
88    return this.__context_impl__.switchArea(mode);
89  }
90
91  get area() {
92    return this.__context_impl__.getArea();
93  }
94
95  get resourceManager() {
96    return this.__context_impl__.resourceManager;
97  }
98
99  get applicationInfo() {
100    return this.__context_impl__.applicationInfo;
101  }
102
103  get cacheDir() {
104    return this.__context_impl__.cacheDir;
105  }
106
107  get tempDir() {
108    return this.__context_impl__.tempDir;
109  }
110
111  get filesDir() {
112    return this.__context_impl__.filesDir;
113  }
114
115  get distributedFilesDir() {
116    return this.__context_impl__.distributedFilesDir;
117  }
118
119  get databaseDir() {
120    return this.__context_impl__.databaseDir;
121  }
122
123  get preferencesDir() {
124    return this.__context_impl__.preferencesDir;
125  }
126
127  get bundleCodeDir() {
128    return this.__context_impl__.bundleCodeDir;
129  }
130
131  get eventHub() {
132    return this.__context_impl__.eventHub;
133  }
134
135  get stageMode() {
136    return true;
137  }
138}
139
140export default Context;
141