Skip to content

Commit

Permalink
fix(main-app): send ipc crash (#1242)
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackHole1 committed Dec 24, 2021
1 parent 459f538 commit 22bcc47
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion desktop/main-app/src/utils/ipc-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export const injectionWindowIPCAction = (customWindow: CustomWindow): void => {
.getWin(args.browserWindowID);

if (realCustomWindow) {
windowActionAsync(customWindow)[args.actions](args.args);
windowActionAsync(realCustomWindow)[args.actions](args.args);
}
},
);
Expand Down
4 changes: 4 additions & 0 deletions desktop/main-app/src/utils/window-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export const windowHookClose = (customWindow: CustomWindow): void => {
});
};

export const windowHookClosed = (customWindow: CustomWindow, cb: Function): void => {
customWindow.window.on("closed", cb);
};

export const windowReadyToShow = (customWindow: CustomWindow): void => {
customWindow.window.on("ready-to-show", () => {
if (customWindow.options.isPortal) {
Expand Down
33 changes: 27 additions & 6 deletions desktop/main-app/src/window-manager/abstract.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { constants } from "flat-types";
import { BrowserWindow, BrowserWindowConstructorOptions } from "electron";
import { windowHookClose, windowOpenDevTools, windowReadyToShow } from "../utils/window-event";
import {
windowHookClose,
windowHookClosed,
windowOpenDevTools,
windowReadyToShow,
} from "../utils/window-event";
import {
defaultBrowserWindowOptions,
defaultWindowOptions,
Expand All @@ -17,16 +22,28 @@ export abstract class AbstractWindow<MULTI_INSTANCE extends boolean> {
public readonly name: constants.WindowsName,
) {}

public remove(id: number): void {
const win = this.getWin(id);
public remove(id: number | CustomWindow): void {
const win = typeof id === "number" ? this.getWin(id) : id;

if (win === null) {
return;
}

AbstractWindow.closeWindow(win);

this.wins = this.isMultiInstance ? this.wins.filter(win => win.window.id !== id) : [];
if (this.isMultiInstance) {
this.wins = this.wins.filter(({ window }) => {
if (window.isDestroyed()) {
return false;
}

return window.id !== id;
});

return;
}

this.wins = [];
}

protected createWindow(
Expand Down Expand Up @@ -62,6 +79,10 @@ export abstract class AbstractWindow<MULTI_INSTANCE extends boolean> {
windowOpenDevTools(win);

windowHookClose(win);
windowHookClosed(win, () => {
// sync this.wins
this.remove(win);
});

windowReadyToShow(win);

Expand All @@ -77,14 +98,14 @@ export abstract class AbstractWindow<MULTI_INSTANCE extends boolean> {
if (this.isMultiInstance) {
const id = ids[0];
for (const win of this.wins) {
if (win.window.id === id) {
if (!win.window.isDestroyed() && win.window.id === id) {
return win;
}
}
return null;
}

return this.wins[0] || null;
return this.wins[0].window.isDestroyed() ? null : this.wins[0];
}

public isEmpty(): boolean {
Expand Down
2 changes: 1 addition & 1 deletion desktop/main-app/src/window-manager/window-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class WindowManager<
}

public remove(customWindow: CustomWindow): void {
this.wins[customWindow.options.name].remove(customWindow.window.id);
this.wins[customWindow.options.name].remove(customWindow);
}

private interceptPortalNewWindow(customWindow: CustomWindow): void {
Expand Down

0 comments on commit 22bcc47

Please sign in to comment.