1/** 2 * Copyright (c) 2023 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 */ 15export class Callback { 16 // message 方法接受一个字符串或数字类型的参数,并返回 void 17 message(message: string | number): void{ 18 // 在这里实现回调逻辑 19 console.log(`Callback received message: ${message}`); 20 } 21} 22 23export class MessageManager { 24 callback?: Callback | null 25 callbacks:Map<string,Callback> = new Map() 26 27 constructor() { 28 29 } 30 31 registerCallback(callback:Callback , type?:string) { 32 if (type == undefined) { 33 console.error('registerCallback callback set') 34 this.callback = callback 35 return 36 } 37 this.callbacks.set(type,callback) 38 } 39 40 notify(message:string, type?:string) { 41 if (type == undefined) { 42 this.callback!.message 43 return 44 } 45 46 let tmpCallback:Callback | undefined = this.callbacks.get(type) 47 if (tmpCallback === undefined) { 48 console.error('callbacks has no callback for type ' + type) 49 return 50 } 51 tmpCallback.message(message) 52 } 53 54 clear() { 55 this.callbacks.clear() 56 this.callback = null 57 } 58}