Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Commit

Permalink
runat-1128988: Split getSelectionInfo into getSelection[Lookup|Data]
Browse files Browse the repository at this point in the history
2 separate methods make more sense than just one, and we're better off
fixing this before it is a 'published' interface in Firefox.

Signed-off-by: Joe Walker <jwalker@mozilla.com>
  • Loading branch information
joewalker committed Feb 12, 2015
1 parent ce979a9 commit c7d092b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 33 deletions.
95 changes: 64 additions & 31 deletions lib/gcli/connectors/remoted.js
Expand Up @@ -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")
}),
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions lib/gcli/types/selection.js
Expand Up @@ -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') {
Expand Down

0 comments on commit c7d092b

Please sign in to comment.