1/* 2 * Copyright (c) 2021-2023 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 set area(mode) { 102 return this.__context_impl__.switchArea(mode); 103 } 104 105 get area() { 106 return this.__context_impl__.getArea(); 107 } 108 109 get resourceManager() { 110 return this.__context_impl__.resourceManager; 111 } 112 113 get applicationInfo() { 114 return this.__context_impl__.applicationInfo; 115 } 116 117 get cacheDir() { 118 return this.__context_impl__.cacheDir; 119 } 120 121 get tempDir() { 122 return this.__context_impl__.tempDir; 123 } 124 125 get resourceDir() { 126 return this.__context_impl__.resourceDir; 127 } 128 129 get filesDir() { 130 return this.__context_impl__.filesDir; 131 } 132 133 get distributedFilesDir() { 134 return this.__context_impl__.distributedFilesDir; 135 } 136 137 get databaseDir() { 138 return this.__context_impl__.databaseDir; 139 } 140 141 get preferencesDir() { 142 return this.__context_impl__.preferencesDir; 143 } 144 145 get bundleCodeDir() { 146 return this.__context_impl__.bundleCodeDir; 147 } 148 149 get cloudFileDir() { 150 return this.__context_impl__.cloudFileDir; 151 } 152 153 get eventHub() { 154 return this.__context_impl__.eventHub; 155 } 156 157 get stageMode() { 158 return true; 159 } 160} 161 162export default Context; 163