Skip to content

Commit

Permalink
fix(dev): BrowserWindow lifecycle (code harmonization) Follows PR #2640
Browse files Browse the repository at this point in the history
  • Loading branch information
danielweck committed Nov 13, 2024
1 parent 79a62f3 commit 3eef469
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 17 deletions.
6 changes: 3 additions & 3 deletions src/main/di.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ const saveLibraryWindowInDi =
const getLibraryWindowFromDi =
() => {
ok(libraryWin, "library window not defined");
return libraryWin;
return libraryWin; // we could filter out based on win.isDestroyed() && win.webContents.isDestroyed() but this would change the null/undefined contract of the return value in consumer code, so let's leave it for now (strictNullChecks and stricter typeof id)
};

const readerWinMap = new Map<string, BrowserWindow>();
Expand All @@ -240,7 +240,7 @@ const saveReaderWindowInDi =
(readerWin: BrowserWindow, id: string) => (readerWinMap.set(id, readerWin), readerWin);

const getReaderWindowFromDi =
(id: string) => readerWinMap.get(id);
(id: string) => readerWinMap.get(id); // we could filter out based on win.isDestroyed() && win.webContents.isDestroyed() but this would change the null/undefined contract of the return value in consumer code, so let's leave it for now (strictNullChecks and stricter typeof id)

const getAllReaderWindowFromDi =
() => {
Expand All @@ -249,7 +249,7 @@ const getAllReaderWindowFromDi =
// return container.getAll<BrowserWindow>("WIN_REGISTRY_READER");

return Array.from(readerWinMap.values()).filter((w) => {
return !w.isDestroyed();
return !w.isDestroyed() && !w.webContents.isDestroyed();
});
};

