refactor: use workspaceDidFailLoad method to judge this

This commit is contained in:
tiddlygit-test 2021-12-26 00:17:33 +08:00
parent 91fc433a80
commit 0343475a4b
5 changed files with 46 additions and 36 deletions

View file

@ -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<void> {
const workspaceMetaData: Record<string, Partial<IWorkspaceMetaData>> = 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]));

View file

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

View file

@ -453,4 +453,9 @@ export class Workspace implements IWorkspaceService {
...options,
};
};
public async workspaceDidFailLoad(id: string): Promise<boolean> {
const workspaceMetaData = this.getMetaDataSync(id);
return typeof workspaceMetaData?.didFailLoadErrorMessage === 'string' && workspaceMetaData.didFailLoadErrorMessage.length > 0;
}
}

View file

@ -146,32 +146,34 @@ export interface IWorkspaceService {
setWorkspaces(newWorkspaces: Record<string, IWorkspace>): Promise<void>;
update(id: string, workspaceSetting: Partial<IWorkspace>, immediate?: boolean): Promise<void>;
updateMetaData: (id: string, options: Partial<IWorkspaceMetaData>) => Promise<void>;
workspaceDidFailLoad(id: string): Promise<boolean>;
workspaces$: BehaviorSubject<Record<string, IWorkspaceWithMetadata>>;
}
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$,
},
};

View file

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