diff --git a/packages/desktop-core/src/main/commands/kill/handler.ts b/packages/desktop-core/src/main/commands/kill/handler.ts index d8de7eb3..d6008a41 100644 --- a/packages/desktop-core/src/main/commands/kill/handler.ts +++ b/packages/desktop-core/src/main/commands/kill/handler.ts @@ -10,7 +10,11 @@ export const handler = async ({ context, uid }: IKillOptions) => { try { await ipcExternal.whenReady(context); - await ipcExternal.invoke(ReservedChannels.instances_kill, { uid }); + const killed = await ipcExternal.invoke(ReservedChannels.instances_kill, { uid }); + + killed.forEach((id) => { + console.log(id); + }); app.exit(); } catch (error) { diff --git a/packages/desktop-core/src/main/commands/kill/iKillOptions.ts b/packages/desktop-core/src/main/commands/kill/iKillOptions.ts index fc223b0e..de9f04e9 100644 --- a/packages/desktop-core/src/main/commands/kill/iKillOptions.ts +++ b/packages/desktop-core/src/main/commands/kill/iKillOptions.ts @@ -1,4 +1,4 @@ export interface IKillOptions { readonly context?: string; - readonly uid: string; + readonly uid: string[]; } diff --git a/packages/desktop-core/src/main/commands/kill/module.ts b/packages/desktop-core/src/main/commands/kill/module.ts index d13d95c2..43ef2551 100644 --- a/packages/desktop-core/src/main/commands/kill/module.ts +++ b/packages/desktop-core/src/main/commands/kill/module.ts @@ -1,15 +1,15 @@ export { handler } from "./handler"; -export const command = "kill [uid]"; +export const command = "kill [uid...]"; export const aliases = ["destroy"]; -export const describe = "Kill a running instance"; +export const describe = "Kill one or more running instances"; export const builder = { uid: { + array: true, demandOption: true, describe: "The application uid", - string: true, }, }; diff --git a/packages/desktop-core/src/main/commands/stop/handler.ts b/packages/desktop-core/src/main/commands/stop/handler.ts index e2494655..55247eca 100644 --- a/packages/desktop-core/src/main/commands/stop/handler.ts +++ b/packages/desktop-core/src/main/commands/stop/handler.ts @@ -10,7 +10,11 @@ export const handler = async ({ context, uid }: IStopOptions) => { try { await ipcExternal.whenReady(context); - await ipcExternal.invoke(ReservedChannels.instances_stop, { uid }); + const stopped = await ipcExternal.invoke(ReservedChannels.instances_stop, { uid }); + + stopped.forEach((id) => { + console.log(id); + }); app.exit(); } catch (error) { diff --git a/packages/desktop-core/src/main/commands/stop/iStopOptions.ts b/packages/desktop-core/src/main/commands/stop/iStopOptions.ts index 92268bea..005c9910 100644 --- a/packages/desktop-core/src/main/commands/stop/iStopOptions.ts +++ b/packages/desktop-core/src/main/commands/stop/iStopOptions.ts @@ -1,4 +1,4 @@ export interface IStopOptions { readonly context?: string; - readonly uid: string; + readonly uid: string[]; } diff --git a/packages/desktop-core/src/main/commands/stop/module.ts b/packages/desktop-core/src/main/commands/stop/module.ts index cca0ae12..813576d1 100644 --- a/packages/desktop-core/src/main/commands/stop/module.ts +++ b/packages/desktop-core/src/main/commands/stop/module.ts @@ -1,15 +1,15 @@ export { handler } from "./handler"; -export const command = "stop [uid]"; +export const command = "stop [uid...]"; export const aliases = ["close", "rm"]; -export const describe = "Stop a running instance"; +export const describe = "Stop one or more running instances"; export const builder = { uid: { demandOption: true, describe: "The application uid", - string: true, + array: true, }, }; diff --git a/packages/desktop-core/src/main/instances/compositeInstanceService.ts b/packages/desktop-core/src/main/instances/compositeInstanceService.ts index 84330eda..b61374ba 100644 --- a/packages/desktop-core/src/main/instances/compositeInstanceService.ts +++ b/packages/desktop-core/src/main/instances/compositeInstanceService.ts @@ -16,6 +16,7 @@ export class CompositeInstanceService implements IInstanceService { break; } } + return configuration; } @@ -23,13 +24,19 @@ export class CompositeInstanceService implements IInstanceService { return this.instanceServices.flatMap((i) => i.list()); } - public kill(uid: string): Promise { - const service = this.instanceServices.find((i) => i.get(uid)); - if (service === undefined) { - throw new Error(`Couldn't find instance with identifier: ${uid}`); - } + public async kill(uid: string[]) { + const services = uid + .map((id) => { + const service = this.instanceServices.find((i) => i.get(id)); + + return service?.kill([id]); + }) + .filter((func) => func !== undefined) + .map((func) => func!); + + const killed = await Promise.all(services); - return service.kill(uid); + return killed.flat(); } public restart(uid: string) { @@ -41,12 +48,18 @@ export class CompositeInstanceService implements IInstanceService { return service.restart(uid); } - public stop(uid: string) { - const service = this.instanceServices.find((i) => i.get(uid)); - if (service === undefined) { - throw new Error(`Couldn't find instance with identifier: ${uid}`); - } + public async stop(uid: string[]) { + const services = uid + .map((id) => { + const service = this.instanceServices.find((i) => i.get(id)); + + return service?.stop([id]); + }) + .filter((func) => func !== undefined) + .map((func) => func!); + + const stopped = await Promise.all(services); - return service.stop(uid); + return stopped.flat(); } } diff --git a/packages/desktop-core/src/main/instances/iInstanceService.ts b/packages/desktop-core/src/main/instances/iInstanceService.ts index 83af0113..075bdea4 100644 --- a/packages/desktop-core/src/main/instances/iInstanceService.ts +++ b/packages/desktop-core/src/main/instances/iInstanceService.ts @@ -3,7 +3,7 @@ import { IConfiguration } from "@reactivemarkets/desktop-types"; export interface IInstanceService { get(uid: string): IConfiguration | undefined; list(): IConfiguration[]; - kill(uid: string): Promise; + kill(uid: string[]): Promise; restart(uid: string): Promise; - stop(uid: string): Promise; + stop(uid: string[]): Promise; } diff --git a/packages/desktop-core/src/main/instances/trayInstanceService.ts b/packages/desktop-core/src/main/instances/trayInstanceService.ts index 4b5dd8f7..da5cd188 100644 --- a/packages/desktop-core/src/main/instances/trayInstanceService.ts +++ b/packages/desktop-core/src/main/instances/trayInstanceService.ts @@ -10,19 +10,21 @@ export class TrayInstanceService implements IInstanceService { return trayService.all().map((instance) => instance.configuration); } - public kill(uid: string) { - trayService.from(uid)?.instance.destroy(); + public kill(uid: string[]) { + const killed = uid.map((id) => { + trayService.from(id)?.destroy(); - return Promise.resolve(); + return id; + }); + + return Promise.resolve(killed); } public restart() { return Promise.resolve(); } - public stop(uid: string) { - trayService.from(uid)?.instance.destroy(); - - return Promise.resolve(); + public stop(uid: string[]) { + return this.kill(uid); } } diff --git a/packages/desktop-core/src/main/instances/windowInstanceService.ts b/packages/desktop-core/src/main/instances/windowInstanceService.ts index 1459e98d..41ce79dd 100644 --- a/packages/desktop-core/src/main/instances/windowInstanceService.ts +++ b/packages/desktop-core/src/main/instances/windowInstanceService.ts @@ -1,5 +1,4 @@ import { IInstanceService } from "./iInstanceService"; - import { windowService } from "../windowing"; export class WindowInstanceService implements IInstanceService { @@ -11,10 +10,14 @@ export class WindowInstanceService implements IInstanceService { return windowService.all().map((instance) => instance.configuration); } - public kill(uid: string) { - windowService.from(uid)?.instance.destroy(); + public kill(uid: string[]) { + const killed = uid.map((id) => { + windowService.from(id)?.instance.destroy(); - return Promise.resolve(); + return id; + }); + + return Promise.resolve(killed); } public restart(uid: string) { @@ -23,9 +26,13 @@ export class WindowInstanceService implements IInstanceService { return Promise.resolve(); } - public stop(uid: string) { - windowService.from(uid)?.instance.close(); + public stop(uid: string[]) { + const stopped = uid.map((id) => { + windowService.from(id)?.instance.close(); - return Promise.resolve(); + return id; + }); + + return Promise.resolve(stopped); } } diff --git a/packages/desktop-core/src/main/tray/defaultTrayService.ts b/packages/desktop-core/src/main/tray/defaultTrayService.ts index bbc23239..5c3d57af 100644 --- a/packages/desktop-core/src/main/tray/defaultTrayService.ts +++ b/packages/desktop-core/src/main/tray/defaultTrayService.ts @@ -49,7 +49,9 @@ export class DefaultTrayService implements ITrayService { }, }; - const instance = new TrayInstance(runningConfiguration, tray); + const instance = new TrayInstance(runningConfiguration, tray, () => { + this.configRegistry.delete(uid); + }); this.configRegistry.set(uid, instance); diff --git a/packages/desktop-core/src/main/tray/trayInstance.ts b/packages/desktop-core/src/main/tray/trayInstance.ts index fccb2254..8ec2016f 100644 --- a/packages/desktop-core/src/main/tray/trayInstance.ts +++ b/packages/desktop-core/src/main/tray/trayInstance.ts @@ -4,10 +4,12 @@ import { Tray } from "electron"; export class TrayInstance { #configuration: IConfiguration; #instance: Tray; + #onDestroy: () => void; - public constructor(configuration: IConfiguration, instance: Tray) { + public constructor(configuration: IConfiguration, instance: Tray, onDestroy: () => void) { this.#configuration = configuration; this.#instance = instance; + this.#onDestroy = onDestroy; } public get configuration() { @@ -17,4 +19,9 @@ export class TrayInstance { public get instance() { return this.#instance; } + + public destroy() { + this.#instance.destroy(); + this.#onDestroy(); + } }