Expand Down
9 changes: 5 additions & 4 deletions src/main/redux/middleware/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ export const reduxSyncMiddleware: Middleware
if (readers[key]) {
try {
const readerWin = getReaderWindowFromDi(readers[key].identifier);
if (!readerWin.isDestroyed() && !readerWin.webContents.isDestroyed())
browserWin.set(readers[key].identifier, readerWin);
if (readerWin && !readerWin.isDestroyed() && !readerWin.webContents.isDestroyed()) {
browserWin.set(readers[key].identifier, readerWin);
}
} catch (_err) {
// ignore
debug("ERROR: Can't found ther reader win from di: ", readers[key].identifier);
Expand All @@ -151,7 +152,7 @@ export const reduxSyncMiddleware: Middleware
const a = ActionSerializer.serialize(action as ActionWithSender);
// debug(a);
try {
if (!win.isDestroyed() && !win.webContents.isDestroyed())
if (!win.isDestroyed() && !win.webContents.isDestroyed()) {
win.webContents.send(syncIpc.CHANNEL, {
type: syncIpc.EventType.MainAction,
payload: {
Expand All @@ -161,7 +162,7 @@ export const reduxSyncMiddleware: Middleware
type: SenderType.Main,
},
} as syncIpc.EventPayload);

}
} catch (error) {
debug("ERROR in SYNC ACTION", error);
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/redux/sagas/annotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ function* importAnnotationSet(action: annotationActions.importAnnotationSet.TAct
const annotationToUpdateOld = annotations.find(({ uuid }) => uuid === annotationToUpdate.uuid);
const a = ActionSerializer.serialize(readerActions.annotation.update.build(annotationToUpdateOld, annotationToUpdate));
try {
if (!readerWin.isDestroyed() && !readerWin.webContents.isDestroyed())
if (readerWin && !readerWin.isDestroyed() && !readerWin.webContents.isDestroyed()) {
readerWin.webContents.send(syncIpc.CHANNEL, {
type: syncIpc.EventType.MainAction,
payload: {
Expand All @@ -357,6 +357,7 @@ function* importAnnotationSet(action: annotationActions.importAnnotationSet.TAct
type: SenderType.Main,
},
} as syncIpc.EventPayload);
}

} catch (error) {
debug("ERROR in SYNC ACTION", error);
Expand All @@ -367,7 +368,7 @@ function* importAnnotationSet(action: annotationActions.importAnnotationSet.TAct
for (const annotationParsedReadyToBeImported of annotationsParsedNoConflictArray) {
const a = ActionSerializer.serialize(readerActions.annotation.push.build(annotationParsedReadyToBeImported));
try {
if (!readerWin.isDestroyed() && !readerWin.webContents.isDestroyed())
if (readerWin && !readerWin.isDestroyed() && !readerWin.webContents.isDestroyed()) {
readerWin.webContents.send(syncIpc.CHANNEL, {
type: syncIpc.EventType.MainAction,
payload: {
Expand All @@ -377,7 +378,7 @@ function* importAnnotationSet(action: annotationActions.importAnnotationSet.TAct
type: SenderType.Main,
},
} as syncIpc.EventPayload);

}
} catch (error) {
debug("ERROR in SYNC ACTION", error);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/redux/sagas/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ export function exit() {

const libraryWin = getLibraryWindowFromDi();
if (process.platform === "darwin") {
if (libraryWin.isDestroyed()) {
if (libraryWin.isDestroyed() || libraryWin.webContents.isDestroyed()) {
if (closeProcessLock.isLock) {
error(filename_, new Error(
`closing process not completed
Expand All @@ -349,7 +349,7 @@ export function exit() {
}

if (libraryWin && !libraryWin.isDestroyed() && !libraryWin.webContents.isDestroyed()) {
libraryWin.close();
libraryWin.close();
}
};

Expand Down
9 changes: 6 additions & 3 deletions src/main/redux/sagas/win/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ function* winClose(_action: winActions.library.closed.TAction) {
}
try {
const readerWin = yield* callTyped(() => getReaderWindowFromDi(reader.identifier));
if (readerWin && !readerWin.isDestroyed() && !readerWin.webContents.isDestroyed())
if (readerWin && !readerWin.isDestroyed() && !readerWin.webContents.isDestroyed()) {
if (sessionSaving) {
// force quit the reader windows to keep session in next startup
debug("destroy reader", index);
Expand All @@ -252,6 +252,7 @@ function* winClose(_action: winActions.library.closed.TAction) {
debug("close reader", index);
readerWin.close();
}
}
} catch (_err) {
// ignore
}
Expand All @@ -265,8 +266,9 @@ function* winClose(_action: winActions.library.closed.TAction) {
}

if (sessionSaving) {
if (libraryWin && !libraryWin.isDestroyed() && !libraryWin.webContents.isDestroyed())
if (libraryWin && !libraryWin.isDestroyed() && !libraryWin.webContents.isDestroyed()) {
libraryWin.destroy();
}
} else {

yield spawn(function* () {
Expand All @@ -280,8 +282,9 @@ function* winClose(_action: winActions.library.closed.TAction) {

} while (readersArray.length);

if (libraryWin && !libraryWin.isDestroyed() && !libraryWin.webContents.isDestroyed())
if (libraryWin && !libraryWin.isDestroyed() && !libraryWin.webContents.isDestroyed()) {
libraryWin.destroy();
}
});

}
Expand Down
3 changes: 2 additions & 1 deletion src/main/redux/sagas/win/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,9 @@ function* winClose(action: winActions.reader.closed.TAction) {
debug("_______3 readerWin.getBounds()", winBound);
normalizeRectangle(winBound);

if (libraryWin && !libraryWin.isDestroyed() && !libraryWin.webContents.isDestroyed())
if (libraryWin && !libraryWin.isDestroyed() && !libraryWin.webContents.isDestroyed()) {
libraryWin.setBounds(winBound);
}
} catch (e) {
debug("error libraryWindow.setBounds(readerWin.getBounds())", e);
}
Expand Down
1 change: 0 additions & 1 deletion src/main/tools/showLibrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export const showLibrary = () => {

const library = getLibraryWindowFromDi();
if (!library || library.isDestroyed() || library.webContents.isDestroyed()) {

const appActivateChannel = getAppActivateEventChannel();
appActivateChannel.put(true);
} else {
Expand Down

0 comments on commit 3eef469

Please sign in to comment.