-
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.
- fix bug for when there was multiple workers defined in the same file - added a new function that behave like an async function but run in parallel - added a new helper to execute a function in background
- Loading branch information
Showing
5 changed files
with
97 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { fileURLToPath } from 'node:url' | ||
import { Worker, isMainThread, parentPort, workerData } from 'node:worker_threads' | ||
import { getExecutionId } from './executionOrder.ts' | ||
import { fork, resolvablePromise } from './utils.ts' | ||
|
||
export function execParallel<Props, ReturnType>(filePath: string, fn: (props: Props) => ReturnType) { | ||
const { executionId } = getExecutionId(filePath) | ||
|
||
if (isMainThread && process.env['execParallelId'] === executionId) { | ||
fork(async () => { | ||
const res = await fn(workerData) | ||
parentPort?.postMessage(res) | ||
|
||
process.exit(0) | ||
}) | ||
} | ||
|
||
return (props: Props) => { | ||
const worker = new Worker(fileURLToPath(filePath), { | ||
workerData: props, | ||
env: { ...process.env, execParallelId: executionId } | ||
}) | ||
|
||
const { promise, resolvePromise } = resolvablePromise<ReturnType>() | ||
|
||
worker.on('message', data => { | ||
resolvePromise(data) | ||
}) | ||
|
||
return promise | ||
} | ||
} |
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,14 @@ | ||
const executionMap = new Map<string, number>() | ||
|
||
/** | ||
* Return an Id that it's consistent for every execution | ||
* It's based on the order of execution | ||
*/ | ||
export const getExecutionId = (fileName: string) => { | ||
executionMap.get(fileName) ?? executionMap.set(fileName, 0) | ||
|
||
const index = executionMap.get(fileName) ?? 0 | ||
executionMap.set(fileName, index + 1) | ||
|
||
return { executionId: index.toString() } | ||
} |
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