Approach to handling delay when modifying model

Intro: This is about ‘fat’ models and delays in re-draws after the model is changed.

Question: Is there a way to trigger a function to be called ONLY after completion of a change to the model is reflected in the diagram. Put another way, I’m checking the position of a node immediately after adding to the model but it seems that my code runs before the new node has ‘landed’ on the diagram in its final location.

I know I can use a listener for the diagram event ‘LayoutCompleted’, but this is non-specific - how will I know it is running for a specific model change ?

Alternatively, is there a way to stop the diagram updating untlil specifically requested ? So I can freeze the redraws, add new nodes to the model then call for the layout update knowing that the next ‘LayoutCompleted’ covers all of my changes ?

Just FYI, animation libraries provide features that allow the programmer to set a callback when a long-running process is completed. It feels like that is what is needed here.

That is the purpose of the “LayoutCompleted” DiagramEvent. And there is also the “AnimationFinished” DiagramEvent, if you care.

While manipulating a model there is no updating until the end of the transaction. JavaScript runs in a single thread, so as long as your code is doing something, there won’t be any rendering of the diagram.

Although there are some ways of forcing an update to happen earlier. That is the usual complaint, which is the opposite of what you are asking about.

There can be a lot of reasons for a layout to be performed. In fact each time one adds or removes a node or a link the responsible Layout is invalidated. But for obvious performance reasons all of those invalidations are batched together by the transaction, so the layout is performed only once for all of those model changes.

OK - so if I

  • start a transaction
  • add multiple nodes to the model
  • complete the transaction

Then that will cause one “LayoutCompleted” DiagramEvent ?

That is correct. Tell me if you find otherwise.