From 8e68d36d95e17e73d6dcbb505ad059f9fb251b7c Mon Sep 17 00:00:00 2001 From: Ryan Di Date: Tue, 1 Apr 2025 16:30:16 +1100 Subject: [PATCH] refactor --- packages/excalidraw/lasso/index.ts | 2 +- .../excalidraw/lasso/lasso-worker.chunk.ts | 78 ------------------- packages/excalidraw/lasso/lasso-worker.ts | 74 ++++++++++++++++++ 3 files changed, 75 insertions(+), 79 deletions(-) delete mode 100644 packages/excalidraw/lasso/lasso-worker.chunk.ts create mode 100644 packages/excalidraw/lasso/lasso-worker.ts diff --git a/packages/excalidraw/lasso/index.ts b/packages/excalidraw/lasso/index.ts index ee1cdba2c..b8fa058a0 100644 --- a/packages/excalidraw/lasso/index.ts +++ b/packages/excalidraw/lasso/index.ts @@ -29,7 +29,7 @@ import { type AnimationFrameHandler } from "../animation-frame-handler"; import { AnimatedTrail } from "../animated-trail"; -import LassoWorker from "./lasso-worker.chunk?worker"; +import LassoWorker from "./lasso-worker?worker"; import { LassoWorkerPolyfill } from "./lasso-worker-polyfill"; diff --git a/packages/excalidraw/lasso/lasso-worker.chunk.ts b/packages/excalidraw/lasso/lasso-worker.chunk.ts deleted file mode 100644 index f6f0d0fe9..000000000 --- a/packages/excalidraw/lasso/lasso-worker.chunk.ts +++ /dev/null @@ -1,78 +0,0 @@ -import { updateSelection } from "./utils"; - -import type { LassoWorkerInput } from "./types"; - -export const WorkerUrl: URL | undefined = import.meta.url - ? new URL(import.meta.url) - : undefined; - -// only run in the worker context -if (typeof window === "undefined" && typeof self !== "undefined") { - // variables to track processing state and latest input data - // for "backpressure" purposes - let isProcessing: boolean = false; - let latestInputData: LassoWorkerInput | null = null; - - self.onmessage = (event: MessageEvent) => { - if (!event.data) { - self.postMessage({ - error: "No data received", - selectedElementIds: [], - }); - return; - } - - latestInputData = event.data; - - if (!isProcessing) { - processInputData(); - } - }; - - // function to process the latest data - const processInputData = () => { - // If no data to process, return - if (!latestInputData) { - return; - } - - // capture the current data to process and reset latestData - const dataToProcess = latestInputData; - latestInputData = null; // reset to avoid re-processing the same data - isProcessing = true; - - try { - const { lassoPath, elements, intersectedElements, enclosedElements } = - dataToProcess; - - if (!Array.isArray(lassoPath) || !Array.isArray(elements)) { - throw new Error("Invalid input: lassoPath and elements must be arrays"); - } - - if ( - !(intersectedElements instanceof Set) || - !(enclosedElements instanceof Set) - ) { - throw new Error( - "Invalid input: intersectedElements and enclosedElements must be Sets", - ); - } - - const result = updateSelection(dataToProcess); - self.postMessage(result); - } catch (error) { - self.postMessage({ - error: - error instanceof Error ? error.message : "Unknown error occurred", - selectedElementIds: [], - }); - } finally { - isProcessing = false; - // if new data arrived during processing, process it - // as we're done with processing the previous data - if (latestInputData) { - processInputData(); - } - } - }; -} diff --git a/packages/excalidraw/lasso/lasso-worker.ts b/packages/excalidraw/lasso/lasso-worker.ts new file mode 100644 index 000000000..aa22889a9 --- /dev/null +++ b/packages/excalidraw/lasso/lasso-worker.ts @@ -0,0 +1,74 @@ +import { updateSelection } from "./utils"; + +import type { LassoWorkerInput } from "./types"; + +const ctx = self as unknown as Worker; + +// variables to track processing state and latest input data +// for "backpressure" purposes +let isProcessing: boolean = false; +let latestInputData: LassoWorkerInput | null = null; + +self.onmessage = (event: MessageEvent) => { + if (!event.data) { + self.postMessage({ + error: "No data received", + selectedElementIds: [], + }); + return; + } + + latestInputData = event.data; + + if (!isProcessing) { + processInputData(); + } +}; + +// function to process the latest data +const processInputData = () => { + // If no data to process, return + if (!latestInputData) { + return; + } + + // capture the current data to process and reset latestData + const dataToProcess = latestInputData; + latestInputData = null; // reset to avoid re-processing the same data + isProcessing = true; + + try { + const { lassoPath, elements, intersectedElements, enclosedElements } = + dataToProcess; + + if (!Array.isArray(lassoPath) || !Array.isArray(elements)) { + throw new Error("Invalid input: lassoPath and elements must be arrays"); + } + + if ( + !(intersectedElements instanceof Set) || + !(enclosedElements instanceof Set) + ) { + throw new Error( + "Invalid input: intersectedElements and enclosedElements must be Sets", + ); + } + + const result = updateSelection(dataToProcess); + self.postMessage(result); + } catch (error) { + self.postMessage({ + error: error instanceof Error ? error.message : "Unknown error occurred", + selectedElementIds: [], + }); + } finally { + isProcessing = false; + // if new data arrived during processing, process it + // as we're done with processing the previous data + if (latestInputData) { + processInputData(); + } + } +}; + +export default ctx;