19b256929Sopenharmony_ci/* 29b256929Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 39b256929Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 49b256929Sopenharmony_ci * you may not use this file except in compliance with the License. 59b256929Sopenharmony_ci * You may obtain a copy of the License at 69b256929Sopenharmony_ci * 79b256929Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 89b256929Sopenharmony_ci * 99b256929Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 109b256929Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 119b256929Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 129b256929Sopenharmony_ci * See the License for the specific language governing permissions and 139b256929Sopenharmony_ci * limitations under the License. 149b256929Sopenharmony_ci */ 159b256929Sopenharmony_ciimport {Log} from "../Log"; 169b256929Sopenharmony_ci 179b256929Sopenharmony_ciexport type Callback = (args: any) => void; 189b256929Sopenharmony_ciconst TAG = "EventBus"; 199b256929Sopenharmony_ci 209b256929Sopenharmony_ciexport interface EventBus<T> { 219b256929Sopenharmony_ci on(event: T | T[], cb: Callback): () => void; 229b256929Sopenharmony_ci once(event: T, cb: Callback): () => void; 239b256929Sopenharmony_ci off(event: T | T[] | undefined, cb: Callback): void; 249b256929Sopenharmony_ci emit(event: T, args: any): void; 259b256929Sopenharmony_ci} 269b256929Sopenharmony_ci 279b256929Sopenharmony_ciexport function createEventBus<T extends string>(): EventBus<T> { 289b256929Sopenharmony_ci let _cbs: { [key: string]: Set<Callback> } = {}; 299b256929Sopenharmony_ci 309b256929Sopenharmony_ci function on(events: T | T[], cb: Callback): () => void { 319b256929Sopenharmony_ci if (Array.isArray(events)) { 329b256929Sopenharmony_ci events.forEach((e) => on(e, cb)); 339b256929Sopenharmony_ci } else { 349b256929Sopenharmony_ci (_cbs[events] || (_cbs[events] = new Set())).add(cb); 359b256929Sopenharmony_ci Log.showInfo(TAG, `add event[${events}] callback, size: ${_cbs[events]?.size}`); 369b256929Sopenharmony_ci } 379b256929Sopenharmony_ci return () => off(events, cb); 389b256929Sopenharmony_ci } 399b256929Sopenharmony_ci 409b256929Sopenharmony_ci function once(event: T, cb: Callback): () => void { 419b256929Sopenharmony_ci let newCallback = (args: any) => { 429b256929Sopenharmony_ci cb(args); 439b256929Sopenharmony_ci removeSelf(); 449b256929Sopenharmony_ci }; 459b256929Sopenharmony_ci function removeSelf() { 469b256929Sopenharmony_ci off(event, newCallback); 479b256929Sopenharmony_ci } 489b256929Sopenharmony_ci return on(event, newCallback); 499b256929Sopenharmony_ci } 509b256929Sopenharmony_ci 519b256929Sopenharmony_ci function off(event: T | T[] | undefined, cb: Callback) { 529b256929Sopenharmony_ci if (!event) { 539b256929Sopenharmony_ci _cbs = {}; 549b256929Sopenharmony_ci Log.showInfo(TAG, `remove event[${event}] all callback`); 559b256929Sopenharmony_ci return; 569b256929Sopenharmony_ci } 579b256929Sopenharmony_ci if (Array.isArray(event)) { 589b256929Sopenharmony_ci event.forEach((e) => off(e, cb)); 599b256929Sopenharmony_ci return; 609b256929Sopenharmony_ci } 619b256929Sopenharmony_ci _cbs[event]?.delete(cb); 629b256929Sopenharmony_ci Log.showInfo(TAG, `remove event[${event}] callback, size: ${_cbs[event]?.size}`); 639b256929Sopenharmony_ci } 649b256929Sopenharmony_ci 659b256929Sopenharmony_ci function emit(event: T, args: any) { 669b256929Sopenharmony_ci _cbs[event]?.forEach((cb) => cb(args)); 679b256929Sopenharmony_ci } 689b256929Sopenharmony_ci 699b256929Sopenharmony_ci function stickyEmit(event: T, argument: any[]) {} 709b256929Sopenharmony_ci return { 719b256929Sopenharmony_ci on, 729b256929Sopenharmony_ci once, 739b256929Sopenharmony_ci off, 749b256929Sopenharmony_ci emit, 759b256929Sopenharmony_ci }; 769b256929Sopenharmony_ci}