Remove potentially unnecessary optimisation

This commit is contained in:
Marcel Mraz 2025-04-02 11:34:10 +01:00
parent 1beab4f5c0
commit aab03481a2
2 changed files with 6 additions and 46 deletions

View file

@ -15,7 +15,8 @@
"scripts": {
"start": "vite",
"build": "vite build",
"build:preview": "yarn build && vite preview --port 5002",
"preview": "vite preview --port 5002",
"build:preview": "yarn build && yarn preview",
"build:package": "yarn workspace @excalidraw/excalidraw run build:esm"
}
}

View file

@ -12,55 +12,14 @@ export const WorkerUrl: URL | undefined = import.meta.url
? new URL(import.meta.url)
: undefined;
// variables to track processing state and latest input data
// for "backpressure" purposes
let isProcessing: boolean = false;
let latestInputData: LassoWorkerInput | null = null;
// run only in the worker context
if (typeof window === "undefined" && typeof self !== "undefined") {
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 {
switch (dataToProcess.command) {
switch (event.data.command) {
case Commands.GET_LASSO_SELECTED_ELEMENT_IDS:
const result = getLassoSelectedElementIds(dataToProcess);
const result = getLassoSelectedElementIds(event.data);
self.postMessage(result);
break;
}
} finally {
isProcessing = false;
// if new data arrived during processing, process it
// as we're done with processing the previous data
if (latestInputData) {
processInputData();
}
}
};
};
}