-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathTaskProcessorThread.js
More file actions
31 lines (25 loc) · 957 Bytes
/
TaskProcessorThread.js
File metadata and controls
31 lines (25 loc) · 957 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import workerpool from "workerpool";
import {FsWorkerThreadInterface, deserializeResources, serializeData, deserializeData} from "./PoolDispatcher.js";
export default async function execInThread({modulePath, methodName, args}) {
const moduleToExecute = await import(modulePath);
const methodCall = moduleToExecute[methodName] || moduleToExecute["default"];
const {options, resources, fs} = args;
const buildUpArgs = {options: await deserializeData(options)};
if (resources) {
buildUpArgs.resources = await deserializeResources(resources);
}
if (fs) {
buildUpArgs.fs = new FsWorkerThreadInterface(fs);
}
const result = await methodCall(buildUpArgs);
return serializeData(result);
}
// Test execution via ava is never done on the main thread
/* istanbul ignore else */
if (!workerpool.isMainThread) {
// Script got loaded through workerpool
// => Create a worker and register public functions
workerpool.worker({
execInThread,
});
}