Skip to content
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

Closed
studiosciences opened this issue Jan 10, 2023 · 2 comments · Fixed by #274
Closed

TerminateWorker typing and errors #208

studiosciences opened this issue Jan 10, 2023 · 2 comments · Fixed by #274
Labels

Comments

@studiosciences
Copy link

studiosciences commented Jan 10, 2023

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 types
  • elk.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

  useEffect(() => {
    const elk = new ELK({
      workerUrl: webWorkerUrl,
    });

    elk
      .layout(elkInput)
      .then((elkOutput) => {
        // ...consume the elkOutput
      })
      .catch((err) => {
        throw new Error(err);
      });

    // Cancel the current job when input changes.
    return elk.terminateWorker; // <-- HERE
  }, [elkInput, webWorkerUrl]);

Workaround

    // Cancel the current job when input changes.
    return () => {
      // @ts-expect-error function is not documented
      if (elk.worker) elk.terminateWorker();
    };
@jlaneve
Copy link

jlaneve commented Jan 10, 2023

+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
@saulshanabrook
Copy link
Contributor

saulshanabrook commented Oct 11, 2024

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
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants