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

fix https://github.com/toeverything/AFFiNE/issues/5161

Looks like I used window.restore incorrectly.
This commit is contained in:
Peng Xiao 2023-12-02 15:13:48 +00:00
parent 1f7654e80f
commit 512504e177
No known key found for this signature in database
GPG Key ID: 23F23D9E8B3971ED
4 changed files with 30 additions and 18 deletions

View File

@ -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');

View File

@ -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));

View File

@ -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,

View File

@ -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,