Skip to content

Get acknowledgements back from messages sent to forked processes

Notifications You must be signed in to change notification settings

laggingreflex/fork-acknowledge

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 

Repository files navigation

fork-acknowledge

Get acknowledgements back from messages sent to forked processes

Install

npm i fork-acknowledge

Usage

  • parent

    const { fork } = require('child_process')
    const fack = require('fork-acknowledge')
    
    const child = fork(...);
    const { send } = fack(child);
    
    const result = await send('message');
            └────────────────────────────────────────┐
  • child

    const fack = require('fork-acknowledge')         
                                                     
    const { on } = fack(process);                    
                                                     
    on(async message => {                            
      return 'result' ───────────────────────────────┘
    });

API

const {send, on} = fack(process)
  • process [process] - process, or a (forked) child_process used to send, or on which to listen for, messages.

  • send [function] - Function to send messages to process

    const promise = send(...args)
    • promise [promise] - Resolves (or rejects) when the corresponding¹ on method on the process returns (or throws).
    • promise.off [function] - Special function attached to the promise, to remove the process.on() event listener created by send, which otherwise is automatically removed upon the promise's resolution/rejection.
    • args [array] - Message arguments to be sent.
  • on [function] - Function to receive messages from process

    const off = on(fn)
    • off [function] - Function to remove the event listener after which fn will no longer be called.
    • fn [function] - Function that gets called when a message is received. It's the return value (or error in case it throws) of this function that's used to resolve or reject the corresponding¹ promise. Correspondence is maintained by sending/receiving a unique id (generated using crypto).

About

Get acknowledgements back from messages sent to forked processes

Resources

Stars

Watchers

Forks

Packages

No packages published