Merge "Wait for all plugins to load before sending plugin events"
This commit is contained in:
commit
62f911698d
@ -48,6 +48,7 @@
|
||||
EventType: EventType,
|
||||
|
||||
handleEvent: function(type, detail) {
|
||||
Gerrit.awaitPluginsLoaded().then(function() {
|
||||
switch (type) {
|
||||
case EventType.HISTORY:
|
||||
this._handleHistory(detail);
|
||||
@ -62,9 +63,11 @@
|
||||
this._handleLabelChange(detail);
|
||||
break;
|
||||
default:
|
||||
console.warn('handleEvent called with unsupported event type:', type);
|
||||
console.warn('handleEvent called with unsupported event type:',
|
||||
type);
|
||||
break;
|
||||
}
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
addElement: function(key, el) {
|
||||
|
@ -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);
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user