Defer loading user avatars

Feature: Issue 8983
Change-Id: I638f8232467cf67543f7c7b571d7dd4a97b6aae8
This commit is contained in:
Viktar Donich
2018-05-17 14:33:22 -07:00
parent d6876c04a6
commit 82ad0ecd66
6 changed files with 62 additions and 41 deletions

View File

@@ -16,7 +16,6 @@ limitations under the License.
--> -->
<link rel="import" href="../../../bower_components/polymer/polymer.html"> <link rel="import" href="../../../bower_components/polymer/polymer.html">
<link rel="import" href="../../shared/gr-js-api-interface/gr-js-api-interface.html">
<dom-module id="gr-reporting"> <dom-module id="gr-reporting">
<script src="gr-jank-detector.js"></script> <script src="gr-jank-detector.js"></script>

View File

@@ -151,8 +151,13 @@
return window.performance.now(); return window.performance.now();
}, },
_arePluginsLoaded() {
return this._baselines &&
!this._baselines.hasOwnProperty(TIMER.PLUGINS_LOADED);
},
reporter(...args) { reporter(...args) {
const report = (Gerrit._arePluginsLoaded() && !pending.length) ? const report = (this._arePluginsLoaded() && !pending.length) ?
this.defaultReporter : this.cachingReporter; this.defaultReporter : this.cachingReporter;
report.apply(this, args); report.apply(this, args);
}, },
@@ -177,7 +182,7 @@
if (type === ERROR.TYPE) { if (type === ERROR.TYPE) {
console.error(eventValue.error || eventName); console.error(eventValue.error || eventName);
} }
if (Gerrit._arePluginsLoaded()) { if (this._arePluginsLoaded()) {
if (pending.length) { if (pending.length) {
for (const args of pending.splice(0)) { for (const args of pending.splice(0)) {
this.reporter(...args); this.reporter(...args);

View File

@@ -202,11 +202,9 @@ limitations under the License.
setup(() => { setup(() => {
element.reporter.restore(); element.reporter.restore();
sandbox.stub(element, 'defaultReporter'); sandbox.stub(element, 'defaultReporter');
sandbox.stub(Gerrit, '_arePluginsLoaded');
}); });
test('pluginsLoaded reports time', () => { test('pluginsLoaded reports time', () => {
Gerrit._arePluginsLoaded.returns(true);
sandbox.stub(element, 'now').returns(42); sandbox.stub(element, 'now').returns(42);
element.pluginsLoaded(); element.pluginsLoaded();
assert.isTrue(element.defaultReporter.calledWithExactly( assert.isTrue(element.defaultReporter.calledWithExactly(
@@ -215,21 +213,18 @@ limitations under the License.
}); });
test('pluginsLoaded reports plugins', () => { test('pluginsLoaded reports plugins', () => {
Gerrit._arePluginsLoaded.returns(true);
element.pluginsLoaded(['foo', 'bar']); element.pluginsLoaded(['foo', 'bar']);
assert.isTrue(element.defaultReporter.calledWithExactly( assert.isTrue(element.defaultReporter.calledWith(
'lifecycle', 'Plugins installed', 'foo,bar' 'lifecycle', 'Plugins installed', 'foo,bar'
)); ));
}); });
test('caches reports if plugins are not loaded', () => { test('caches reports if plugins are not loaded', () => {
Gerrit._arePluginsLoaded.returns(false);
element.timeEnd('foo'); element.timeEnd('foo');
assert.isFalse(element.defaultReporter.called); assert.isFalse(element.defaultReporter.called);
}); });
test('reports if plugins are loaded', () => { test('reports if plugins are loaded', () => {
Gerrit._arePluginsLoaded.returns(true);
element.pluginsLoaded(); element.pluginsLoaded();
assert.isTrue(element.defaultReporter.called); assert.isTrue(element.defaultReporter.called);
}); });
@@ -237,14 +232,19 @@ limitations under the License.
test('reports cached events preserving order', () => { test('reports cached events preserving order', () => {
element.time('foo'); element.time('foo');
element.time('bar'); element.time('bar');
Gerrit._arePluginsLoaded.returns(false);
element.timeEnd('foo'); element.timeEnd('foo');
Gerrit._arePluginsLoaded.returns(true); element.pluginsLoaded();
element.timeEnd('bar'); element.timeEnd('bar');
assert.isTrue(element.defaultReporter.firstCall.calledWith( assert.isTrue(element.defaultReporter.getCall(0).calledWith(
'timing-report', 'UI Latency', 'foo' 'timing-report', 'UI Latency', 'foo'
)); ));
assert.isTrue(element.defaultReporter.secondCall.calledWith( assert.isTrue(element.defaultReporter.getCall(1).calledWith(
'timing-report', 'UI Latency', 'PluginsLoaded'
));
assert.isTrue(element.defaultReporter.getCall(2).calledWith(
'lifecycle', 'Plugins installed'
));
assert.isTrue(element.defaultReporter.getCall(3).calledWith(
'timing-report', 'UI Latency', 'bar' 'timing-report', 'UI Latency', 'bar'
)); ));
}); });

View File

@@ -16,9 +16,10 @@ limitations under the License.
--> -->
<link rel="import" href="../../../bower_components/polymer/polymer.html"> <link rel="import" href="../../../bower_components/polymer/polymer.html">
<link rel="import" href="../gr-rest-api-interface/gr-rest-api-interface.html">
<link rel="import" href="../../../behaviors/base-url-behavior/base-url-behavior.html"> <link rel="import" href="../../../behaviors/base-url-behavior/base-url-behavior.html">
<link rel="import" href="../../../styles/shared-styles.html"> <link rel="import" href="../../../styles/shared-styles.html">
<link rel="import" href="../../shared/gr-js-api-interface/gr-js-api-interface.html">
<link rel="import" href="../gr-rest-api-interface/gr-rest-api-interface.html">
<dom-module id="gr-avatar"> <dom-module id="gr-avatar">
<template> <template>

View File

@@ -29,37 +29,40 @@
type: Number, type: Number,
value: 16, value: 16,
}, },
_hasAvatars: {
type: Boolean,
value: false,
},
}, },
behaviors: [ behaviors: [
Gerrit.BaseUrlBehavior, Gerrit.BaseUrlBehavior,
], ],
created() {
this.hidden = true;
},
attached() { attached() {
this.$.restAPI.getConfig().then(cfg => { Promise.all([
const hasAvatars = !!(cfg && cfg.plugin && cfg.plugin.has_avatars); this.$.restAPI.getConfig(),
if (hasAvatars) { Gerrit.awaitPluginsLoaded(),
this.hidden = false; ]).then(([cfg]) => {
this._hasAvatars = !!(cfg && cfg.plugin && cfg.plugin.has_avatars);
if (this._hasAvatars && this.account) {
// src needs to be set if avatar becomes visible // src needs to be set if avatar becomes visible
this._updateAvatarURL(this.account); this._updateAvatarURL();
} else {
this.hidden = true;
} }
}); });
}, },
_accountChanged(account) { _accountChanged(account) {
this._updateAvatarURL(account); this._updateAvatarURL();
}, },
_updateAvatarURL(account) { _updateAvatarURL() {
if (!this.hidden && account) { if (this.hidden || !this._hasAvatars) { return; }
const url = this._buildAvatarURL(this.account); const url = this._buildAvatarURL(this.account);
if (url) { if (url) {
this.style.backgroundImage = 'url("' + url + '")'; this.style.backgroundImage = 'url("' + url + '")';
}
} }
}, },

View File

@@ -38,7 +38,7 @@ limitations under the License.
setup(() => { setup(() => {
stub('gr-rest-api-interface', { stub('gr-rest-api-interface', {
getConfig() { return Promise.resolve({}); }, getConfig() { return Promise.resolve({plugin: {has_avatars: true}}); },
}); });
element = fixture('basic'); element = fixture('basic');
}); });
@@ -97,23 +97,36 @@ limitations under the License.
}); });
test('dom for existing account', () => { test('dom for existing account', () => {
assert.isTrue(element.hasAttribute('hidden'), assert.isFalse(element.hasAttribute('hidden'));
'element not hidden initially');
element.hidden = false;
element.imageSize = 64; element.imageSize = 64;
element.account = { element.account = {
_account_id: 123, _account_id: 123,
}; };
assert.isFalse(element.hasAttribute('hidden'), 'element hidden'); assert.strictEqual(element.style.backgroundImage, '');
assert.isTrue( // Emulate plugins loaded.
element.style.backgroundImage.includes('/accounts/123/avatar?s=64')); Gerrit._setPluginsPending([]);
return Promise.all([
element.$.restAPI.getConfig(),
Gerrit.awaitPluginsLoaded(),
]).then(() => {
assert.isFalse(element.hasAttribute('hidden'));
assert.isTrue(
element.style.backgroundImage.includes('/accounts/123/avatar?s=64'));
});
}); });
test('dom for non available account', () => { test('dom for non available account', () => {
assert.isTrue(element.hasAttribute('hidden'), assert.isFalse(element.hasAttribute('hidden'));
'element not hidden initially'); element.account = null;
element.account = undefined; assert.isFalse(element.hasAttribute('hidden'));
assert.isTrue(element.hasAttribute('hidden'), 'element not hidden'); // Emulate plugins loaded.
Gerrit._setPluginsPending([]);
return Promise.all([
element.$.restAPI.getConfig(),
Gerrit.awaitPluginsLoaded(),
]).then(() => {
assert.isTrue(element.hasAttribute('hidden'));
});
}); });
}); });
</script> </script>