-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add createWorker function for non-React usage and TanStack Quer…
…y support
- Loading branch information
Showing
7 changed files
with
138 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
export { useWorker } from './useWorker'; | ||
export { createWorker } from './worker'; | ||
export type { WorkerFunction } from './worker'; | ||
export type { | ||
WorkerStatus, | ||
WorkerState, | ||
WorkerOptions, | ||
WorkerFunction, | ||
UseWorkerReturn, | ||
} from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
export type WorkerFunction<TArgs, TResult> = (args: TArgs) => TResult | Promise<TResult>; | ||
|
||
export function createWorker<TArgs, TResult>( | ||
fn: WorkerFunction<TArgs, TResult>, | ||
options: { transferable?: boolean } = {} | ||
) { | ||
const fnString = fn.toString(); | ||
const workerCode = ` | ||
self.onmessage = async (e) => { | ||
try { | ||
const fn = ${fnString}; | ||
const result = await fn(e.data); | ||
self.postMessage({ type: 'result', data: result }); | ||
} catch (error) { | ||
self.postMessage({ type: 'error', error: error.message }); | ||
} | ||
}; | ||
`; | ||
|
||
const blob = new Blob([workerCode], { type: 'application/javascript' }); | ||
const url = URL.createObjectURL(blob); | ||
const worker = new Worker(url); | ||
URL.revokeObjectURL(url); | ||
|
||
return { | ||
run: (args: TArgs, transferables?: Transferable[]): Promise<TResult> => { | ||
return new Promise((resolve, reject) => { | ||
worker.onmessage = (e) => { | ||
const { type, data, error } = e.data; | ||
if (type === 'result') { | ||
resolve(data as TResult); | ||
} else if (type === 'error') { | ||
reject(new Error(error)); | ||
} | ||
}; | ||
|
||
worker.onerror = (error) => { | ||
reject(new Error(error.message)); | ||
}; | ||
|
||
if (options.transferable && transferables) { | ||
worker.postMessage(args, transferables); | ||
} else { | ||
worker.postMessage(args); | ||
} | ||
}); | ||
}, | ||
terminate: () => worker.terminate() | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters