import path from 'path'; import { Worker } from 'worker_threads'; const WORKER_PATH = path.resolve(process.cwd(), 'assets/app/workers/unionWorker.js'); /** * Circle-union of already-validated, already-decorated subscriptions, run in a * worker thread so the main event loop (DDP/HTTP) stays free regardless of * how long any single turf.union call takes. subs must be an array of plain * { location: { lat, lon }, distance } objects. */ const calcUnionAsync = subs => new Promise((resolve, reject) => { const worker = new Worker(WORKER_PATH, { workerData: { subs } }); worker.once('message', (msg) => { worker.terminate(); if (msg.error) { reject(new Error(msg.error)); } else { resolve(msg.union); } }); worker.once('error', (err) => { worker.terminate(); reject(err); }); }); export default calcUnionAsync;