methods.js
This file
Path
lib/ipc/methods.js
50 lines · 1.3 KiB · JavaScript
Last changed by
Update dev dependencies (#1257)Sindre Sorhusbefore adoption5944b2d
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051import process from 'node:process';import {sendMessage} from './send.js';import {getOneMessage} from './get-one.js';import {getEachMessage} from './get-each.js';import {getCancelSignal} from './graceful.js';
// Add promise-based IPC methods in current processexport const addIpcMethods = (target, subprocess, {ipc}) => { Object.assign(target, getIpcMethods(subprocess, false, ipc, target));};
// Get promise-based IPC in the subprocessexport const getIpcExport = () => { const anyProcess = process; const isSubprocess = true; const isIpc = process.channel !== undefined;
return { ...getIpcMethods(anyProcess, isSubprocess, isIpc), getCancelSignal: getCancelSignal.bind(undefined, { anyProcess, channel: anyProcess.channel, isSubprocess, ipc: isIpc, }), };};
// Retrieve the `ipc` shared by both the current process and the subprocessconst getIpcMethods = (anyProcess, isSubprocess, ipc, waitProcess = anyProcess) => ({ sendMessage: sendMessage.bind(undefined, { anyProcess, channel: anyProcess.channel, isSubprocess, ipc, }), getOneMessage: getOneMessage.bind(undefined, { anyProcess, channel: anyProcess.channel, isSubprocess, ipc, }), getEachMessage: getEachMessage.bind(undefined, { anyProcess, channel: anyProcess.channel, isSubprocess, ipc, waitProcess, }),});