How to conditionally reset the command stack?

Hi all,

We have a UI service that polls a backend that serves BPMN XML. Every time the UI receives this XML, it calls modeler.importXML(). However, it seems every time importXML is called, the commandStack._stack array resets. What’s the easiest way to prevent this from happening and only when a certain condition is met (e.g. when the BPMNDiagram id changes)?

Whenever you use modeler.importXML(), the previously used diagram definitions and all of the objects used in previous actions are wiped out. Thus, it is not possible to use the commandStack._stack from the previous diagram even if the XML is the same. The only way to keep the stack is to not re-import diagram during work.

Its not possible to extend the existing CommandStack implementation and override this line? https://github.com/bpmn-io/diagram-js/blob/develop/lib/command/CommandStack.js#L122

Although you can technically override that line, it still will not allow you to undo or redo any actions performed on the diagram before the new import. The result of a try will be a bunch of errors, but not the desired behaviour.

Why do you actually want to keep the old stack? Maybe we could figure out a solution that would fit your needs.

1 Like

I have mapped a lot of BPMN events to REST calls that are sent to another system. Those calls respond with an updated XML payload reflecting the event that just occurred. This remote system is also registered as an event source, so XML updates are pushed to the UI. Naturally, I import this new XML via the modeler. One of the reasons we do it this way is that it allows multiple users to concurrently edit the same BPMN diagram (sort of in a Google Docs style).

Keeping the old stack around will allow users to undo or redo their actions such as adding a task. I can map those undo redo events to POST or DELETES as necessary.

Still, the diagram loaded to bpmn-js is represented inside as a complex JavaScript object and not a XML. As such, the undo and redo actions are related to that object and its properties. Whenever you re-import the XML, a new JavaScript data structure is built and the links inside of the executed commands become invalid. Thus, I am not sure whether keeping the old stack makes any sense.

In your use case, I would rather give up with re-importing XML. Instead, you could map the modeling actions taken by users and then execute them programmatically on the others’ machines. Thus, you could keep the stack and allow to undo and redo commands.

That would work, but it would require a lot of backend changes on the remote system we integrate the bpmn-js library with. That also adds a good bit of complexity.

I’ll try out overriding the commandstack impl and see what happens, otherwise we would have to do what you suggested.

Did you find out what happens? :nerd_face: