_loadFromObject: function(data) { var promises = []; // take the promise action out of the loop to avoid closure problems var setterFactory = function(keyName) { return function(value) { this.set(keyName, value); }; }; for (var key in data) { if (data.hasOwnProperty(key)) { var valueStr = data[key]; var settingExt = catalog.getExtensionByKey('setting', key); if (settingExt) { // TODO: We shouldn't just ignore values without a setting var promise = types.fromString(valueStr, settingExt.type); var setter = setterFactory(key); promise.then(setter); promises.push(promise); } } } // Promise.group (a.k.a groupPromises) gives you a list of all the data // in the grouped promises. We don't want that in case we change how // this works with ignored settings (see above). // So we do this to hide the list of promise resolutions. var replyPromise = new Promise(); groupPromises(promises).then(function() { replyPromise.resolve(); }); return replyPromise; },
_saveToObject: function() { var promises = []; var reply = {}; this._getSettingNames().forEach(function(key) { var value = this.get(key); var settingExt = catalog.getExtensionByKey('setting', key); if (settingExt) { // TODO: We shouldn't just ignore values without a setting var promise = types.toString(value, settingExt.type); promise.then(function(value) { reply[key] = value; }); promises.push(promise); } }.bind(this)); var replyPromise = new Promise(); groupPromises(promises).then(function() { replyPromise.resolve(reply); }); return replyPromise; },
_convertTypes: function() { // Use {} when there are no params if (!this._commandExt.params) { this.argsPromise.resolve({}); return; } // The data we pass to the command var argOutputs = {}; // Cache of promises, because we're only done when they're done var convertPromises = []; this._assignments.forEach(function(assignment) { // HACK! deferred types need to have some parameters // by which to determine which type they should defer to // so we hack in the assignments so the deferrer can work assignment.param.type.assignments = this._assignments; var promise = this._convertType(assignment, argOutputs); promise.then(function(converted) { assignment.converted = converted; argOutputs[assignment.param.name] = converted; }, function(ex) { var message = 'Can\'t convert \'' + assignment.value + '\' to a ' + param.type + ': ' + ex; this.hints.push(new Hint(Level.Error, message)); }.bind(this)); convertPromises.push(promise); }.bind(this)); groupPromises(convertPromises).then(function() { this.argsPromise.resolve(argOutputs); }.bind(this)); },