Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

fix(electron): do not restore window on get window (#5163)

Peng Xiao512504e17704
diff --git a/packages/frontend/electron/src/main/deep-link.ts b/packages/frontend/electron/src/main/deep-link.ts
index d006c55bb5..bc5edb05f6 100644
--- a/packages/frontend/electron/src/main/deep-link.ts
+++ b/packages/frontend/electron/src/main/deep-link.ts
@@ -5,10 +5,10 @@ import { type App, type BrowserWindow, ipcMain } from 'electron';
import { buildType, CLOUD_BASE_URL, isDev } from './config';
import { logger } from './logger';
import {
+ getOrCreateWindow,
handleOpenUrlInHiddenWindow,
mainWindowOrigin,
removeCookie,
- restoreOrCreateWindow,
setCookie,
} from './main-window';
@@ -39,8 +39,9 @@ export function setupDeepLink(app: App) {
// on windows & linux, we need to listen for the second-instance event
app.on('second-instance', (event, commandLine) => {
- restoreOrCreateWindow()
- .then(() => {
+ getOrCreateWindow()
+ .then(window => {
+ window.show();
const url = commandLine.pop();
if (url?.startsWith(`${protocol}://`)) {
event.preventDefault();
@@ -67,7 +68,7 @@ async function handleAffineUrl(url: string) {
async function handleOauthJwt(url: string) {
if (url) {
try {
- const mainWindow = await restoreOrCreateWindow();
+ const mainWindow = await getOrCreateWindow();
mainWindow.show();
const urlObj = new URL(url);
const token = urlObj.searchParams.get('token');
diff --git a/packages/frontend/electron/src/main/index.ts b/packages/frontend/electron/src/main/index.ts
index ccaed0b076..3a38caa3f0 100644
--- a/packages/frontend/electron/src/main/index.ts
+++ b/packages/frontend/electron/src/main/index.ts
@@ -11,7 +11,7 @@ import { registerEvents } from './events';
import { registerHandlers } from './handlers';
import { ensureHelperProcess } from './helper-process';
import { logger } from './logger';
-import { restoreOrCreateWindow } from './main-window';
+import { getOrCreateWindow } from './main-window';
import { registerProtocol } from './protocol';
import { registerUpdater } from './updater';
@@ -56,7 +56,7 @@ app.on('window-all-closed', () => {
* @see https://www.electronjs.org/docs/v14-x-y/api/app#event-activate-macos Event: 'activate'
*/
app.on('activate', () => {
- restoreOrCreateWindow().catch(e =>
+ getOrCreateWindow().catch(e =>
console.error('Failed to restore or create window:', e)
);
});
@@ -72,7 +72,7 @@ app
.then(registerHandlers)
.then(registerEvents)
.then(ensureHelperProcess)
- .then(restoreOrCreateWindow)
+ .then(getOrCreateWindow)
.then(createApplicationMenu)
.then(registerUpdater)
.catch(e => console.error('Failed create window:', e));
diff --git a/packages/frontend/electron/src/main/main-window.ts b/packages/frontend/electron/src/main/main-window.ts
index 9aeeb5e3e7..c4234adfa8 100644
--- a/packages/frontend/electron/src/main/main-window.ts
+++ b/packages/frontend/electron/src/main/main-window.ts
@@ -148,16 +148,11 @@ let browserWindow$: Promise<BrowserWindow> | undefined;
/**
* Restore existing BrowserWindow or Create new BrowserWindow
*/
-export async function restoreOrCreateWindow() {
+export async function getOrCreateWindow() {
if (!browserWindow$ || (await browserWindow$.then(w => w.isDestroyed()))) {
browserWindow$ = createWindow();
}
const mainWindow = await browserWindow$;
-
- if (mainWindow.isMinimized()) {
- mainWindow.restore();
- logger.info('restore main window');
- }
return mainWindow;
}
@@ -188,7 +183,11 @@ export async function setCookie(
arg0: CookiesSetDetails | string,
arg1?: string
) {
- const window = await restoreOrCreateWindow();
+ const window = await browserWindow$;
+ if (!window) {
+ // do nothing if window is not ready
+ return;
+ }
const details =
typeof arg1 === 'string' && typeof arg0 === 'string'
? parseCookie(arg0, arg1)
@@ -204,12 +203,20 @@ export async function setCookie(
}
export async function removeCookie(url: string, name: string): Promise<void> {
- const window = await restoreOrCreateWindow();
+ const window = await browserWindow$;
+ if (!window) {
+ // do nothing if window is not ready
+ return;
+ }
await window.webContents.session.cookies.remove(url, name);
}
export async function getCookie(url?: string, name?: string) {
- const window = await restoreOrCreateWindow();
+ const window = await browserWindow$;
+ if (!window) {
+ // do nothing if window is not ready
+ return;
+ }
const cookies = await window.webContents.session.cookies.get({
url,
name,
diff --git a/packages/frontend/electron/src/main/protocol.ts b/packages/frontend/electron/src/main/protocol.ts
index 9b622a0cf4..e874ff6cab 100644
--- a/packages/frontend/electron/src/main/protocol.ts
+++ b/packages/frontend/electron/src/main/protocol.ts
@@ -119,8 +119,12 @@ export function registerProtocol() {
// if sending request to the cloud, attach the session cookie
if (isNetworkResource(pathname)) {
const cookie = await getCookie(CLOUD_BASE_URL);
- const cookieString = cookie.map(c => `${c.name}=${c.value}`).join('; ');
- details.requestHeaders['cookie'] = cookieString;
+ if (cookie) {
+ const cookieString = cookie
+ .map(c => `${c.name}=${c.value}`)
+ .join('; ');
+ details.requestHeaders['cookie'] = cookieString;
+ }
}
callback({
cancel: false,