Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Commit

Permalink
feat(core): stop and kill commands can take multiple uid's
Browse files Browse the repository at this point in the history
Allows stopping stopping or killing multiple instances.
  • Loading branch information
markmcdowell committed Dec 21, 2020
1 parent 84eeeb6 commit 0406ee0
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 40 deletions.
6 changes: 5 additions & 1 deletion packages/desktop-core/src/main/commands/kill/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown, string[]>(ReservedChannels.instances_kill, { uid });

killed.forEach((id) => {
console.log(id);
});

app.exit();
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface IKillOptions {
readonly context?: string;
readonly uid: string;
readonly uid: string[];
}
6 changes: 3 additions & 3 deletions packages/desktop-core/src/main/commands/kill/module.ts
Original file line number Diff line number Diff line change
@@ -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,
},
};
6 changes: 5 additions & 1 deletion packages/desktop-core/src/main/commands/stop/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown, string[]>(ReservedChannels.instances_stop, { uid });

stopped.forEach((id) => {
console.log(id);
});

app.exit();
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface IStopOptions {
readonly context?: string;
readonly uid: string;
readonly uid: string[];
}
6 changes: 3 additions & 3 deletions packages/desktop-core/src/main/commands/stop/module.ts
Original file line number Diff line number Diff line change
@@ -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,
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,27 @@ export class CompositeInstanceService implements IInstanceService {
break;
}
}

return configuration;
}

public list() {
return this.instanceServices.flatMap((i) => i.list());
}

public kill(uid: string): Promise<void> {
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) {
Expand All @@ -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();
}
}
4 changes: 2 additions & 2 deletions packages/desktop-core/src/main/instances/iInstanceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IConfiguration } from "@reactivemarkets/desktop-types";
export interface IInstanceService {
get(uid: string): IConfiguration | undefined;
list(): IConfiguration[];
kill(uid: string): Promise<void>;
kill(uid: string[]): Promise<string[]>;
restart(uid: string): Promise<void>;
stop(uid: string): Promise<void>;
stop(uid: string[]): Promise<string[]>;
}
16 changes: 9 additions & 7 deletions packages/desktop-core/src/main/instances/trayInstanceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
21 changes: 14 additions & 7 deletions packages/desktop-core/src/main/instances/windowInstanceService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { IInstanceService } from "./iInstanceService";

import { windowService } from "../windowing";

export class WindowInstanceService implements IInstanceService {
Expand All @@ -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) {
Expand All @@ -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);
}
}
4 changes: 3 additions & 1 deletion packages/desktop-core/src/main/tray/defaultTrayService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
9 changes: 8 additions & 1 deletion packages/desktop-core/src/main/tray/trayInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -17,4 +19,9 @@ export class TrayInstance {
public get instance() {
return this.#instance;
}

public destroy() {
this.#instance.destroy();
this.#onDestroy();
}
}

0 comments on commit 0406ee0

Please sign in to comment.