1/* 2* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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*/ 15import * as vscode from 'vscode'; 16import { Callback } from "../common/define" 17import { Logger } from "../common/log"; 18export abstract class IModel { 19 abstract name: string; 20 uri: vscode.Uri | undefined = undefined; 21 callbacks: Map<string, Callback> = new Map(); 22 23 abstract init(uri: vscode.Uri): void; 24 abstract doStart(): void; 25 abstract doStop(): void; 26 abstract doPause(): void; 27 abstract doResume(): void; 28 29 public onEvent(event: string, cb: Callback): void { 30 this.callbacks.set(event, cb); 31 }; 32 33 public offEvent(event: string): void { 34 this.callbacks.delete(event); 35 }; 36 37 public emmitAllEvent(...args: any[]): void { 38 this.callbacks.forEach(callback => callback(...args)); 39 } 40 41 public emmitEventForKey(event: string, ...args: any[]): void { 42 const callback = this.callbacks.get(event); 43 if (callback) { 44 callback(...args); 45 } else { 46 Logger.getInstance().error(`No callback found with key "${event}".`); 47 } 48 } 49}