Events provided by EventBus are updated asynchronously

Hi all,

I’m having an issue where I need information from a commandStack event as part of a payload in a REST call to a remote server.

In this case, I am listening to the commandStack.shape.move.executed event, in which I get the event.context.shape.incoming.waypoints array to use in a REST call:

eventBus.on('commandStack.shape.move.executed', (event) => {
  updateShapeOnMove(event); // this makes a REST call
});

However, when I send out the REST call, the event has not updated with the new line waypoints (event.context.shape.incoming.waypoints). I’ve tried using the .postExecute and .postExecuted command stack events without luck. The only successful way I’ve been able to ensure that the referenced event is updated in time for the REST call is by using a timeout:

setTimeout(() => updateShapeOnMove(event), 1000);  

I don’t like this solution. Is there a better way to know when the referenced event is updated?

There’s another event that’s fired after these shape.move events called commandStack.connection.layout.postExecuted and at that point, it seems like the event is updated with the new waypoints. So it sounds like I need to wait for this event to fire before I make my REST call. Non-trivial, but doable.

There are different phases during command execution. Simply put there is a pre-execution, execution and post-execution phase. These different phases allow anyone to hook into command execution and execute additional steps. If you want to make sure you’re getting the event after everyone else is finished listen for postExecuted with low priority:

const LOW_PRIORITY = 100;

eventBus.on('commandStack.shape.move.postExecuted', LOW_PRIORITY, event => {
  // ...
});