node-worker-threads-pool-ts
Version:
Simple worker threads pool using Node's worker_threads module. Compatible with ES6+ Promise, Typescript, Async/Await.
48 lines • 1.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DynamicTaskExecutor = exports.StaticTaskExecutor = void 0;
const create_code_1 = require("./create-code");
class BaseTaskExecutor {
constructor(pool) {
this._taskConfig = {};
this._called = false;
this._pool = pool;
}
/** Set timeout (in millisecond) to this task. */
setTimeout(t) {
this._taskConfig.timeout = t;
return this;
}
/**
* @see {@link https://nodejs.org/dist/latest-v14.x/docs/api/worker_threads.html#worker_threads_port_postmessage_value_transferlist transferList}
*/
setTransferList(transferList) {
this._taskConfig.transferList = transferList;
return this;
}
/** Execute this task with the parameter provided. */
async exec(param) {
if (this._called) {
throw new Error("task executor is already called!");
}
this._called = true;
return await this._pool.runTask(param, this._taskConfig);
}
}
/** Executor for StaticPool. Used to apply some advanced settings to a task. */
class StaticTaskExecutor extends BaseTaskExecutor {
}
exports.StaticTaskExecutor = StaticTaskExecutor;
/** Executor for DynamicPool. Used to apply some advanced settings to a task. */
class DynamicTaskExecutor extends BaseTaskExecutor {
constructor(dynamicPool, task) {
super(dynamicPool);
this._code = create_code_1.createCode(task);
}
async exec(param) {
const workerParam = { code: this._code, param };
return await super.exec(workerParam);
}
}
exports.DynamicTaskExecutor = DynamicTaskExecutor;
//# sourceMappingURL=task-executor.js.map