Merge "Change view update checks according to update_delay"

This commit is contained in:
Wyatt Allen 2017-04-27 09:40:49 +00:00 committed by Gerrit Code Review
commit 6f8b5e3338
2 changed files with 94 additions and 1 deletions

View File

@ -27,6 +27,7 @@
// Maximum length for patch set descriptions.
var PATCH_DESC_MAX_LENGTH = 500;
var REVIEWERS_REGEX = /^R=/gm;
var MIN_CHECK_INTERVAL_SECS = 0;
Polymer({
is: 'gr-change-view',
@ -64,7 +65,10 @@
},
backPage: String,
hasParent: Boolean,
serverConfig: Object,
serverConfig: {
type: Object,
observer: '_startUpdateCheckTimer',
},
keyEventTarget: {
type: Object,
value: function() { return document.body; },
@ -159,6 +163,7 @@
type: Boolean,
value: true,
},
_updateCheckTimerHandle: Number,
},
behaviors: [
@ -204,10 +209,16 @@
this.addEventListener('editable-content-cancel',
this._handleCommitMessageCancel.bind(this));
this.listen(window, 'scroll', '_handleScroll');
this.listen(document, 'visibilitychange', '_handleVisibilityChange');
},
detached: function() {
this.unlisten(window, 'scroll', '_handleScroll');
this.unlisten(document, 'visibilitychange', '_handleVisibilityChange');
if (this._updateCheckTimerHandle) {
this._cancelUpdateCheckTimer();
}
},
_handleEditCommitMessage: function(e) {
@ -1154,5 +1165,46 @@
return this._getScrollHeight(this.$.relatedChanges) <=
this._getOffsetHeight(this.$.relatedChanges);
},
_startUpdateCheckTimer: function() {
if (!this.serverConfig ||
!this.serverConfig.change ||
this.serverConfig.change.update_delay === undefined ||
this.serverConfig.change.update_delay <= MIN_CHECK_INTERVAL_SECS) {
return;
}
this._updateCheckTimerHandle = this.async(function() {
this.fetchIsLatestKnown(this._change, this.$.restAPI)
.then(function(latest) {
if (!latest) {
this._cancelUpdateCheckTimer();
this.fire('show-alert', {
message: 'A newer patch has been uploaded.',
action: 'Reload',
callback: function() {
// Load the current change without any patch range.
location.href = this.getBaseUrl() + '/c/' +
this._change._number;
}.bind(this),
});
}
this._startUpdateCheckTimer();
}.bind(this));
}, this.serverConfig.change.update_delay * 1000);
},
_cancelUpdateCheckTimer: function() {
this.cancelAsync(this._updateCheckTimerHandle);
this._updateCheckTimerHandle = null;
},
_handleVisibilityChange: function() {
if (document.hidden && this._updateCheckTimerHandle) {
this._cancelUpdateCheckTimer();
} else if (!this._updateCheckTimerHandle) {
this._startUpdateCheckTimer();
}
},
});
})();

View File

@ -1103,6 +1103,47 @@ limitations under the License.
assert.equal(element.customStyle['--related-change-btn-top-padding'],
'2px');
});
suite('update checks', function() {
setup(function() {
sandbox.spy(element, '_startUpdateCheckTimer');
sandbox.stub(element, 'async', function(f) {
// Only fire the async callback one time.
if (element.async.callCount > 1) { return; }
f.call(element);
});
});
test('_startUpdateCheckTimer negative delay', function() {
sandbox.stub(element, 'fetchIsLatestKnown');
element.serverConfig = {change: {update_delay: -1}};
assert.isTrue(element._startUpdateCheckTimer.called);
assert.isFalse(element.fetchIsLatestKnown.called);
});
test('_startUpdateCheckTimer up-to-date', function() {
sandbox.stub(element, 'fetchIsLatestKnown',
function() { return Promise.resolve(true); });
element.serverConfig = {change: {update_delay: 12345}};
assert.isTrue(element._startUpdateCheckTimer.called);
assert.isTrue(element.fetchIsLatestKnown.called);
assert.equal(element.async.lastCall.args[1], 12345 * 1000);
});
test('_startUpdateCheckTimer out-of-date shows an alert',
function(done) {
sandbox.stub(element, 'fetchIsLatestKnown',
function() { return Promise.resolve(false); });
element.addEventListener('show-alert', function() {
done();
});
element.serverConfig = {change: {update_delay: 12345}};
});
});
});
});
</script>