lib/Master.js
/**
   * Broadcasts a message to all slaves, or a group. Returns an object for syntatic sugar.
   * @param {String} command The command to send to the slave.
   * @param {*} data The data to send with this command.
   * @param {Object} meta The metadata to send with this broadcast.
   * @return {Promise} A promise for completion.
   */
Broadcasts a message to all slaves, or a group. Returns an object for syntatic sugar.
@param {String} command The command to send to the slave.
@param {*} data The data to send with this command.
@param {Object} meta The metadata to send with this broadcast.
@return {Promise} A promise for completion.
Broadcasts a message to all slaves or a group . Returns an object for syntatic sugar . @param { String } command The command to send to the slave . @param { * } data The data to send with this command . @param { Object } meta The metadata to send with this broadcast . @return { Promise } A promise for completion .
broadcast(command, data, meta) {
    const bcast = {
      to: (...slaveList) => new Promise((resolve, reject) => {
        const done = slaveList._.getAndStripCallback();
        if (typeof command === 'string' || typeof command === 'symbol' || (command && command._.isNumeric())) {
          if (typeof command === 'number') command = command.toString();
          const slaves = flattenSlaveList(true, ...slaveList);
          const totalTasks = slaves.length;
          const singleResponse = slaves.length === 1;
          const responses = new ResponseArray();
          let broken = false;
          let completedTasks = 0;
          const onResponse = res => {
            if (!broken) {
              if (res instanceof Error) {
                broken = true;
                done.call(this, res, null);
                reject(res);
                return;
              }
              responses.push(res);
              if (++completedTasks === totalTasks) {
                responses.sort(
                  (a, b) => (a.from < b.from ? -1 : a.from > b.from ? 1 : 0) 
                );
                if (singleResponse) {
                  done.call(this, null, responses[0]);
                  resolve(responses[0]);
                } else {
                  done.call(this, null, responses);
                  resolve(responses);
                }
              }
            }
          };
          if (slaves.length === 0) {
            done.call(this, null, responses);
            resolve(responses);
          } else {
            slaves._.every(s => {
              s.exec(command, data, meta).then(onResponse).catch(onResponse);
              return true;
            });
          }
        } else {
          const e =
            new TypeError(
              `Master#broadcast expected argument #0 (command) to be a string or number, but got ${typeof command}.`
            );
          reject(e);
          done.call(this, e, null);
        }
      }),
    };
    bcast.to.all = () => this.broadcast(command, data, meta).to(...Slave.getAllSlaves());
    bcast.to.idle = () => this.broadcast(command, data, meta).to(...Slave.getAllIdleSlaves());
    bcast.to.group = (g) => this.broadcast(command, data, meta).to(...Slave.getSlavesInGroup(g));
    return bcast;
  }
broadcast ( command , data , meta ) { const bcast = { to : ( ... slaveList ) => new Promise ( ( resolve , reject ) => { const done = slaveList . _ . getAndStripCallback ( ) ; if ( typeof command === 'string' || typeof command === 'symbol' || ( command && command . _ . isNumeric ( ) ) ) { if ( typeof command === 'number' ) command = command . toString ( ) ; const slaves = flattenSlaveList ( true , ... slaveList ) ; const totalTasks = slaves . length ; const singleResponse = slaves . length === 1 ; const responses = new ResponseArray ( ) ; let broken = false ; let completedTasks = 0 ; const onResponse = res => { if ( ! broken ) { if ( res instanceof Error ) { broken = true ; done . call ( this , res , null ) ; reject ( res ) ; return ; } responses . push ( res ) ; if ( ++ completedTasks === totalTasks ) { responses . sort ( ( a , b ) => ( a . from < b . from ? - 1 : a . from > b . from ? 1 : 0 ) ) ; if ( singleResponse ) { done . call ( this , null , responses [ 0 ] ) ; resolve ( responses [ 0 ] ) ; } else { done . call ( this , null , responses ) ; resolve ( responses ) ; } } } } ; if ( slaves . length === 0 ) { done . call ( this , null , responses ) ; resolve ( responses ) ; } else { slaves . _ . every ( s => { s . exec ( command , data , meta ) . then ( onResponse ) . catch ( onResponse ) ; return true ; } ) ; } } else { const e = new TypeError ( ` ${ typeof command } ` ) ; reject ( e ) ; done . call ( this , e , null ) ; } } ) , } ; bcast . to . all = ( ) => this . broadcast ( command , data , meta ) . to ( ... Slave . getAllSlaves ( ) ) ; bcast . to . idle = ( ) => this . broadcast ( command , data , meta ) . to ( ... Slave . getAllIdleSlaves ( ) ) ; bcast . to . group = ( g ) => this . broadcast ( command , data , meta ) . to ( ... Slave . getSlavesInGroup ( g ) ) ; return bcast ; }
Broadcasts a message to all slaves, or a group.
Broadcasts a message to all slaves or a group .
/**
           * When a response is received from one of the slaves.
           * @param {Response} res The response object from the slave.
           * @returns {undefined}
           * @function
           */ // eslint-disable-line no-nested-ternary // No slaves to send the message to, resolve with empty response array. // Got invalid command. // Attach broadcast to all function // Attach broadcast to idle function // Attach broadcast to group function