UNPKG

@arrows/worker

Version:

Simple, promise-based API for native worker threads (with pools)

21 lines (20 loc) 784 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const worker_threads_1 = require("worker_threads"); const spawn_1 = require("./spawn"); const work_1 = require("./work"); const getCallerFile = require("get-caller-file"); /** * Spawns workers and returns a function that handles messaging * and returns responses as promises. * * @param handler Function that performs calculations inside worker threads * @param config Configuration options * @returns Async function that communicates with worker threads. */ const worker = (handler, config = {}) => { const fileName = getCallerFile(); return (worker_threads_1.isMainThread ? spawn_1.default(fileName, config) : work_1.default(handler)); }; exports.worker = worker; exports.default = worker;