• 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    set area(mode) {
84        return this.__context_impl__.switchArea(mode)
85    }
86
87    get area() {
88        return this.__context_impl__.getArea()
89    }
90
91    get resourceManager() {
92        return this.__context_impl__.resourceManager
93    }
94
95    get applicationInfo() {
96        return this.__context_impl__.applicationInfo
97    }
98
99    get cacheDir() {
100        return this.__context_impl__.cacheDir
101    }
102
103    get tempDir() {
104        return this.__context_impl__.tempDir
105    }
106
107    get filesDir() {
108        return this.__context_impl__.filesDir
109    }
110
111    get distributedFilesDir() {
112        return this.__context_impl__.distributedFilesDir
113    }
114
115    get databaseDir() {
116        return this.__context_impl__.databaseDir
117    }
118
119    get preferencesDir() {
120        return this.__context_impl__.preferencesDir
121    }
122
123    get bundleCodeDir() {
124        return this.__context_impl__.bundleCodeDir
125    }
126
127    get eventHub() {
128        return this.__context_impl__.eventHub
129    }
130
131    get stageMode() {
132        return true;
133    }
134}
135
136export default Context
137