commit 54c2bafd2b0632dcc3a1b72c2c8dedbeea9eb732 Author: Ole Date: Fri Oct 9 17:12:59 2020 +0200 Refactor reload 3: Await diff and asset request This function is very hard to follow. Making small steps to ensure careful review. Use await to move this code block left. Change-Id: Ic035908bf0a9e16d040a852aab0b98ea395fb301 diff --git a/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host.ts b/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host.ts index d683129..009bfbb 100644 --- a/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host.ts +++ b/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host.ts @@ -327,7 +327,7 @@ export class GrDiffHost extends GestureEventListeners( * signal to report metrics event that started on location change. * @return */ - reload(shouldReportMetric?: boolean) { + async reload(shouldReportMetric?: boolean) { this.clear(); if (!this.path) throw new Error('Missing required "path" property.'); if (!this.changeNum) throw new Error('Missing required "changeNum" prop.'); @@ -372,48 +372,46 @@ export class GrDiffHost extends GestureEventListeners( return this._loadDiffAssets(diff); }); - // Not waiting for coverage ranges intentionally as - // plugin loading should not block the content rendering - return Promise.all([diffRequest, assetRequest]) - .then(async results => { - const diff = results[0]; - if (!diff) { - return Promise.resolve(); - } - this.filesWeblinks = this._getFilesWeblinks(diff); - this.diff = diff; - const event = await new Promise(resolve => { - const callback = (event: CustomEvent) => { - this.removeEventListener('render', callback); - resolve(event); - }; - this.addEventListener('render', callback); - }); - const needsSyntaxHighlighting = - event.detail && event.detail.contentRendered; - let result: Promise = Promise.resolve(); - if (needsSyntaxHighlighting) { - this.reporting.time(TimingLabel.SYNTAX); - result = this.$.syntaxLayer.process().finally(() => { - this.reporting.timeEnd(TimingLabel.SYNTAX); - }); - } - result.finally(() => { - this.reporting.timeEnd(TimingLabel.TOTAL); - }); - if (shouldReportMetric) { - // We report diffViewContentDisplayed only on reload caused - // by params changed - expected only on Diff Page. - this.reporting.diffViewContentDisplayed(); - } - return result; - }) - .catch(err => { - console.warn('Error encountered loading diff:', err); - }) - .then(() => { + try { + // Not waiting for coverage ranges intentionally as + // plugin loading should not block the content rendering + const results = await Promise.all([diffRequest, assetRequest]); + const diff = results[0]; + if (!diff) { this._loading = false; + return; + } + this.filesWeblinks = this._getFilesWeblinks(diff); + this.diff = diff; + const event = await new Promise(resolve => { + const callback = (event: CustomEvent) => { + this.removeEventListener('render', callback); + resolve(event); + }; + this.addEventListener('render', callback); + }); + const needsSyntaxHighlighting = + event.detail && event.detail.contentRendered; + let result: Promise = Promise.resolve(); + if (needsSyntaxHighlighting) { + this.reporting.time(TimingLabel.SYNTAX); + result = this.$.syntaxLayer.process().finally(() => { + this.reporting.timeEnd(TimingLabel.SYNTAX); + }); + } + result.finally(() => { + this.reporting.timeEnd(TimingLabel.TOTAL); }); + if (shouldReportMetric) { + // We report diffViewContentDisplayed only on reload caused + // by params changed - expected only on Diff Page. + this.reporting.diffViewContentDisplayed(); + } + await result; + } catch (err) { + console.warn('Error encountered loading diff:', err); + } + this._loading = false; } clear() {