Easily create and manage subprocesses through json signals.
const { createSubprocess, handleMain } = require('@ideadesignmedia/subprocess')
createSubprocess(command, args = [], options = {}, onSignal: ({type: string, data: any}) => void) => { child, eventManager request, send, close, id }
Creates a subprocess and returns a function to send signals to it.
command
- The command to run in the subprocess.
args
- The arguments to pass to the command.
options
- The options to pass to the child process. If you require ipc for communication use {ipc: true}
.
onSignal({type: string, data: any}) => void
- A function to handle signals from the subprocess.
Object with keys:
type: string
- The type of signal received.
data: any
- The data received.
child: ChildProcess
- The child process.
eventManager: EventEmitter
- The event manager for the child process.
request: (type: string, data: any) => Promise<{type: 'success'|'failure', data: any}>
- A function to send a request to the subprocess and wait for a response.
send: ({type: string, data: any}) => void
- A function to send a signal to the subprocess.
close: () => void
- A function to close the subprocess.
id: string
- The id of the subprocess.
handleMain(onMessage: ({type: string, data: any}) => void) => { eventManager, sendToMain }
Creates a handler for signals to the subprocess and returns a function to send signals to the main process.
onMessage({type: string, data: any}) => void
- A function to handle signals from the subprocess.
type: string
- The type of signal received.
data: any
- The data received.
eventManager: EventEmitter
- The event manager for the main process.
sendToMain: ({type: string, data: any}) => void
- A function to send a signal to the main process.