fix(src/utils): enforce consistent formatting and enhance worker script initialization
Refactored worker script initialization in `src/utils/workerUtils.ts` by introducing global variables for safer script execution. Applied consistent formatting adjustments, including removal of redundant semicolons, to improve code readability and maintainability without altering functionality.
This commit is contained in:
parent
a2ad5fabdf
commit
bd123c4bb9
1 changed files with 17 additions and 11 deletions
|
|
@ -15,18 +15,24 @@ export function executeInWebWorker<T>(fnString: string, args: any): Promise<T> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
// Create a blob URL for the worker script
|
// Create a blob URL for the worker script
|
||||||
const workerScript = `
|
const workerScript = `
|
||||||
|
// Define global variables to prevent "is not defined" errors
|
||||||
|
// This ensures that any minified variable names used by bundlers are available
|
||||||
|
self.index = {}
|
||||||
|
self.index$1 = {}
|
||||||
|
self.index$2 = {}
|
||||||
|
|
||||||
self.onmessage = async function(e) {
|
self.onmessage = async function(e) {
|
||||||
try {
|
try {
|
||||||
const fn = ${fnString};
|
const fn = ${fnString}
|
||||||
const result = await fn(e.data);
|
const result = await fn(e.data)
|
||||||
self.postMessage({ success: true, data: result });
|
self.postMessage({ success: true, data: result })
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
self.postMessage({
|
self.postMessage({
|
||||||
success: false,
|
success: false,
|
||||||
error: error instanceof Error ? error.message : String(error)
|
error: error instanceof Error ? error.message : String(error)
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
const blob = new Blob([workerScript], { type: 'application/javascript' })
|
const blob = new Blob([workerScript], { type: 'application/javascript' })
|
||||||
|
|
@ -78,20 +84,20 @@ export function executeInWorkerThread<T>(
|
||||||
|
|
||||||
// Create a worker script
|
// Create a worker script
|
||||||
const workerScript = `
|
const workerScript = `
|
||||||
const { parentPort } = require('worker_threads');
|
const { parentPort } = require('worker_threads')
|
||||||
|
|
||||||
parentPort.once('message', async (data) => {
|
parentPort.once('message', async (data) => {
|
||||||
try {
|
try {
|
||||||
const fn = ${fnString};
|
const fn = ${fnString}
|
||||||
const result = await fn(data);
|
const result = await fn(data)
|
||||||
parentPort.postMessage({ success: true, data: result });
|
parentPort.postMessage({ success: true, data: result })
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
parentPort.postMessage({
|
parentPort.postMessage({
|
||||||
success: false,
|
success: false,
|
||||||
error: error instanceof Error ? error.message : String(error)
|
error: error instanceof Error ? error.message : String(error)
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
`
|
`
|
||||||
|
|
||||||
// Create a worker
|
// Create a worker
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue