Cannot 'undo' if updateProperties is called in .added event handler

I don’t know if I’m doing something wrong, or if it’s a bug.

Given a event handler for ‘connection.added’ similar to the one below,

    eventBus.on('connection.added', function (event) {
      var target = event.element.target;
      if (!is(target, 'bpmn:Gateway')) {
        return;
      }

      modeling.updateProperties(target, { gatewayDirection: 'Converging' });
    });

and a BPMN flow A->B->C where C is a gateway.
When performing a undo after having removed B, an error is produced

Error: element with id SequenceFlow_2 already exists

Failing test is available here https://github.com/adbre/bpmn-js/tree/update-properties-in-connection-added
(tip commit https://github.com/adbre/bpmn-js/commit/03b1dc7004bc49ae70125bb278a4a76b970bcc81)

Hello @adbre,

I’ve added an issue regarding this. The way to solve your issue is by adding the listener to connection.added.postExecute, because during execute or revert there shouldn’t be any modeling actions being performed or the execution chain will break. During these phases only atomic actions should be performed. What happens here is that during revert the connection.added is being fired and therefore it’s trying to add SequenceFlow_2 twice.

Cheers,
Ricardo

Thanks! But there is no connection.added.postExecute event.
But there are several commandStack.X.postExecute:slightly_smiling:

Good catch and yes, that is the idea.

You cannot invoke other modeling operations in the atomic execute and revert phases. You can hook into the respective preExecute or postExecute phases though in order to do your magic.

We are going to improve this by throwing an error indicating that (via bpmn-io/bpmn-js#143).