1/** 2 * Copyright (c) 2024 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 interface Callback { 16 (message:any):void 17} 18 19export class MessageManager { 20 callback:Callback 21 callbacks:Map<string,Callback> = new Map() 22 23 constructor() { 24 25 } 26 27 registerCallback(callback:Callback , type?:string) { 28 if (type == undefined) { 29 console.error('registerCallback callback set') 30 this.callback = callback 31 return 32 } 33 this.callbacks.set(type,callback) 34 } 35 36 notify(message:any, type?:string) { 37 if (type == undefined) { 38 this.callback(message) 39 return 40 } 41 42 let tmpCallback:Callback = this.callbacks.get(type) 43 if (tmpCallback === undefined) { 44 console.error('callbacks has no callback for type ' + type) 45 return 46 } 47 tmpCallback(message) 48 } 49 50 clear() { 51 this.callbacks.clear() 52 this.callback = null 53 } 54}