Merge "Wait for all plugins to load before sending plugin events"

This commit is contained in:
Viktar Donich 2016-12-09 18:45:47 +00:00 committed by Gerrit Code Review
commit 62f911698d
3 changed files with 60 additions and 22 deletions

View File

@ -48,23 +48,26 @@
EventType: EventType,
handleEvent: function(type, detail) {
switch (type) {
case EventType.HISTORY:
this._handleHistory(detail);
break;
case EventType.SHOW_CHANGE:
this._handleShowChange(detail);
break;
case EventType.COMMENT:
this._handleComment(detail);
break;
case EventType.LABEL_CHANGE:
this._handleLabelChange(detail);
break;
default:
console.warn('handleEvent called with unsupported event type:', type);
break;
}
Gerrit.awaitPluginsLoaded().then(function() {
switch (type) {
case EventType.HISTORY:
this._handleHistory(detail);
break;
case EventType.SHOW_CHANGE:
this._handleShowChange(detail);
break;
case EventType.COMMENT:
this._handleComment(detail);
break;
case EventType.LABEL_CHANGE:
this._handleLabelChange(detail);
break;
default:
console.warn('handleEvent called with unsupported event type:',
type);
break;
}
}.bind(this));
},
addElement: function(key, el) {

View File

@ -45,6 +45,7 @@ limitations under the License.
});
element = fixture('basic');
errorStub = sinon.stub(console, 'error');
Gerrit._setPluginsCount(1);
Gerrit.install(function(p) { plugin = p; }, '0.1',
'http://test.com/plugins/testplugin/static/test.js');
});
@ -75,10 +76,7 @@ limitations under the License.
test('showchange event', function(done) {
var testChange = {
_number: 42,
revisions: {
def: {_number: 2},
abc: {_number: 1},
},
revisions: {def: {_number: 2}, abc: {_number: 1}},
};
plugin.on(element.EventType.SHOW_CHANGE, throwErrFn);
plugin.on(element.EventType.SHOW_CHANGE, function(change, revision) {
@ -91,6 +89,24 @@ limitations under the License.
{change: testChange, patchNum: 1});
});
test('handleEvent awaits plugins load', function(done) {
var testChange = {
_number: 42,
revisions: {def: {_number: 2}, abc: {_number: 1}},
};
var spy = sinon.spy();
Gerrit._setPluginsCount(1);
plugin.on(element.EventType.SHOW_CHANGE, spy);
element.handleEvent(element.EventType.SHOW_CHANGE,
{change: testChange, patchNum: 1});
assert.isFalse(spy.called);
Gerrit._setPluginsCount(0);
flush(function() {
assert.isTrue(spy.called);
done();
});
});
test('comment event', function(done) {
var testCommentNode = {foo: 'bar'};
plugin.on(element.EventType.COMMENT, throwErrFn);
@ -196,7 +212,7 @@ limitations under the License.
});
test('_arePluginsLoaded', function() {
assert.isFalse(Gerrit._arePluginsLoaded());
assert.isTrue(Gerrit._arePluginsLoaded());
Gerrit._setPluginsCount(1);
assert.isFalse(Gerrit._arePluginsLoaded());
Gerrit._setPluginsCount(0);

View File

@ -111,10 +111,29 @@
Gerrit._pluginInstalled();
};
Gerrit._allPluginsPromise = null;
Gerrit._resolveAllPluginsLoaded = null;
Gerrit.awaitPluginsLoaded = function() {
if (!Gerrit._allPluginsPromise) {
if (Gerrit._arePluginsLoaded()) {
Gerrit._allPluginsPromise = Promise.resolve();
} else {
Gerrit._allPluginsPromise = new Promise(function(resolve) {
Gerrit._resolveAllPluginsLoaded = resolve;
});
}
}
return Gerrit._allPluginsPromise;
};
Gerrit._setPluginsCount = function(count) {
Gerrit._pluginsPending = count;
if (Gerrit._arePluginsLoaded()) {
document.createElement('gr-reporting').pluginsLoaded();
if (Gerrit._resolveAllPluginsLoaded) {
Gerrit._resolveAllPluginsLoaded();
}
}
};