16e80583aSopenharmony_ci/**
26e80583aSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
36e80583aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
46e80583aSopenharmony_ci * you may not use this file except in compliance with the License.
56e80583aSopenharmony_ci * You may obtain a copy of the License at
66e80583aSopenharmony_ci *
76e80583aSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
86e80583aSopenharmony_ci *
96e80583aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
106e80583aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
116e80583aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
126e80583aSopenharmony_ci * See the License for the specific language governing permissions and
136e80583aSopenharmony_ci * limitations under the License.
146e80583aSopenharmony_ci */
156e80583aSopenharmony_ci
166e80583aSopenharmony_ciimport { Log } from '../utils/Log';
176e80583aSopenharmony_ciimport { CommonConstants } from '../constants/CommonConstants';
186e80583aSopenharmony_ci
196e80583aSopenharmony_ciconst TAG = 'LocalEventManager';
206e80583aSopenharmony_ci
216e80583aSopenharmony_ci/**
226e80583aSopenharmony_ci * Local event management class
236e80583aSopenharmony_ci * main duty:
246e80583aSopenharmony_ci * 1.Registration and deregistration of event listeners
256e80583aSopenharmony_ci * 2.distribution of events
266e80583aSopenharmony_ci */
276e80583aSopenharmony_ciclass LocalEventManager {
286e80583aSopenharmony_ci  private mEventListenerMap: Object = {};
296e80583aSopenharmony_ci
306e80583aSopenharmony_ci  private mEventMsgCache: Object = {};
316e80583aSopenharmony_ci
326e80583aSopenharmony_ci  /**
336e80583aSopenharmony_ci   * Get the local event management class object
346e80583aSopenharmony_ci   *
356e80583aSopenharmony_ci   * @return Single instance of local event management class object
366e80583aSopenharmony_ci   */
376e80583aSopenharmony_ci  static getInstance(): LocalEventManager {
386e80583aSopenharmony_ci    if (globalThis.localEventManager == null) {
396e80583aSopenharmony_ci      globalThis.localEventManager = new LocalEventManager();
406e80583aSopenharmony_ci    }
416e80583aSopenharmony_ci    return globalThis.localEventManager;
426e80583aSopenharmony_ci  }
436e80583aSopenharmony_ci
446e80583aSopenharmony_ci  /**
456e80583aSopenharmony_ci   * register listener
466e80583aSopenharmony_ci   *
476e80583aSopenharmony_ci   * @param listener
486e80583aSopenharmony_ci   * @param events
496e80583aSopenharmony_ci   */
506e80583aSopenharmony_ci  registerEventListener(listener, events: string[]): void {
516e80583aSopenharmony_ci    Log.showInfo(TAG, `registerEventListener events: ${JSON.stringify(events)}`);
526e80583aSopenharmony_ci    if (listener != null && events != null) {
536e80583aSopenharmony_ci      for (let index = 0; index < events.length; index++) {
546e80583aSopenharmony_ci        const event: string = events[index];
556e80583aSopenharmony_ci        if (this.mEventListenerMap[event] == undefined) {
566e80583aSopenharmony_ci          this.mEventListenerMap[event] = new Array<any>();
576e80583aSopenharmony_ci        }
586e80583aSopenharmony_ci        if (this.mEventListenerMap[event].indexOf(listener) === CommonConstants.INVALID_VALUE) {
596e80583aSopenharmony_ci          this.mEventListenerMap[event].push(listener);
606e80583aSopenharmony_ci        }
616e80583aSopenharmony_ci      }
626e80583aSopenharmony_ci    }
636e80583aSopenharmony_ci  }
646e80583aSopenharmony_ci
656e80583aSopenharmony_ci  /**
666e80583aSopenharmony_ci   * unregister listener
676e80583aSopenharmony_ci   *
686e80583aSopenharmony_ci   * @param listener
696e80583aSopenharmony_ci   */
706e80583aSopenharmony_ci  unregisterEventListener(listener): void {
716e80583aSopenharmony_ci    Log.showInfo(TAG, 'unregisterEventListener event listener');
726e80583aSopenharmony_ci    for(const key in this.mEventListenerMap) {
736e80583aSopenharmony_ci      const listenerList: any[] = this.mEventListenerMap[key];
746e80583aSopenharmony_ci      const index: number = listenerList.indexOf(listener);
756e80583aSopenharmony_ci      if (index != CommonConstants.INVALID_VALUE) {
766e80583aSopenharmony_ci        this.mEventListenerMap[key].splice(index, 1);
776e80583aSopenharmony_ci      }
786e80583aSopenharmony_ci    }
796e80583aSopenharmony_ci  }
806e80583aSopenharmony_ci
816e80583aSopenharmony_ci  /**
826e80583aSopenharmony_ci   * Send local broadcasts synchronously
836e80583aSopenharmony_ci   *
846e80583aSopenharmony_ci   * @param event
856e80583aSopenharmony_ci   * @param params
866e80583aSopenharmony_ci   */
876e80583aSopenharmony_ci  sendLocalEvent(event, params?): void {
886e80583aSopenharmony_ci    Log.showInfo(TAG, `sendLocalEvent event: ${JSON.stringify(event)}`);
896e80583aSopenharmony_ci    let listenerList = this.mEventListenerMap[event];
906e80583aSopenharmony_ci    if (listenerList != undefined) {
916e80583aSopenharmony_ci      Log.showDebug(TAG, `sendLocalEvent listenerList length: ${listenerList.length}`);
926e80583aSopenharmony_ci      for (let listener of listenerList) {
936e80583aSopenharmony_ci        listener.onReceiveEvent(event, params);
946e80583aSopenharmony_ci      }
956e80583aSopenharmony_ci    } else {
966e80583aSopenharmony_ci      Log.showInfo(TAG, 'sendLocalEvent, send local event with no receiver');
976e80583aSopenharmony_ci    }
986e80583aSopenharmony_ci  }
996e80583aSopenharmony_ci
1006e80583aSopenharmony_ci  /**
1016e80583aSopenharmony_ci   * Send local broadcast asynchronously
1026e80583aSopenharmony_ci   *
1036e80583aSopenharmony_ci   * @param event
1046e80583aSopenharmony_ci   * @param params
1056e80583aSopenharmony_ci   */
1066e80583aSopenharmony_ci  async sendLocalEventAsync(event, params?): Promise<void> {
1076e80583aSopenharmony_ci    this.sendLocalEvent(event, params);
1086e80583aSopenharmony_ci  }
1096e80583aSopenharmony_ci
1106e80583aSopenharmony_ci  /**
1116e80583aSopenharmony_ci   * Send sticky local broadcast (async only)
1126e80583aSopenharmony_ci   *
1136e80583aSopenharmony_ci   * @param event
1146e80583aSopenharmony_ci   * @param params
1156e80583aSopenharmony_ci   */
1166e80583aSopenharmony_ci  async sendLocalEventSticky(event, params): Promise<void> {
1176e80583aSopenharmony_ci    Log.showDebug(TAG, `sendLocalEventSticky, send local event sticky, params: ${JSON.stringify(params)}`);
1186e80583aSopenharmony_ci    this.sendLocalEvent(event, params);
1196e80583aSopenharmony_ci    this.mEventMsgCache[event] = params;
1206e80583aSopenharmony_ci  }
1216e80583aSopenharmony_ci}
1226e80583aSopenharmony_ci
1236e80583aSopenharmony_ciexport const localEventManager = LocalEventManager.getInstance();
124