Merge "Prefetch /diff requests when expanding all diffs"

This commit is contained in:
Milutin Kristofic
2020-06-16 06:59:44 +00:00
committed by Gerrit Code Review
4 changed files with 50 additions and 0 deletions

View File

@@ -1279,6 +1279,14 @@ class GrFileList extends mixinBehaviors( [
_renderInOrder(files, diffElements, initialCount) {
let iter = 0;
for (const file of files) {
const path = file.path;
const diffElem = this._findDiffByPath(path, diffElements);
if (diffElem) {
diffElem.prefetchDiff();
}
}
return (new Promise(resolve => {
this.dispatchEvent(new CustomEvent('reload-drafts', {
detail: {resolve},

View File

@@ -95,6 +95,7 @@ suite('gr-file-list tests', () => {
});
stub('gr-diff-host', {
reload() { return Promise.resolve(); },
prefetchDiff() {},
});
// Element must be wrapped in an element with direct access to the
@@ -1106,6 +1107,7 @@ suite('gr-file-list tests', () => {
reload() {
done();
},
prefetchDiff() {},
cancel() {},
getCursorStops() { return []; },
addEventListener(eventName, callback) {
@@ -1163,6 +1165,7 @@ suite('gr-file-list tests', () => {
const diffs = [{
path: 'p0',
style: {},
prefetchDiff() {},
reload() {
assert.equal(callCount++, 2);
return Promise.resolve();
@@ -1170,6 +1173,7 @@ suite('gr-file-list tests', () => {
}, {
path: 'p1',
style: {},
prefetchDiff() {},
reload() {
assert.equal(callCount++, 1);
return Promise.resolve();
@@ -1177,6 +1181,7 @@ suite('gr-file-list tests', () => {
}, {
path: 'p2',
style: {},
prefetchDiff() {},
reload() {
assert.equal(callCount++, 0);
return Promise.resolve();
@@ -1199,6 +1204,7 @@ suite('gr-file-list tests', () => {
const diffs = [{
path: 'p0',
style: {},
prefetchDiff() {},
reload() {
assert.equal(reviewStub.callCount, 2);
assert.equal(callCount++, 2);
@@ -1207,6 +1213,7 @@ suite('gr-file-list tests', () => {
}, {
path: 'p1',
style: {},
prefetchDiff() {},
reload() {
assert.equal(reviewStub.callCount, 1);
assert.equal(callCount++, 1);
@@ -1215,6 +1222,7 @@ suite('gr-file-list tests', () => {
}, {
path: 'p2',
style: {},
prefetchDiff() {},
reload() {
assert.equal(reviewStub.callCount, 0);
assert.equal(callCount++, 0);
@@ -1237,6 +1245,7 @@ suite('gr-file-list tests', () => {
const diffs = [{
path: 'p',
style: {},
prefetchDiff() {},
reload() { return Promise.resolve(); },
}];
@@ -1566,6 +1575,7 @@ suite('gr-file-list tests', () => {
});
stub('gr-diff-host', {
reload() { return Promise.resolve(); },
prefetchDiff() {},
});
// Element must be wrapped in an element with direct access to the

View File

@@ -218,6 +218,11 @@ class GrDiffHost extends mixinBehaviors( [
notify: true,
},
_fetchDiffPromise: {
type: Object,
value: null,
},
/** @type {?Object} */
_blame: {
type: Object,
@@ -535,8 +540,21 @@ class GrDiffHost extends mixinBehaviors( [
!this.noAutoRender;
}
// TODO(milutin): Use rest-api with fetchCacheURL instead of this.
prefetchDiff() {
if (!!this.changeNum && !!this.patchRange && !!this.path
&& this._fetchDiffPromise === null) {
this._fetchDiffPromise = this._getDiff();
}
}
/** @return {!Promise<!Object>} */
_getDiff() {
if (this._fetchDiffPromise !== null) {
const fetchDiffPromise = this._fetchDiffPromise;
this._fetchDiffPromise = null;
return fetchDiffPromise;
}
// Wrap the diff request in a new promise so that the error handler
// rejects the promise, allowing the error to be handled in the .catch.
return new Promise((resolve, reject) => {
@@ -907,6 +925,7 @@ class GrDiffHost extends mixinBehaviors( [
return;
}
this._fetchDiffPromise = null;
if (preferredWhitespaceLevel !== loadedWhitespaceLevel &&
!noRenderOnPrefsChange) {
this.reload();

View File

@@ -445,6 +445,19 @@ suite('gr-diff-host tests', () => {
});
});
test('prefetch getDiff', done => {
const diffRestApiStub = sandbox.stub(element.$.restAPI, 'getDiff')
.returns(Promise.resolve({content: []}));
element.changeNum = 123;
element.patchRange = {basePatchNum: 1, patchNum: 2};
element.path = 'file.txt';
element.prefetchDiff();
element._getDiff().then(() =>{
assert.isTrue(diffRestApiStub.calledOnce);
done();
});
});
test('_getDiff handles null diff responses', done => {
stub('gr-rest-api-interface', {
getDiff() { return Promise.resolve(null); },