clusterphone.js
function constructMessageApi(namespace, cmd, destName, seq) {
  var api = {},
      valid = true,
      timeout = module.exports.ackTimeout,
      resolve,
      reject;

  var promise = new Promise(function(_resolve, _reject) {
    resolve = _resolve;
    reject = _reject;
  });

  setImmediate(function() {
    valid = false;
  });

  api.within = function(newTimeout) {
    if (!valid) {
      throw new Error("within() / acknowledged() calls are only valid immediately after sending a message.");
    }
    if (resolve.monitored) {
      throw new Error("within() must be called *before* acknowledged()");
    }
    if ("number" !== typeof newTimeout) {
      newTimeout = parseInt(newTimeout, 10);
    }
    if(!newTimeout) {
      throw new Error("Timeout must be a number");
    }
    timeout = newTimeout;
    return api;
  };

  api.acknowledged = function(cb) {
    if (!valid) {
      throw new Error("within() / acknowledged() calls are only valid immediately after sending a message.");
    }
    // This flag indicates that the caller does actually care about the resolution of this acknowledgement.
    resolve.monitored = true;
    return promise.timeout(timeout)
        .catch(Promise.TimeoutError, function() {
          // We retrieve the pending here to ensure it's deleted.
          namespace.getPending(seq);
          throw new Error("Timed out waiting for acknowledgement for message " + cmd + " with seq " + seq + " to " + destName + " in namespace " + namespace.interface.name);
        })
        .nodeify(cb);
  };

  api.ackd = api.acknowledged;

  return [api, resolve, reject];
}
function constructMessageApi(namespace, cmd, destName, seq) {
  var api = {},
      valid = true,
      timeout = module.exports.ackTimeout,
      resolve,
      reject;

  var promise = new Promise(function(_resolve, _reject) {
    resolve = _resolve;
    reject = _reject;
  });

  setImmediate(function() {
    valid = false;
  });

  api.within = function(newTimeout) {
    if (!valid) {
      throw new Error("within() / acknowledged() calls are only valid immediately after sending a message.");
    }
    if (resolve.monitored) {
      throw new Error("within() must be called *before* acknowledged()");
    }
    if ("number" !== typeof newTimeout) {
      newTimeout = parseInt(newTimeout, 10);
    }
    if(!newTimeout) {
      throw new Error("Timeout must be a number");
    }
    timeout = newTimeout;
    return api;
  };

  api.acknowledged = function(cb) {
    if (!valid) {
      throw new Error("within() / acknowledged() calls are only valid immediately after sending a message.");
    }
    // This flag indicates that the caller does actually care about the resolution of this acknowledgement.
    resolve.monitored = true;
    return promise.timeout(timeout)
        .catch(Promise.TimeoutError, function() {
          // We retrieve the pending here to ensure it's deleted.
          namespace.getPending(seq);
          throw new Error("Timed out waiting for acknowledgement for message " + cmd + " with seq " + seq + " to " + destName + " in namespace " + namespace.interface.name);
        })
        .nodeify(cb);
  };

  api.ackd = api.acknowledged;

  return [api, resolve, reject];
}
function constructMessageApi ( namespace , cmd , destName , seq ) { var api = { } , valid = true , timeout = module . exports . ackTimeout , resolve , reject ; var promise = new Promise ( function ( _resolve , _reject ) { resolve = _resolve ; reject = _reject ; } ) ; setImmediate ( function ( ) { valid = false ; } ) ; api . within = function ( newTimeout ) { if ( ! valid ) { throw new Error ( "within() / acknowledged() calls are only valid immediately after sending a message." ) ; } if ( resolve . monitored ) { throw new Error ( "within() must be called *before* acknowledged()" ) ; } if ( "number" !== typeof newTimeout ) { newTimeout = parseInt ( newTimeout , 10 ) ; } if ( ! newTimeout ) { throw new Error ( "Timeout must be a number" ) ; } timeout = newTimeout ; return api ; } ; api . acknowledged = function ( cb ) { if ( ! valid ) { throw new Error ( "within() / acknowledged() calls are only valid immediately after sending a message." ) ; } // This flag indicates that the caller does actually care about the resolution of this acknowledgement. resolve . monitored = true ; return promise . timeout ( timeout ) . catch ( Promise . TimeoutError , function ( ) { // We retrieve the pending here to ensure it's deleted. namespace . getPending ( seq ) ; throw new Error ( "Timed out waiting for acknowledgement for message " + cmd + " with seq " + seq + " to " + destName + " in namespace " + namespace . interface . name ) ; } ) . nodeify ( cb ) ; } ; api . ackd = api . acknowledged ; return [ api , resolve , reject ] ; }
We use this to prevent callers from obtaining the "clusterphone" namespace. Build the object that we return from sendTo calls.
We use this to prevent callers from obtaining the clusterphone namespace . Build the object that we return from sendTo calls .