feat(lib): deprecate import.parse.complete <context> payload

The <import.parse.complete> event is now invoked with

{
 error,
 definitions,
 elementsById,
 references,
 warnings
}

The old payload is deprecated:

{
  error,
  definitions,
  context: { elementsById, references, warnings }
}
This commit is contained in:
Oguz 2020-04-27 07:57:45 +02:00 committed by Nico Rehwaldt
parent 04ca31fac9
commit 157aec6ed6
3 changed files with 64 additions and 17 deletions

View File

@ -24,7 +24,7 @@ export default function BaseModeler(options) {
// hook ID collection into the modeler
this.on('import.parse.complete', function(event) {
if (!event.error) {
this._collectIds(event.definitions, event.context);
this._collectIds(event.definitions, event.elementsById);
}
}, this);
@ -59,7 +59,7 @@ BaseModeler.prototype._createModdle = function(options) {
* @param {ModdleElement} definitions
* @param {Context} context
*/
BaseModeler.prototype._collectIds = function(definitions, context) {
BaseModeler.prototype._collectIds = function(definitions, elementsById) {
var moddle = definitions.$model,
ids = moddle.ids,
@ -68,7 +68,7 @@ BaseModeler.prototype._collectIds = function(definitions, context) {
// remove references from previous import
ids.clear();
for (id in context.elementsById) {
ids.claim(id, context.elementsById[id]);
for (id in elementsById) {
ids.claim(id, elementsById[id]);
}
};

View File

@ -110,6 +110,31 @@ BaseViewer.prototype.importXML = wrapForCompatibility(function importXML(xml, bp
var self = this;
function ParseCompleteEvent(data) {
var event = self.get('eventBus').createEvent(data);
// TODO(nikku): remove with future bpmn-js version
Object.defineProperty(event, 'context', {
enumerable: true,
get: function() {
console.warn(new Error(
'import.parse.complete <context> is deprecated ' +
'and will be removed in future library versions'
));
return {
warnings: data.warnings,
references: data.references,
elementsById: data.elementsById
};
}
});
return event;
}
return new Promise(function(resolve, reject) {
// hook in pre-parse listeners +
@ -122,19 +147,15 @@ BaseViewer.prototype.importXML = wrapForCompatibility(function importXML(xml, bp
var parseWarnings = result.warnings;
var elementsById = result.elementsById;
var context = {
// hook in post parse listeners +
// allow definitions manipulation
definitions = self._emit('import.parse.complete', ParseCompleteEvent({
error: null,
definitions: definitions,
elementsById: elementsById,
references: references,
warnings: parseWarnings
};
// hook in post parse listeners +
// allow definitions manipulation
definitions = self._emit('import.parse.complete', {
error: null,
definitions: definitions,
context: context
}) || definitions;
})) || definitions;
self.importDefinitions(definitions, bpmnDiagram).then(function(result) {
var allWarnings = [].concat(parseWarnings, result.warnings || []);

View File

@ -635,7 +635,7 @@ describe('Viewer', function() {
// then
expect(events).to.eql([
[ 'import.parse.start', [ 'xml' ] ],
[ 'import.parse.complete', ['error', 'definitions', 'context' ] ],
[ 'import.parse.complete', [ 'error', 'definitions', 'elementsById', 'references', 'warnings', 'context' ] ],
[ 'import.render.start', [ 'definitions' ] ],
[ 'import.render.complete', [ 'error', 'warnings' ] ],
[ 'import.done', [ 'error', 'warnings' ] ]
@ -1673,7 +1673,7 @@ describe('Viewer', function() {
// then
expect(events).to.eql([
[ 'import.parse.start', [ 'xml' ] ],
[ 'import.parse.complete', ['error', 'definitions', 'context' ] ],
[ 'import.parse.complete', ['error', 'definitions', 'elementsById', 'references', 'warnings', 'context' ] ],
[ 'import.render.start', [ 'definitions' ] ],
[ 'import.render.complete', [ 'error', 'warnings' ] ],
[ 'import.done', [ 'error', 'warnings' ] ]
@ -1683,6 +1683,32 @@ describe('Viewer', function() {
});
});
it('should emit <import.parse.complete> event', function() {
// given
var viewer = new Viewer({ container: container });
var xml = require('../fixtures/bpmn/simple.bpmn');
viewer.on([
'import.parse.complete'
], function(event) {
// then
var context = event.context;
expect(context).to.exist;
expect(context.warnings).to.equal(event.warnings);
expect(context.elementsById).to.equal(event.elementsById);
expect(context.references).to.equal(event.references);
});
// when
return viewer.importXML(xml);
});
});