[MM-41891] Improve error and reload states (#2019)

This commit is contained in:
Devin Binnie
2022-03-18 10:16:12 -04:00
committed by GitHub
parent f79c445920
commit 3eb904cd67
7 changed files with 51 additions and 23 deletions

View File

@@ -105,12 +105,15 @@ describe('main/views/MattermostView', () => {
describe('retry', () => {
const window = {on: jest.fn()};
const mattermostView = new MattermostView(tabView, {}, window, {});
const retryInBackgroundFn = jest.fn();
beforeEach(() => {
jest.useFakeTimers();
mattermostView.view.webContents.loadURL.mockImplementation(() => Promise.resolve());
mattermostView.loadSuccess = jest.fn();
mattermostView.loadRetry = jest.fn();
mattermostView.emit = jest.fn();
mattermostView.retryInBackground = () => retryInBackgroundFn;
});
it('should do nothing when webcontents are destroyed', () => {
@@ -140,7 +143,7 @@ describe('main/views/MattermostView', () => {
expect(mattermostView.loadRetry).toBeCalledWith('http://server-1.com', error);
});
it('should set to error status when max retries are reached', async () => {
it('should set to error status and retry in the background when max retries are reached', async () => {
mattermostView.maxRetries = 0;
const error = new Error('test');
const promise = Promise.reject(error);
@@ -151,6 +154,8 @@ describe('main/views/MattermostView', () => {
expect(mattermostView.loadRetry).not.toBeCalled();
expect(WindowManager.sendToRenderer).toBeCalledWith(LOAD_FAILED, mattermostView.tab.name, expect.any(String), expect.any(String));
expect(mattermostView.status).toBe(-1);
jest.runAllTimers();
expect(retryInBackgroundFn).toBeCalled();
});
});

View File

@@ -175,13 +175,27 @@ export class MattermostView extends EventEmitter {
} else {
WindowManager.sendToRenderer(LOAD_FAILED, this.tab.name, err.toString(), loadURL.toString());
this.emit(LOAD_FAILED, this.tab.name, err.toString(), loadURL.toString());
log.info(`[${Util.shorten(this.tab.name)}] Couldn't stablish a connection with ${loadURL}: ${err}.`);
log.info(`[${Util.shorten(this.tab.name)}] Couldn't stablish a connection with ${loadURL}: ${err}. Will continue to retry in the background.`);
this.status = Status.ERROR;
this.retryLoad = setTimeout(this.retryInBackground(loadURL), RELOAD_INTERVAL);
}
});
};
}
retryInBackground = (loadURL: string) => {
return () => {
// window was closed while retrying
if (!this.view || !this.view.webContents) {
return;
}
const loading = this.view.webContents.loadURL(loadURL, {userAgent: composeUserAgent()});
loading.then(this.loadSuccess(loadURL)).catch(() => {
this.retryLoad = setTimeout(this.retryInBackground(loadURL), RELOAD_INTERVAL);
});
};
}
loadRetry = (loadURL: string, err: Error) => {
this.retryLoad = setTimeout(this.retry(loadURL), RELOAD_INTERVAL);
WindowManager.sendToRenderer(LOAD_RETRY, this.tab.name, Date.now() + RELOAD_INTERVAL, err.toString(), loadURL.toString());

View File

@@ -98,7 +98,7 @@ export class ViewManager {
view.load(url);
view.on(UPDATE_TARGET_URL, this.showURLView);
view.on(LOADSCREEN_END, this.finishLoading);
view.once(LOAD_FAILED, this.failLoading);
view.on(LOAD_FAILED, this.failLoading);
}
reloadViewIfNeeded = (viewName: string) => {

View File

@@ -24,6 +24,7 @@ import {
BROWSER_HISTORY_BUTTON,
DISPATCH_GET_DESKTOP_SOURCES,
DESKTOP_SOURCES_RESULT,
RELOAD_CURRENT_VIEW,
} from 'common/communication';
import urlUtils from 'common/utils/url';
import {SECOND} from 'common/utils/constants';
@@ -68,6 +69,7 @@ export class WindowManager {
ipcMain.handle(GET_VIEW_NAME, this.handleGetViewName);
ipcMain.handle(GET_VIEW_WEBCONTENTS_ID, this.handleGetWebContentsId);
ipcMain.on(DISPATCH_GET_DESKTOP_SOURCES, this.handleGetDesktopSources);
ipcMain.on(RELOAD_CURRENT_VIEW, this.handleReloadCurrentView);
}
handleUpdateConfig = () => {
@@ -650,6 +652,15 @@ export class WindowManager {
}));
});
}
handleReloadCurrentView = () => {
const view = this.viewManager?.getCurrentView();
if (!view) {
return;
}
view?.reload();
this.viewManager?.showByName(view?.name);
}
}
const windowManager = new WindowManager();