lib/Master.js
/**
   * Splits data up among slaves for the given command.
   * @param {Object} command The command to bind to this scatter.
   * @param {Object} meta Meta data for this scatter.
   * @return {Object<Function>} Various functions to finish creating this scatter.
   */
Splits data up among slaves for the given command.
@param {Object} command The command to bind to this scatter.
@param {Object} meta Meta data for this scatter.
@return {Object} Various functions to finish creating this scatter.
Splits data up among slaves for the given command . @param { Object } command The command to bind to this scatter . @param { Object } meta Meta data for this scatter . @return { Object } Various functions to finish creating this scatter .
createScatter(command, meta) {
    command = validateCommand(command);
    let dataList = [];
    const tasks = [];
    const gather = (...slaves) => {
      meta = typeof meta === 'object' ? meta : {};
      let completed = 0;
      let done = function EMPTY_CALLBACK() {};
      const responses = new ResponseArray();
      if (slaves[slaves.length - 1] instanceof Function) done = slaves.pop();
      slaves = flattenSlaveList(true, ...slaves);
      return new Promise((resolve, reject) => {
        if (!slaves || slaves.length === 0) {
          const e = Error('Cannot gather without at least one slave!');
          done.call(this, e, responses);
          reject(e);
        }
        if (meta.chunk) {
          const datas = [];
          while (dataList.length > 0) datas.push(dataList.splice(0, slaves.length));
          for (let i = 0; i < datas.length; i++) {
            tasks.push({
              data: datas[i],
              slave: slaves[i % slaves.length],
            });
          }
        } else {
          for (let i = 0; i < dataList.length; i++) {
            tasks.push({
              data: dataList[i],
              slave: slaves[i % slaves.length],
            });
          }
        }
        if (tasks.length === 0) {
          done.call(this, null, responses);
          resolve(responses);
          return;
        }
        let hasRejected = false;
        tasks.forEach(t => {
          this.tellSlave(t.slave).to(command, t.data, meta)
            .then(res => {
              responses.push(res);
              if (++completed === tasks.length) {
                done.call(this, null, responses);
                resolve(responses);
              }
            })
            .catch(e => {
              if (meta.catchAll) {
                if (!hasRejected) {
                  hasRejected = true;
                  done.call(this, e, null);
                  reject(e);
                }
              } else {
                responses.push(e);
                if (++completed === tasks.length) {
                  done.call(this, null, responses);
                  resolve(responses);
                }
              }
            });
        });
      });
    };
    const data = (...d) => {
      dataList = dataList.concat(...d);
      return { data, gather };
    };
    return { data };
  }
createScatter ( command , meta ) { command = validateCommand ( command ) ; let dataList = [ ] ; const tasks = [ ] ; const gather = ( ... slaves ) => { meta = typeof meta === 'object' ? meta : { } ; let completed = 0 ; let done = function EMPTY_CALLBACK ( ) { } ; const responses = new ResponseArray ( ) ; if ( slaves [ slaves . length - 1 ] instanceof Function ) done = slaves . pop ( ) ; slaves = flattenSlaveList ( true , ... slaves ) ; return new Promise ( ( resolve , reject ) => { if ( ! slaves || slaves . length === 0 ) { const e = Error ( 'Cannot gather without at least one slave!' ) ; done . call ( this , e , responses ) ; reject ( e ) ; } if ( meta . chunk ) { const datas = [ ] ; while ( dataList . length > 0 ) datas . push ( dataList . splice ( 0 , slaves . length ) ) ; for ( let i = 0 ; i < datas . length ; i ++ ) { tasks . push ( { data : datas [ i ] , slave : slaves [ i % slaves . length ] , } ) ; } } else { for ( let i = 0 ; i < dataList . length ; i ++ ) { tasks . push ( { data : dataList [ i ] , slave : slaves [ i % slaves . length ] , } ) ; } } if ( tasks . length === 0 ) { done . call ( this , null , responses ) ; resolve ( responses ) ; return ; } let hasRejected = false ; tasks . forEach ( t => { this . tellSlave ( t . slave ) . to ( command , t . data , meta ) . then ( res => { responses . push ( res ) ; if ( ++ completed === tasks . length ) { done . call ( this , null , responses ) ; resolve ( responses ) ; } } ) . catch ( e => { if ( meta . catchAll ) { if ( ! hasRejected ) { hasRejected = true ; done . call ( this , e , null ) ; reject ( e ) ; } } else { responses . push ( e ) ; if ( ++ completed === tasks . length ) { done . call ( this , null , responses ) ; resolve ( responses ) ; } } } ) ; } ) ; } ) ; } ; const data = ( ... d ) => { dataList = dataList . concat ( ... d ) ; return { data , gather } ; } ; return { data } ; }
Splits data up among slaves for the given command.
Splits data up among slaves for the given command .
/**
     * Executes the scatter, and gathers the responses into a response array.
     * @param {...Slaves} slaves The slaves to execute this scatter with.
     * @return {ResponseArray} An array of responses.
     */ // Build out task list... // We have no tasks to scatter, simply resolve... // Execute task list... /**
     * Adds data to the scatter.
     * @param {...*} d The data to add.
     * @return {Object<Function>} An object containing a self reference for chaining, and the gather method.
     */