elkjs icon indicating copy to clipboard operation
elkjs copied to clipboard

TerminateWorker typing and errors

Open studiosciences opened this issue 2 years ago • 1 comments

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();
    };

studiosciences avatar Jan 10 '23 18:01 studiosciences

+1 on this, was looking to do something similar but couldn't figure it out!

jlaneve avatar Jan 10 '23 20:01 jlaneve