100aff185Sopenharmony_ci/* 200aff185Sopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 300aff185Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 400aff185Sopenharmony_ci * you may not use this file except in compliance with the License. 500aff185Sopenharmony_ci * You may obtain a copy of the License at 600aff185Sopenharmony_ci * 700aff185Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 800aff185Sopenharmony_ci * 900aff185Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1000aff185Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1100aff185Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1200aff185Sopenharmony_ci * See the License for the specific language governing permissions and 1300aff185Sopenharmony_ci * limitations under the License. 1400aff185Sopenharmony_ci */ 1500aff185Sopenharmony_ci 1600aff185Sopenharmony_ciexport class BroadCast { 1700aff185Sopenharmony_ci private callBackArray: Map<string, Function[]> = new Map(); 1800aff185Sopenharmony_ci 1900aff185Sopenharmony_ci constructor() { 2000aff185Sopenharmony_ci } 2100aff185Sopenharmony_ci 2200aff185Sopenharmony_ci public on(event: string, callback: Function): void { 2300aff185Sopenharmony_ci let cbs = this.callBackArray.get(event); 2400aff185Sopenharmony_ci if (!cbs) { 2500aff185Sopenharmony_ci cbs = new Array<Function>(); 2600aff185Sopenharmony_ci this.callBackArray.set(event, cbs); 2700aff185Sopenharmony_ci } 2800aff185Sopenharmony_ci cbs.push(callback); 2900aff185Sopenharmony_ci } 3000aff185Sopenharmony_ci 3100aff185Sopenharmony_ci public off(event: string, callback?: Function): void { 3200aff185Sopenharmony_ci if (!event) { 3300aff185Sopenharmony_ci this.callBackArray = new Map(); 3400aff185Sopenharmony_ci return; 3500aff185Sopenharmony_ci } 3600aff185Sopenharmony_ci if (!callback) { 3700aff185Sopenharmony_ci this.callBackArray.delete(event); 3800aff185Sopenharmony_ci return; 3900aff185Sopenharmony_ci } 4000aff185Sopenharmony_ci 4100aff185Sopenharmony_ci const cbs = this.callBackArray.get(event); 4200aff185Sopenharmony_ci if (!cbs) { 4300aff185Sopenharmony_ci return; 4400aff185Sopenharmony_ci } 4500aff185Sopenharmony_ci let length = cbs.length; 4600aff185Sopenharmony_ci for (let i = 0; i < length; i++) { 4700aff185Sopenharmony_ci let cb = cbs[i]; 4800aff185Sopenharmony_ci if (cb === callback) { 4900aff185Sopenharmony_ci cbs.splice(i, 1); 5000aff185Sopenharmony_ci break; 5100aff185Sopenharmony_ci } 5200aff185Sopenharmony_ci } 5300aff185Sopenharmony_ci } 5400aff185Sopenharmony_ci 5500aff185Sopenharmony_ci public emit(event: string, args: unknown[]): void { 5600aff185Sopenharmony_ci let cbs = this.callBackArray.get(event); 5700aff185Sopenharmony_ci if (!cbs) { 5800aff185Sopenharmony_ci return; 5900aff185Sopenharmony_ci } 6000aff185Sopenharmony_ci let l = cbs.length; 6100aff185Sopenharmony_ci for (let i = 0; i < l; i++) { 6200aff185Sopenharmony_ci try { 6300aff185Sopenharmony_ci cbs[i].apply(this, args); 6400aff185Sopenharmony_ci } catch (e) { 6500aff185Sopenharmony_ci new Error(e); 6600aff185Sopenharmony_ci } 6700aff185Sopenharmony_ci } 6800aff185Sopenharmony_ci } 6900aff185Sopenharmony_ci 7000aff185Sopenharmony_ci public release(): void { 7100aff185Sopenharmony_ci this.callBackArray.clear(); 7200aff185Sopenharmony_ci } 7300aff185Sopenharmony_ci}