addNodeData then findNodeForData results in null

Given the following code, we get lots of inconsistent behavior:

const node = diagram.findNodeForData(data)
// then we edit the node and also set some fields if the node is within the virtualized viewport

The problem is, the node variable is often null, sometimes it is not null.
How do we make this more reliable so that node is never null in this case.

You should call addNodeData within a transaction, then findNodeForData after that transaction, at which point you know it’s been added to the model.

diagram.model.commit(function(m) {
const node = diagram.findNodeForData(data);
// ...

I’m wondering if the virtualization has something to do with the inconsistency. If the added Node isn’t in the viewport, perhaps it is being removed from the Diagram.

@jhardy We tried the transaction thing, didn’t work

if we add rebuildParts, it works (only as a test of course)

const node = diagram.findNodeForData(data)

so, not sure what’s happening in ‘rebuildParts’ that would sync the diagram to the node data

If you disable or remove your virtualization code, does everything work reliably?

It appears we might have it working by delaying the logic of adding node data and finding nodes on the diagram based on the data until after the ‘InitialLayoutCompleted’ event fires for both the whole diagram (used in overview) and the virtual diagram.