client/lib/utils.js
function handleArguments(args) {
  return new Promise(function(resolve, reject) {
    var requires = [];
    var promises = [];

    // check for Promises in args
    if (args && args instanceof Array) {
      args.some(function (arg, i) {
        if ((arg.value === null || typeof(arg.value) == 'undefined') && arg.optional) {
          return true;
        }

        if (arg.value && Array.isArray(arg.value) && arg.optional && arg.value.length == 0) {
          return true;
        }

        if (arg.type == 'string') {
          var s = '"' + arg.value.replace(/"/g, '\\\"') + '"';
          promises.push(Promise.resolve(s));
        } else if (arg.type == 'lambda') {
          promises.push(serializeLambda(arg.value));
        } else if (arg.type == 'lambdaArgs') {
          promises.push(new Promise(function(resolve, reject) {
            handleArrayArgument(arg.value).then(function (result) {
              resolve(result);
            }).catch(reject);
          }));
        } else if (arg.type == 'map') {
          promises.push(Promise.resolve(JSON.stringify(arg.value)));
        } else if (arg.type == 'promise') {
          promises.push(new Promise(function(resolve, reject) {
            arg.value.then(function(val) {
              if (typeof(val) == 'string') {
                if (isNaN(val)) {
                  // a string
                  var s = '"' + val.replace(/"/g, '\\\"') + '"';
                  resolve(s);
                } else {
                  resolve(parseFloat(val));
                }
              } else {
                resolve(val);
              }
            });
          }));
        } else if (arg.value.refIdP) {
          promises.push(arg.value.refIdP);
        } else if (arg.type == 'List') {
          promises.push(Promise.resolve(arg.value.toString()));
        } else if (arg.type == 'array') {
          // simple array argument
          promises.push(handleArrayArgument(arg.value));
        } else if (arg.type == '_eclairSerialize') {
          if (arg.value._eclairSerialize == 'staticProperty') {
            var val = arg.value.ref.name + '.' + arg.value.value;
            promises.push(Promise.resolve(val));
            requires.push({name: arg.value.ref.name, moduleLocation: arg.value.ref.moduleLocation});
          }
        } else if (Array.isArray(arg.value)) {
          // TODO: should we try to wrap the array if it isn't?
          promises.push(new Promise(function (resolve, reject) {
            handleArguments(arg.value).then(function (result) {
              if (result.requires.length > 0) {
                result.requires.forEach(function (r) {
                  requires.push(r)
                });
              }

              resolve('[' + result.args + ']');
            }).catch(reject);
          }));
        } else if (arg.type == '_eclairForceFloat') {
          var val = arg.value.value;
          if (val.toString().indexOf('.') === -1) {
            val = val.toFixed(1);
          }
          promises.push(Promise.resolve(val));
        } else if (arg.type == '_eclairLocal') {
          promises.push(Promise.resolve(arg.value._generateRemote().refIdP));
        } else if (arg.type == 'sparkclassref') {
          // we have a spark class reference, so return its name
          promises.push(Promise.resolve(arg.value.name));
          // add the class to the requires list
          requires.push({name: arg.value.name, moduleLocation: arg.value.moduleLocation});
        } else {
          promises.push(Promise.resolve(arg.value));
        }

        return false;
      });
    }

    Promise.all(promises).then(function(finalArgs) {
      resolve({args: finalArgs, requires: requires});
      //resolve(finalArgs);
    }).catch(function(e) {
      console.log(e);
      reject(e)
    });
  });
}
function handleArguments(args) {
  return new Promise(function(resolve, reject) {
    var requires = [];
    var promises = [];

    // check for Promises in args
    if (args && args instanceof Array) {
      args.some(function (arg, i) {
        if ((arg.value === null || typeof(arg.value) == 'undefined') && arg.optional) {
          return true;
        }

        if (arg.value && Array.isArray(arg.value) && arg.optional && arg.value.length == 0) {
          return true;
        }

        if (arg.type == 'string') {
          var s = '"' + arg.value.replace(/"/g, '\\\"') + '"';
          promises.push(Promise.resolve(s));
        } else if (arg.type == 'lambda') {
          promises.push(serializeLambda(arg.value));
        } else if (arg.type == 'lambdaArgs') {
          promises.push(new Promise(function(resolve, reject) {
            handleArrayArgument(arg.value).then(function (result) {
              resolve(result);
            }).catch(reject);
          }));
        } else if (arg.type == 'map') {
          promises.push(Promise.resolve(JSON.stringify(arg.value)));
        } else if (arg.type == 'promise') {
          promises.push(new Promise(function(resolve, reject) {
            arg.value.then(function(val) {
              if (typeof(val) == 'string') {
                if (isNaN(val)) {
                  // a string
                  var s = '"' + val.replace(/"/g, '\\\"') + '"';
                  resolve(s);
                } else {
                  resolve(parseFloat(val));
                }
              } else {
                resolve(val);
              }
            });
          }));
        } else if (arg.value.refIdP) {
          promises.push(arg.value.refIdP);
        } else if (arg.type == 'List') {
          promises.push(Promise.resolve(arg.value.toString()));
        } else if (arg.type == 'array') {
          // simple array argument
          promises.push(handleArrayArgument(arg.value));
        } else if (arg.type == '_eclairSerialize') {
          if (arg.value._eclairSerialize == 'staticProperty') {
            var val = arg.value.ref.name + '.' + arg.value.value;
            promises.push(Promise.resolve(val));
            requires.push({name: arg.value.ref.name, moduleLocation: arg.value.ref.moduleLocation});
          }
        } else if (Array.isArray(arg.value)) {
          // TODO: should we try to wrap the array if it isn't?
          promises.push(new Promise(function (resolve, reject) {
            handleArguments(arg.value).then(function (result) {
              if (result.requires.length > 0) {
                result.requires.forEach(function (r) {
                  requires.push(r)
                });
              }

              resolve('[' + result.args + ']');
            }).catch(reject);
          }));
        } else if (arg.type == '_eclairForceFloat') {
          var val = arg.value.value;
          if (val.toString().indexOf('.') === -1) {
            val = val.toFixed(1);
          }
          promises.push(Promise.resolve(val));
        } else if (arg.type == '_eclairLocal') {
          promises.push(Promise.resolve(arg.value._generateRemote().refIdP));
        } else if (arg.type == 'sparkclassref') {
          // we have a spark class reference, so return its name
          promises.push(Promise.resolve(arg.value.name));
          // add the class to the requires list
          requires.push({name: arg.value.name, moduleLocation: arg.value.moduleLocation});
        } else {
          promises.push(Promise.resolve(arg.value));
        }

        return false;
      });
    }

    Promise.all(promises).then(function(finalArgs) {
      resolve({args: finalArgs, requires: requires});
      //resolve(finalArgs);
    }).catch(function(e) {
      console.log(e);
      reject(e)
    });
  });
}
function handleArguments ( args ) { return new Promise ( function ( resolve , reject ) { var requires = [ ] ; var promises = [ ] ; // check for Promises in args if ( args && args instanceof Array ) { args . some ( function ( arg , i ) { if ( ( arg . value === null || typeof ( arg . value ) == 'undefined' ) && arg . optional ) { return true ; } if ( arg . value && Array . isArray ( arg . value ) && arg . optional && arg . value . length == 0 ) { return true ; } if ( arg . type == 'string' ) { var s = '"' + arg . value . replace ( / " / g , '\\\"' ) + '"' ; promises . push ( Promise . resolve ( s ) ) ; } else if ( arg . type == 'lambda' ) { promises . push ( serializeLambda ( arg . value ) ) ; } else if ( arg . type == 'lambdaArgs' ) { promises . push ( new Promise ( function ( resolve , reject ) { handleArrayArgument ( arg . value ) . then ( function ( result ) { resolve ( result ) ; } ) . catch ( reject ) ; } ) ) ; } else if ( arg . type == 'map' ) { promises . push ( Promise . resolve ( JSON . stringify ( arg . value ) ) ) ; } else if ( arg . type == 'promise' ) { promises . push ( new Promise ( function ( resolve , reject ) { arg . value . then ( function ( val ) { if ( typeof ( val ) == 'string' ) { if ( isNaN ( val ) ) { // a string var s = '"' + val . replace ( / " / g , '\\\"' ) + '"' ; resolve ( s ) ; } else { resolve ( parseFloat ( val ) ) ; } } else { resolve ( val ) ; } } ) ; } ) ) ; } else if ( arg . value . refIdP ) { promises . push ( arg . value . refIdP ) ; } else if ( arg . type == 'List' ) { promises . push ( Promise . resolve ( arg . value . toString ( ) ) ) ; } else if ( arg . type == 'array' ) { // simple array argument promises . push ( handleArrayArgument ( arg . value ) ) ; } else if ( arg . type == '_eclairSerialize' ) { if ( arg . value . _eclairSerialize == 'staticProperty' ) { var val = arg . value . ref . name + '.' + arg . value . value ; promises . push ( Promise . resolve ( val ) ) ; requires . push ( { name : arg . value . ref . name , moduleLocation : arg . value . ref . moduleLocation } ) ; } } else if ( Array . isArray ( arg . value ) ) { // TODO: should we try to wrap the array if it isn't? promises . push ( new Promise ( function ( resolve , reject ) { handleArguments ( arg . value ) . then ( function ( result ) { if ( result . requires . length > 0 ) { result . requires . forEach ( function ( r ) { requires . push ( r ) } ) ; } resolve ( '[' + result . args + ']' ) ; } ) . catch ( reject ) ; } ) ) ; } else if ( arg . type == '_eclairForceFloat' ) { var val = arg . value . value ; if ( val . toString ( ) . indexOf ( '.' ) === - 1 ) { val = val . toFixed ( 1 ) ; } promises . push ( Promise . resolve ( val ) ) ; } else if ( arg . type == '_eclairLocal' ) { promises . push ( Promise . resolve ( arg . value . _generateRemote ( ) . refIdP ) ) ; } else if ( arg . type == 'sparkclassref' ) { // we have a spark class reference, so return its name promises . push ( Promise . resolve ( arg . value . name ) ) ; // add the class to the requires list requires . push ( { name : arg . value . name , moduleLocation : arg . value . moduleLocation } ) ; } else { promises . push ( Promise . resolve ( arg . value ) ) ; } return false ; } ) ; } Promise . all ( promises ) . then ( function ( finalArgs ) { resolve ( { args : finalArgs , requires : requires } ) ; //resolve(finalArgs); } ) . catch ( function ( e ) { console . log ( e ) ; reject ( e ) } ) ; } ) ; }
Returns a promise that resolves once all the arguments have been resolved.
Returns a promise that resolves once all the arguments have been resolved .