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