Defer metrics reporting until plugins are loaded
Cache all events reported via gr-reporting instances in closure in order
to share the cahce. Flush the cache on next reporting after plugins are
loaded.
This allows plugins to receive all reporting events and unlocks Google
Analytics integration.
Change-Id: Ic0c1d37667208bfd55bbf77e2138d0170f7fb097
diff --git a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface.html b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface.html
index 1967b80..5c0535b 100644
--- a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface.html
+++ b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface.html
@@ -14,6 +14,7 @@
limitations under the License.
-->
<link rel="import" href="../../../bower_components/polymer/polymer.html">
+<link rel="import" href="../../core/gr-reporting/gr-reporting.html">
<link rel="import" href="../gr-rest-api-interface/gr-rest-api-interface.html">
<dom-module id="gr-js-api-interface">
@@ -23,4 +24,3 @@
<script src="gr-js-api-interface.js"></script>
<script src="gr-public-js-api.js"></script>
</dom-module>
-
diff --git a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html
index 97e0a18..2e7c53d 100644
--- a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html
+++ b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html
@@ -172,5 +172,49 @@
});
});
+ test('_setPluginsCount', function(done) {
+ stub('gr-reporting', {
+ pluginsLoaded: function() {
+ assert.equal(Gerrit._pluginsPending, 0);
+ done();
+ }
+ });
+ Gerrit._setPluginsCount(0);
+ });
+
+ test('_arePluginsLoaded', function() {
+ assert.isFalse(Gerrit._arePluginsLoaded());
+ Gerrit._setPluginsCount(1);
+ assert.isFalse(Gerrit._arePluginsLoaded());
+ Gerrit._setPluginsCount(0);
+ assert.isTrue(Gerrit._arePluginsLoaded());
+ });
+
+ test('_pluginInstalled', function(done) {
+ stub('gr-reporting', {
+ pluginsLoaded: function() {
+ done();
+ }
+ });
+ Gerrit._setPluginsCount(2);
+ Gerrit._pluginInstalled();
+ assert.equal(Gerrit._pluginsPending, 1);
+ Gerrit._pluginInstalled();
+ });
+
+ test('install calls _pluginInstalled', function() {
+ var stub = sinon.stub(Gerrit, '_pluginInstalled');
+ Gerrit.install(function(p) { plugin = p; }, '0.1',
+ 'http://test.com/plugins/testplugin/static/test.js');
+ assert.isTrue(stub.calledOnce);
+ stub.restore();
+ });
+
+ test('install calls _pluginInstalled on error', function() {
+ var stub = sinon.stub(Gerrit, '_pluginInstalled');
+ Gerrit.install(function() {}, '0.0pre-alpha');
+ assert.isTrue(stub.calledOnce);
+ stub.restore();
+ });
});
</script>
diff --git a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js
index 21d76f1..ad8c135 100644
--- a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js
+++ b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js
@@ -64,6 +64,9 @@
var Gerrit = window.Gerrit || {};
+ // Number of plugins to initialize, -1 means 'not yet known'.
+ Gerrit._pluginsPending = -1;
+
Gerrit.getPluginName = function() {
console.warn('Gerrit.getPluginName is not supported in PolyGerrit.',
'Please use self.getPluginName() instead.');
@@ -85,12 +88,14 @@
if (opt_version && opt_version !== API_VERSION) {
console.warn('Only version ' + API_VERSION +
' is supported in PolyGerrit. ' + opt_version + ' was given.');
+ Gerrit._pluginInstalled();
return;
}
// TODO(andybons): Polyfill currentScript for IE10/11 (edge supports it).
var src = opt_src || (document.currentScript && document.currentScript.src);
callback(new Plugin(src));
+ Gerrit._pluginInstalled();
};
Gerrit.getLoggedIn = function() {
@@ -101,5 +106,20 @@
// NOOP since PolyGerrit doesn’t support GWT plugins.
};
+ Gerrit._setPluginsCount = function(count) {
+ Gerrit._pluginsPending = count;
+ if (Gerrit._arePluginsLoaded()) {
+ document.createElement('gr-reporting').pluginsLoaded();
+ }
+ };
+
+ Gerrit._pluginInstalled = function() {
+ Gerrit._setPluginsCount(Gerrit._pluginsPending - 1);
+ };
+
+ Gerrit._arePluginsLoaded = function() {
+ return Gerrit._pluginsPending === 0;
+ };
+
window.Gerrit = Gerrit;
})(window);