-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TerminateWorker typing and errors #208
Labels
Comments
+1 on this, was looking to do something similar but couldn't figure it out! |
Eddykasp
added a commit
that referenced
this issue
Apr 16, 2024
#208 Add worker termination conditions and method
For anyone else coming here, I wanted to share a short snippet of how to use elkjs with custom termination based on an abort signal: import ELKWorker from "elkjs/lib/elk-worker?worker&inline";
/**
* Run an ELK layout that terminates if the signal is aborted.
*/
function layoutWithCancel(graph: ElkNode, signal: AbortSignal): Promise<ElkNode> {
return new Promise((resolve, reject) => {
// https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal#implementing_an_abortable_api
if (signal.aborted) {
reject(signal.reason);
}
// I am doing something a little odd with the web worker so the source gets inlined, but you can create it however you want with a webworker.
const elk = new ELK({
workerFactory: () => new ELKWorker(),
workerUrl: "",
});
signal.addEventListener("abort", () => {
elk.terminateWorker();
reject(signal.reason);
});
elk.layout(graph).then(resolve, reject);
});
} I am using this with Tanstack Query, which has support for aborting data fetching based on the signal. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
terminateWorker
is documented in the README, but not included in the types. It can also error if the worker is not currently defined.Expected behavior
elk.terminateWorker()
is a documented in typeselk.terminateWorker()
will not error if the worker is not defined (Not sure of the circumstances if this).Screenshots
If applicable, add screenshots to help explain your problem.
ELK Version
0.8.2
Additional context
I want to terminate the layout rendering if the graph changes again before the rendering is complete. I'm not 100% sure that calling terminateWorker will not create a
Workaround
The text was updated successfully, but these errors were encountered: