From 0343475a4bf0c826d58fb4c3a7f0de53a627d3db Mon Sep 17 00:00:00 2001 From: tiddlygit-test Date: Sun, 26 Dec 2021 00:17:33 +0800 Subject: [PATCH] refactor: use workspaceDidFailLoad method to judge this --- src/services/view/index.ts | 19 ++++++------ src/services/view/setupViewEventHandlers.ts | 20 ++++++------ src/services/workspaces/index.ts | 5 +++ src/services/workspaces/interface.ts | 34 +++++++++++---------- src/services/workspacesView/index.ts | 4 +-- 5 files changed, 46 insertions(+), 36 deletions(-) diff --git a/src/services/view/index.ts b/src/services/view/index.ts index 85c0cbd4..d100a638 100644 --- a/src/services/view/index.ts +++ b/src/services/view/index.ts @@ -303,7 +303,11 @@ export class View implements IViewService { `loadInitialUrlWithCatch(): view.webContents: ${String(view.webContents)} ${hostReplacedUrl} for windowName ${windowName} for workspace ${ workspace.name }`, + { stack: new Error('debug error, not a real error').stack }, ); + if (await this.workspaceService.workspaceDidFailLoad(workspace.id)) { + return; + } // will set again in view.webContents.on('did-start-loading'), but that one sometimes is too late to block services that wait for `isLoading` await this.workspaceService.updateMetaData(workspace.id, { // eslint-disable-next-line unicorn/no-null @@ -370,7 +374,7 @@ export class View implements IViewService { } else { browserWindow.setBrowserView(view); const contentSize = browserWindow.getContentSize(); - if (typeof (await this.workspaceService.getMetaData(workspaceID)).didFailLoadErrorMessage !== 'string') { + if (workspace !== undefined && (await this.workspaceService.workspaceDidFailLoad(workspace.id))) { view.setBounds(await getViewBounds(contentSize as [number, number], false, 0, 0)); // hide browserView to show error message } else { view.setBounds(await getViewBounds(contentSize as [number, number])); @@ -435,12 +439,10 @@ export class View implements IViewService { }; public async reloadViewsWebContentsIfDidFailLoad(): Promise { - const workspaceMetaData: Record> = await this.workspaceService.getAllMetaData(); - this.forEachView((view, id, name) => { - if (typeof workspaceMetaData[id].didFailLoadErrorMessage !== 'string') { - return; + this.forEachView(async (view, id, name) => { + if (await this.workspaceService.workspaceDidFailLoad(id)) { + view.webContents.reload(); } - view.webContents.reload(); }); } @@ -486,9 +488,8 @@ export class View implements IViewService { const view = browserWindow.getBrowserView(); if (view?.webContents !== null && view?.webContents !== undefined) { const contentSize = browserWindow.getContentSize(); - const didFailLoadErrorMessage = (await this.workspaceService.getMetaData(activeId)).didFailLoadErrorMessage; - if (typeof didFailLoadErrorMessage === 'string' && didFailLoadErrorMessage.length > 0) { - logger.warn(`realignActiveView() hide because didFailLoadErrorMessage: ${didFailLoadErrorMessage}`); + if (await this.workspaceService.workspaceDidFailLoad(activeId)) { + logger.warn(`realignActiveView() hide because didFailLoad`); view?.setBounds(await getViewBounds(contentSize as [number, number], false, 0, 0)); // hide browserView to show error message } else { view?.setBounds(await getViewBounds(contentSize as [number, number])); diff --git a/src/services/view/setupViewEventHandlers.ts b/src/services/view/setupViewEventHandlers.ts index adc6c408..74d5a592 100644 --- a/src/services/view/setupViewEventHandlers.ts +++ b/src/services/view/setupViewEventHandlers.ts @@ -62,13 +62,8 @@ export default function setupViewEventHandlers( if (workspaceObject === undefined) { return; } - if ( - workspaceObject.active && - typeof (await workspaceService.getMetaData(workspace.id)).didFailLoadErrorMessage === 'string' && - browserWindow !== undefined && - !browserWindow.isDestroyed() - ) { - // fix https://github.com/atomery/singlebox/issues/228 + if (workspaceObject.active && (await workspaceService.workspaceDidFailLoad(workspace.id)) && browserWindow !== undefined && !browserWindow.isDestroyed()) { + // fix https://github.com/webcatalog/singlebox-legacy/issues/228 const contentSize = browserWindow.getContentSize(); view.setBounds(await getViewBounds(contentSize as [number, number])); } @@ -83,6 +78,10 @@ export default function setupViewEventHandlers( }); const throttledDidFinishedLoad = throttle(async () => { + // if have error, don't realignActiveWorkspace, which will hide the error message + if (await workspaceService.workspaceDidFailLoad(workspace.id)) { + return; + } logger.debug(`throttledDidFinishedLoad() workspace.id: ${workspace.id}, now workspaceViewService.realignActiveWorkspace() then set isLoading to false`); // focus on initial load // https://github.com/atomery/webcatalog/issues/398 @@ -112,14 +111,17 @@ export default function setupViewEventHandlers( // https://electronjs.org/docs/api/web-contents#event-did-fail-load // https://github.com/webcatalog/neutron/blob/3d9e65c255792672c8bc6da025513a5404d98730/main-src/libs/views.js#L397 view.webContents.on('did-fail-load', async (_event, errorCode, errorDesc, _validateUrl, isMainFrame) => { - const [workspaceObject, workspaceMetaData] = await Promise.all([workspaceService.get(workspace.id), workspaceService.getMetaData(workspace.id)]); + const [workspaceObject, workspaceDidFailLoad] = await Promise.all([ + workspaceService.get(workspace.id), + workspaceService.workspaceDidFailLoad(workspace.id), + ]); // this event might be triggered // even after the workspace obj and BrowserView // are destroyed. See https://github.com/atomery/webcatalog/issues/836 if (workspaceObject === undefined) { return; } - if (typeof workspaceMetaData.didFailLoadErrorMessage === 'string' && workspaceMetaData.didFailLoadErrorMessage.length > 0) { + if (workspaceDidFailLoad) { return; } if (isMainFrame && errorCode < 0 && errorCode !== -3) { diff --git a/src/services/workspaces/index.ts b/src/services/workspaces/index.ts index 0708da19..e44862d5 100644 --- a/src/services/workspaces/index.ts +++ b/src/services/workspaces/index.ts @@ -453,4 +453,9 @@ export class Workspace implements IWorkspaceService { ...options, }; }; + + public async workspaceDidFailLoad(id: string): Promise { + const workspaceMetaData = this.getMetaDataSync(id); + return typeof workspaceMetaData?.didFailLoadErrorMessage === 'string' && workspaceMetaData.didFailLoadErrorMessage.length > 0; + } } diff --git a/src/services/workspaces/interface.ts b/src/services/workspaces/interface.ts index bd029d76..a99e70ff 100644 --- a/src/services/workspaces/interface.ts +++ b/src/services/workspaces/interface.ts @@ -146,32 +146,34 @@ export interface IWorkspaceService { setWorkspaces(newWorkspaces: Record): Promise; update(id: string, workspaceSetting: Partial, immediate?: boolean): Promise; updateMetaData: (id: string, options: Partial) => Promise; + workspaceDidFailLoad(id: string): Promise; workspaces$: BehaviorSubject>; } export const WorkspaceServiceIPCDescriptor = { channel: WorkspaceChannel.name, properties: { - workspaces$: ProxyPropertyType.Value$, - getWorkspacesAsList: ProxyPropertyType.Function, + countWorkspaces: ProxyPropertyType.Function, + create: ProxyPropertyType.Function, get: ProxyPropertyType.Function, get$: ProxyPropertyType.Function$, - create: ProxyPropertyType.Function, - getWorkspaces: ProxyPropertyType.Function, - countWorkspaces: ProxyPropertyType.Function, - getMetaData: ProxyPropertyType.Function, + getActiveWorkspace: ProxyPropertyType.Function, getAllMetaData: ProxyPropertyType.Function, - updateMetaData: ProxyPropertyType.Function, + getByName: ProxyPropertyType.Function, + getFirstWorkspace: ProxyPropertyType.Function, + getMetaData: ProxyPropertyType.Function, + getNextWorkspace: ProxyPropertyType.Function, + getPreviousWorkspace: ProxyPropertyType.Function, + getWorkspaces: ProxyPropertyType.Function, + getWorkspacesAsList: ProxyPropertyType.Function, + remove: ProxyPropertyType.Function, + removeWorkspacePicture: ProxyPropertyType.Function, set: ProxyPropertyType.Function, - update: ProxyPropertyType.Function, - setWorkspaces: ProxyPropertyType.Function, setActiveWorkspace: ProxyPropertyType.Function, setWorkspacePicture: ProxyPropertyType.Function, - removeWorkspacePicture: ProxyPropertyType.Function, - remove: ProxyPropertyType.Function, - getByName: ProxyPropertyType.Function, - getPreviousWorkspace: ProxyPropertyType.Function, - getNextWorkspace: ProxyPropertyType.Function, - getActiveWorkspace: ProxyPropertyType.Function, - getFirstWorkspace: ProxyPropertyType.Function, + setWorkspaces: ProxyPropertyType.Function, + update: ProxyPropertyType.Function, + updateMetaData: ProxyPropertyType.Function, + workspaceDidFailLoad: ProxyPropertyType.Function, + workspaces$: ProxyPropertyType.Value$, }, }; diff --git a/src/services/workspacesView/index.ts b/src/services/workspacesView/index.ts index 585399b3..b738e416 100644 --- a/src/services/workspacesView/index.ts +++ b/src/services/workspacesView/index.ts @@ -118,7 +118,7 @@ export class WorkspaceView implements IWorkspaceViewService { // and also wait for wiki BrowserView to be able to receive command // eslint-disable-next-line global-require let workspaceMetadata = await this.workspaceService.getMetaData(workspace.id); - let loadFailed = typeof workspaceMetadata.didFailLoadErrorMessage === 'string' && workspaceMetadata.didFailLoadErrorMessage.length > 0; + let loadFailed = await this.workspaceService.workspaceDidFailLoad(workspace.id); // if wikiStartup cause load failed, we skip the view creation if (loadFailed) { logger.info(`Exit initializeWorkspaceView() because loadFailed`, { workspace, workspaceMetadata }); @@ -132,7 +132,7 @@ export class WorkspaceView implements IWorkspaceViewService { await delay(500); workspaceMetadata = await this.workspaceService.getMetaData(workspace.id); } - loadFailed = typeof workspaceMetadata.didFailLoadErrorMessage === 'string' && workspaceMetadata.didFailLoadErrorMessage.length > 0; + loadFailed = await this.workspaceService.workspaceDidFailLoad(workspace.id); if (loadFailed) { const latestWorkspaceData = await this.workspaceService.get(workspace.id); throw new WorkspaceFailedToLoadError(workspaceMetadata.didFailLoadErrorMessage!, latestWorkspaceData?.lastUrl ?? homeUrl);