Controls the early termination of graph algorithms.
Remarks
The LayoutAbortController class allows graph algorithms to be interrupted or terminated before they complete. It supports both graceful terminations (via the stop method) and immediate cancellations (via the cancel method).
This class is particularly useful when algorithms are long-running and need to be interrupted due to time constraints or user interactions. It can be used in both multithreaded and single-threaded scenarios.
Usage in Client Code
When working with IGraph and related APIs, the LayoutAbortController of the LayoutExecutor provides an instance of this class that can be configured and monitored by layout algorithms.
If working directly with the LayoutGraph API, an instance of this class should be attached via the layoutAbortController property. This ensures that layout algorithms executed on the graph will respect stop or cancel requests.
- stop – Schedules a request for graceful termination. This allows the algorithm to finish its current step and leave the graph in a consistent state.
- cancel – Requests an immediate termination, discarding any partial results. There is no guarantee that the graph or the layout algorithm will be left in a consistent state, and any in-progress work will be discarded.
In scenarios where an algorithm runs multiple times on the same graph or where different algorithms are applied sequentially, the controller must be reset using the reset method before each execution. Failure to reset the controller can result in unintended early terminations due to lingering stop or cancel requests.
stop and cancel are designed for multithreaded scenarios where the algorithm runs in a background thread while the stop or cancel requests are issued from the main thread. In single-threaded environments, you can use stopDuration and cancelDuration to automatically terminate an algorithm after a specified duration.
Algorithm Development
When developing custom algorithms, you should query the controller for termination requests by invoking the check method at appropriate points within the algorithm. This ensures that stop and cancel requests are respected, allowing for smooth integration with the LayoutAbortController.
If a stop request is detected, ensure the algorithm completes its work in a way that keeps the graph in a consistent state. If a cancel request is detected, terminate immediately and discard any intermediate results.
Default Values of Properties
cancelDuration | MAX_VALUE
| No automatic termination will occur. |
stopDuration | MAX_VALUE
| No automatic termination will occur. |
Type Details
- yFiles module
- algorithms
See Also
Constructors
Initializes a new instance of the LayoutAbortController class.
Properties
Gets or sets the maximum duration an algorithm may run before being canceled automatically.
Remarks
Default Value
Property Value
Throws
- Exception({ name: 'ArgumentError' })
- Thrown if the duration is negative.
See Also
Gets a value indicating whether a cancel request has been scheduled.
Gets or sets the maximum duration an algorithm may run before being stopped automatically.
Remarks
Default Value
Property Value
Throws
- Exception({ name: 'ArgumentError' })
- Thrown when the duration is negative.
See Also
Gets a value indicating whether a stop request has been scheduled.
Methods
Schedules a cancellation request.
Remarks
Calling the check method after a cancellation request has been scheduled will result in an AlgorithmAbortedError being thrown. This immediately halts the execution of the current layout algorithm. Since the algorithm is terminated prematurely, the state of the processed graph cannot be guaranteed to be valid or consistent.
It is recommended to schedule cancellation requests only for algorithms that work on a copy of the graph, rather than the original graph structure. This prevents the risk of leaving the graph in an inconsistent or partially updated state.
Examples
const copiedGraph = LayoutGraph.createCopy(graph)
copiedGraph.applyLayout(layout, layoutData)
copiedGraph.context.graphCopyData?.commitLayoutToOriginalGraph()
See Also
Checks if the algorithm should terminate.
Remarks
Returns
- ↪boolean
true
if the algorithm should stop gracefully,false
otherwise.
Throws
- Exception({ name: 'AlgorithmAbortedError' })
- Thrown if the algorithm should terminate immediately due to a cancellation.
See Also
Resets the state of the LayoutAbortController.
Remarks
See Also
Schedules a graceful termination request.
Remarks
When a stop request is scheduled, the algorithm will attempt to finish its current operation and ensure that the processed graph is left in a consistent state.
If a cancel request has already been made, the stop request is ignored. If a cancel request is made later, it will override the stop request.