import { ElectronApiClient } from "./electron" import { BrowserApiClient } from "./browser" import { BaseSingleton } from "base/index" // 定义抽象 API 接口 export interface IApiClient { call(command: string, ...args: any[]): Promise callSync(command: string, ...args: any[]): void on(channel: K, callback: (...args: any[]) => void): void off(channel: K, callback: (...args: any[]) => void): void offAll(channel: K): void } class NullApiClient implements IApiClient { async call(command: string, ...args: any[]): Promise { args console.warn(`API call to ${command} failed: API client not initialized`) return undefined as any } callSync(command: string, ...args: any[]): void { args console.warn(`API callSync to ${command} failed: API client not initialized`) return undefined as any } on(channel: K, callback: (...args: any[]) => void): void { callback console.warn(`Failed to register listener for ${channel}: API client not initialized`) } off(channel: K, callback: (...args: any[]) => void): void { callback console.warn(`Failed to unregister listener for ${channel}: API client not initialized`) } offAll(channel: K): void { console.warn(`Failed to unregister all listeners for ${channel}: API client not initialized`) } } // 创建 API 工厂 export class ApiFactory { private static instance: IApiClient = new NullApiClient() // 默认使用空实现 static setApiClient(client: IApiClient) { this.instance = client } static getApiClient(): IApiClient { if (this.instance instanceof NullApiClient) { // 根据环境选择合适的 API 客户端 // @ts-ignore 忽略类型检查 if (window.api && window.electron) { this.instance = new ElectronApiClient() } else { this.instance = new BrowserApiClient() } } return this.instance } } export class BaseEvent extends BaseSingleton { constructor() { super() } public get api() { return ApiFactory.getApiClient() } }