1/* 2 * Copyright (c) 2022 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 16import { EventBus } from './EventBus' 17 18export default class EventBusManager { 19 // The global eventbus of the application process. Event registration and destruction should be paired 20 private appEventBus: EventBus; 21 22 constructor() { 23 this.appEventBus = new EventBus(); 24 } 25 26 public static getInstance(): EventBusManager { 27 if (!globalThis?.sInstanceEventBus) { 28 globalThis.sInstanceEventBus = new EventBusManager(); 29 console.info('EventBusManager create a new EventBus') 30 } 31 return globalThis.sInstanceEventBus; 32 } 33 34 public static getMainInstance(): EventBusManager { 35 if (!globalThis?.mInstanceEventBus) { 36 globalThis.mInstanceEventBus = new EventBusManager(); 37 } 38 return globalThis.mInstanceEventBus; 39 } 40 41 public static getWorkerInstance(): EventBusManager { 42 if (!globalThis?.wInstanceEventBus) { 43 globalThis.wInstanceEventBus = new EventBusManager(); 44 } 45 return globalThis.wInstanceEventBus; 46 } 47 48 public static getCameraInstance(): EventBusManager { 49 if (!globalThis?.cInstanceEventBus) { 50 globalThis.cInstanceEventBus = new EventBusManager(); 51 } 52 return globalThis.cInstanceEventBus; 53 } 54 55 public getEventBus(): EventBus { 56 return this.appEventBus; 57 } 58}