mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
refactor
This commit is contained in:
parent
7c70f77255
commit
8e68d36d95
3 changed files with 75 additions and 79 deletions
|
@ -29,7 +29,7 @@ import { type AnimationFrameHandler } from "../animation-frame-handler";
|
||||||
|
|
||||||
import { AnimatedTrail } from "../animated-trail";
|
import { AnimatedTrail } from "../animated-trail";
|
||||||
|
|
||||||
import LassoWorker from "./lasso-worker.chunk?worker";
|
import LassoWorker from "./lasso-worker?worker";
|
||||||
|
|
||||||
import { LassoWorkerPolyfill } from "./lasso-worker-polyfill";
|
import { LassoWorkerPolyfill } from "./lasso-worker-polyfill";
|
||||||
|
|
||||||
|
|
|
@ -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<LassoWorkerInput>) => {
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
74
packages/excalidraw/lasso/lasso-worker.ts
Normal file
74
packages/excalidraw/lasso/lasso-worker.ts
Normal file
|
@ -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<LassoWorkerInput>) => {
|
||||||
|
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;
|
Loading…
Add table
Add a link
Reference in a new issue