Browse Source
将 `BasicCommand`、`TabsCommand` 和 `UpdateCommand` 从主进程迁移到 `common/event` 目录下,统一管理命令模块。同时,重构了 `PlatForm` 模块,使其使用新的 `ApiFactory` 进行 API 调用,提升了代码的可维护性和复用性。feat/icon
26 changed files with 444 additions and 312 deletions
@ -1,13 +1,11 @@ |
|||
import { Container, ContainerModule } from "inversify" |
|||
import BasicCommand from "./BasicCommand" |
|||
import TabsCommand from "./TabsCommand" |
|||
import UpdateCommand from "./UpdateCommand" |
|||
|
|||
// TODO 考虑迁移,将所有命令都注册common/event中
|
|||
import UpdateCommand from "common/event/Update/main/command" |
|||
import PlatFormCommand from "common/event/PlatForm/main/command" |
|||
import TabsCommand from "common/event/Tabs/main/command" |
|||
|
|||
const modules = new ContainerModule(bind => { |
|||
bind("BasicCommand").to(BasicCommand).inSingletonScope() |
|||
bind("TabsCommand").to(TabsCommand).inSingletonScope() |
|||
bind("PlatFormCommand").to(PlatFormCommand).inSingletonScope() |
|||
bind("UpdateCommand").to(UpdateCommand).inSingletonScope() |
|||
}) |
|||
|
@ -1,7 +1,7 @@ |
|||
import { inject } from "inversify" |
|||
import Updater from "main/modules/updater" |
|||
|
|||
export default class BasicCommand { |
|||
export default class PlatFormCommand { |
|||
constructor(@inject(Updater) private _Updater: Updater) {} |
|||
|
|||
async triggerHotUpdate() { |
@ -1,5 +1,5 @@ |
|||
import { broadcast } from "main/utils" |
|||
import { AllKeys } from "../common" |
|||
import { AllKeys } from "common/event/common" |
|||
|
|||
function emitHotUpdateReady(...argu) { |
|||
broadcast<AllKeys>("hot-update-ready", ...argu) |
@ -0,0 +1,53 @@ |
|||
import { ElectronApiClient } from "common/lib/electron" |
|||
import { BrowserApiClient } from "common/lib/browser" |
|||
|
|||
// 定义抽象 API 接口
|
|||
export interface IApiClient { |
|||
call<T = any>(command: string, ...args: any[]): Promise<T> |
|||
on<K extends string>(channel: K, callback: (...args: any[]) => void): void |
|||
off<K extends string>(channel: K, callback: (...args: any[]) => void): void |
|||
offAll<K extends string>(channel: K): void |
|||
} |
|||
|
|||
class NullApiClient implements IApiClient { |
|||
async call<T = any>(command: string, ...args: any[]): Promise<T> { |
|||
args |
|||
console.warn(`API call to ${command} failed: API client not initialized`) |
|||
return undefined as any |
|||
} |
|||
|
|||
on<K extends string>(channel: K, callback: (...args: any[]) => void): void { |
|||
callback |
|||
console.warn(`Failed to register listener for ${channel}: API client not initialized`) |
|||
} |
|||
|
|||
off<K extends string>(channel: K, callback: (...args: any[]) => void): void { |
|||
callback |
|||
console.warn(`Failed to unregister listener for ${channel}: API client not initialized`) |
|||
} |
|||
|
|||
offAll<K extends string>(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 客户端
|
|||
if (window.api && window.electron) { |
|||
this.instance = new ElectronApiClient() |
|||
} else { |
|||
this.instance = new BrowserApiClient() |
|||
} |
|||
} |
|||
return this.instance |
|||
} |
|||
} |
@ -0,0 +1,29 @@ |
|||
import { IApiClient } from "./abstract" |
|||
|
|||
export class BrowserApiClient implements IApiClient { |
|||
call<T = any>(command: string, ...args: any[]): Promise<T> { |
|||
// 浏览器特定实现,可能使用 fetch 或其他方式
|
|||
const [service, method] = command.split(".") |
|||
return fetch(`/api/${service}/${method}`, { |
|||
method: "POST", |
|||
body: JSON.stringify(args), |
|||
headers: { "Content-Type": "application/json" }, |
|||
}).then(res => res.json()) |
|||
} |
|||
|
|||
// 实现其他方法...
|
|||
on<K extends string>(channel: K, callback: (...args: any[]) => void): void { |
|||
// 浏览器中可能使用 WebSocket 或其他方式
|
|||
console.log("不支持 on 方法", channel, callback) |
|||
} |
|||
|
|||
off<K extends string>(channel: K, callback: (...args: any[]) => void): void { |
|||
// 相应的解绑实现
|
|||
console.log("不支持 on 方法", channel, callback) |
|||
} |
|||
|
|||
offAll<K extends string>(channel: K): void { |
|||
// 相应的全部解绑实现
|
|||
console.log("不支持 on 方法", channel) |
|||
} |
|||
} |
@ -0,0 +1,20 @@ |
|||
import { IApiClient } from "./abstract" |
|||
|
|||
export class ElectronApiClient implements IApiClient { |
|||
call<T = any>(command: string, ...args: any[]): Promise<T> { |
|||
// Electron 特定实现
|
|||
return window.api.call(command, ...args) |
|||
} |
|||
|
|||
on<K extends string>(channel: K, callback: (...args: any[]) => void): void { |
|||
window.api.on(channel, callback) |
|||
} |
|||
|
|||
off<K extends string>(channel: K, callback: (...args: any[]) => void): void { |
|||
window.api.off(channel, callback) |
|||
} |
|||
|
|||
offAll<K extends string>(channel: K): void { |
|||
window.api.offAll(channel) |
|||
} |
|||
} |
@ -1,12 +0,0 @@ |
|||
// import { inject } from "inversify"
|
|||
// import Setting from "main/modules/setting"
|
|||
|
|||
// class SettingCommand {
|
|||
// constructor(@inject(Setting) private _Setting: Setting) {
|
|||
// console.log(this._Setting)
|
|||
// }
|
|||
|
|||
// getAll() {
|
|||
// return this._Setting.config()
|
|||
// }
|
|||
// }
|
Loading…
Reference in new issue