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 { Log } from '../utils/Log'; 17import { CommonConstants } from '../constants/CommonConstants'; 18 19const TAG = 'LocalEventManager'; 20 21/** 22 * Local event management class 23 * main duty: 24 * 1.Registration and deregistration of event listeners 25 * 2.distribution of events 26 */ 27class LocalEventManager { 28 private mEventListenerMap: Object = {}; 29 30 private mEventMsgCache: Object = {}; 31 32 /** 33 * Get the local event management class object 34 * 35 * @return Single instance of local event management class object 36 */ 37 static getInstance(): LocalEventManager { 38 if (globalThis.localEventManager == null) { 39 globalThis.localEventManager = new LocalEventManager(); 40 } 41 return globalThis.localEventManager; 42 } 43 44 /** 45 * register listener 46 * 47 * @param listener 48 * @param events 49 */ 50 registerEventListener(listener, events: string[]): void { 51 Log.showInfo(TAG, `registerEventListener events: ${JSON.stringify(events)}`); 52 if (listener != null && events != null) { 53 for (let index = 0; index < events.length; index++) { 54 const event: string = events[index]; 55 if (this.mEventListenerMap[event] == undefined) { 56 this.mEventListenerMap[event] = new Array<any>(); 57 } 58 if (this.mEventListenerMap[event].indexOf(listener) === CommonConstants.INVALID_VALUE) { 59 this.mEventListenerMap[event].push(listener); 60 } 61 } 62 } 63 } 64 65 /** 66 * unregister listener 67 * 68 * @param listener 69 */ 70 unregisterEventListener(listener): void { 71 Log.showInfo(TAG, 'unregisterEventListener event listener'); 72 for(const key in this.mEventListenerMap) { 73 const listenerList: any[] = this.mEventListenerMap[key]; 74 const index: number = listenerList.indexOf(listener); 75 if (index != CommonConstants.INVALID_VALUE) { 76 this.mEventListenerMap[key].splice(index, 1); 77 } 78 } 79 } 80 81 /** 82 * Send local broadcasts synchronously 83 * 84 * @param event 85 * @param params 86 */ 87 sendLocalEvent(event, params?): void { 88 Log.showInfo(TAG, `sendLocalEvent event: ${JSON.stringify(event)}`); 89 let listenerList = this.mEventListenerMap[event]; 90 if (listenerList != undefined) { 91 Log.showDebug(TAG, `sendLocalEvent listenerList length: ${listenerList.length}`); 92 for (let listener of listenerList) { 93 listener.onReceiveEvent(event, params); 94 } 95 } else { 96 Log.showInfo(TAG, 'sendLocalEvent, send local event with no receiver'); 97 } 98 } 99 100 /** 101 * Send local broadcast asynchronously 102 * 103 * @param event 104 * @param params 105 */ 106 async sendLocalEventAsync(event, params?): Promise<void> { 107 this.sendLocalEvent(event, params); 108 } 109 110 /** 111 * Send sticky local broadcast (async only) 112 * 113 * @param event 114 * @param params 115 */ 116 async sendLocalEventSticky(event, params): Promise<void> { 117 Log.showDebug(TAG, `sendLocalEventSticky, send local event sticky, params: ${JSON.stringify(params)}`); 118 this.sendLocalEvent(event, params); 119 this.mEventMsgCache[event] = params; 120 } 121} 122 123export const localEventManager = LocalEventManager.getInstance(); 124