UNPKG

redis-smq-common

Version:

Provides essential components and utilities shared across RedisSMQ packages.

47 lines 1.64 kB
import { EWorkerThreadChildExecutionCode, EWorkerThreadChildExitCode, EWorkerThreadParentMessage, } from '../types/index.js'; import { exit } from './worker-thread-message.js'; function isWorkerClass(Worker) { return Worker.prototype && typeof Worker.prototype.constructor === 'function'; } export function handleRunnableWorker(Worker, messagePort, initialPayload) { let instance = null; try { if (isWorkerClass(Worker)) instance = new Worker(initialPayload); else instance = Worker(initialPayload); } catch (err) { exit(EWorkerThreadChildExitCode.INVALID_WORKER_TYPE, err); } if (!instance || typeof instance !== 'object' || !instance.run || !instance.shutdown) { exit(EWorkerThreadChildExitCode.INVALID_WORKER_TYPE); } else { const run = () => { try { instance.run((err) => { if (err) exit(EWorkerThreadChildExecutionCode.PROCESSING_ERROR, err); }); } catch (err) { exit(EWorkerThreadChildExecutionCode.PROCESSING_CAUGHT_ERROR, err); } }; const shutdown = () => { instance.shutdown(() => void 0); }; const onMessage = (message) => { if (message.type === EWorkerThreadParentMessage.RUN) run(); if (message.type === EWorkerThreadParentMessage.SHUTDOWN) shutdown(); }; messagePort.on('message', onMessage); } } //# sourceMappingURL=runnable-worker-thread.js.map