UNPKG

@arrows/worker

Version:

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

24 lines (23 loc) 803 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const types_1 = require("./types"); exports.transferKey = types_1.transferKey; /** * Wraps a worker handler so you can add a transfer list for the result. * * Works only if the result is an instance of a Buffer or a TypedArray. * * @param handler - Handler function * @param mapperFn - Takes the result and should produce a transfer list. * @returns A wrapped handler whose result will be moved rather than cloned. */ const transfer = (handler, mapperFn) => (payload, workerData) => { const result = handler(payload, workerData); const transferList = mapperFn(result); return { result, [types_1.transferKey]: transferList, }; }; exports.transfer = transfer; exports.default = transfer;