1/** 2 * Copyright (c) 2021-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 CommonEvent from '@ohos.commonEvent'; 17import { AsyncCallback } from 'basic'; 18import { CommonEventData } from 'commonEvent/commonEventData'; 19import { CommonEventSubscriber } from 'commonEvent/commonEventSubscriber'; 20 21const TAG = 'CommonEventManager'; 22 23/** 24 * Wrapper class for CommonEvent. 25 */ 26class CommonEventManager { 27 RECENT_FULL_SCREEN = 'CREATE_RECENT_WINDOW_EVENT'; 28 RECENT_SPLIT_SCREEN = 'common.event.SPLIT_SCREEN'; 29 30 private callbackList: AsyncCallback<CommonEventData>[] = []; 31 private subscriberList: CommonEventSubscriber[] = []; 32 33 /** 34 * get CommonEventManager instance 35 * 36 * @return CommonEventManager singleton 37 */ 38 static getInstance(): CommonEventManager { 39 if (globalThis.CommonEventManager == null) { 40 globalThis.CommonEventManager = new CommonEventManager(); 41 } 42 return globalThis.CommonEventManager; 43 } 44 45 private constructor() { 46 } 47 48 /** 49 * Register common event listener. 50 */ 51 public registerCommonEvent(subscriber: CommonEventSubscriber, eventCallback: AsyncCallback<CommonEventData>): void { 52 if (this.subscriberList.indexOf(subscriber) != -1) { 53 return; 54 } 55 CommonEvent.subscribe(subscriber, eventCallback); 56 this.subscriberList.push(subscriber); 57 this.callbackList.push(eventCallback); 58 } 59 60 /** 61 * Unregister common event listener. 62 */ 63 public unregisterCommonEvent(subscriber: CommonEventSubscriber, eventCallback: AsyncCallback<CommonEventData>): void { 64 const subscriberIndex: number = this.subscriberList.indexOf(subscriber); 65 const callbackIndex: number = this.callbackList.indexOf(eventCallback); 66 if (subscriberIndex != -1) { 67 CommonEvent.unsubscribe(subscriber); 68 this.subscriberList.splice(subscriberIndex, 1); 69 } 70 callbackIndex != -1 && this.callbackList.splice(callbackIndex, 1); 71 } 72} 73 74const commonEventManager = CommonEventManager.getInstance(); 75export default commonEventManager; 76