diff --git a/lib/gcli/connectors/remoted.js b/lib/gcli/connectors/remoted.js index f1b23df0..d36afbde 100644 --- a/lib/gcli/connectors/remoted.js +++ b/lib/gcli/connectors/remoted.js @@ -189,44 +189,35 @@ Remoter.prototype.exposed = { /** * Perform a lookup on a selection type to get the allowed values */ - getSelectionInfo: method(function(commandName, paramName, action) { - var command = this.requisition.system.commands.get(commandName); - if (command == null) { - throw new Error('No command called \'' + commandName + '\''); - } - - var type; - command.params.forEach(function(param) { - if (param.name === paramName) { - type = param.type; - } - }); - if (type == null) { - throw new Error('No parameter called \'' + paramName + '\' in \'' + - commandName + '\''); - } + getSelectionLookup: method(function(commandName, paramName) { + var type = getType(this.requisition, commandName, paramName); var context = this.requisition.executionContext; + return type.lookup(context).map(function(info) { + // lookup returns an array of objects with name/value properties and + // the values might not be JSONable, so remove them + return { name: info.name }; + }); + }, { + request: { + commandName: Arg(0, "string"), // The command containing the parameter in question + paramName: Arg(1, "string"), // The name of the parameter + }, + response: RetVal("json") + }), - switch (action) { - case 'lookup': - return type.lookup(context).map(function(info) { - // lookup returns an array of objects with name/value properties and - // the values might not be JSONable, so remove them - return { name: info.name }; - }); - - case 'data': - return type.data(context); + /** + * Perform a lookup on a selection type to get the allowed values + */ + getSelectionData: method(function(commandName, paramName) { + var type = getType(this.requisition, commandName, paramName); - default: - throw new Error('Action must be either \'lookup\' or \'data\''); - } + var context = this.requisition.executionContext; + return type.data(context); }, { request: { commandName: Arg(0, "string"), // The command containing the parameter in question - paramName: Arg(1, "string"), // The name of the parameter - action: Arg(2, "string") // 'lookup' or 'data' depending on the function to call + paramName: Arg(1, "string"), // The name of the parameter }, response: RetVal("json") }), @@ -282,6 +273,32 @@ Remoter.prototype.exposed = { }) }; +/** + * Helper for #getSelectionLookup and #getSelectionData that finds a type + * instance given a commandName and paramName + */ +function getType(requisition, commandName, paramName) { + var command = requisition.system.commands.get(commandName); + if (command == null) { + throw new Error('No command called \'' + commandName + '\''); + } + + var type; + command.params.forEach(function(param) { + if (param.name === paramName) { + type = param.type; + } + }); + + if (type == null) { + throw new Error('No parameter called \'' + paramName + '\' in \'' + + commandName + '\''); + } + + return type; +} + + /** * Asynchronous construction. Use GcliFront(); @@ -366,6 +383,22 @@ GcliFront.prototype.decrementType = function(typed, param) { return this.connection.call('decrementType', data); }; +GcliFront.prototype.getSelectionLookup = function(commandName, paramName) { + var data = { + commandName: commandName, + paramName: paramName + }; + return this.connection.call('getSelectionLookup', data); +}; + +GcliFront.prototype.getSelectionData = function(commandName, paramName) { + var data = { + commandName: commandName, + paramName: paramName + }; + return this.connection.call('getSelectionData', data); +}; + GcliFront.prototype.system = function(cmd, args, cwd, env) { var data = { cmd: cmd, diff --git a/lib/gcli/types/selection.js b/lib/gcli/types/selection.js index 4cd2c92f..1ab4fea7 100644 --- a/lib/gcli/types/selection.js +++ b/lib/gcli/types/selection.js @@ -126,11 +126,11 @@ SelectionType.prototype.getLookup = function(context) { var reply; if (this.remoteLookup) { - reply = this.front.getSelectionInfo('lookup', this.commandName, this.paramName); + reply = this.front.getSelectionLookup(this.commandName, this.paramName); reply = resolve(reply, context); } else if (this.remoteData) { - reply = this.front.getSelectionInfo('data', this.commandName, this.paramName); + reply = this.front.getSelectionData(this.commandName, this.paramName); reply = resolve(reply, context).then(this._dataToLookup); } else if (typeof this.lookup === 'function') {