falcor
Version:
A JavaScript library for efficient data fetching.
110 lines (96 loc) • 3.42 kB
JavaScript
var ModelResponse = require("./../ModelResponse");
var pathSyntax = require("falcor-path-syntax");
var isArray = Array.isArray;
var isPathValue = require("./../../support/isPathValue");
var isJSONGraphEnvelope = require("./../../support/isJSONGraphEnvelope");
var isJSONEnvelope = require("./../../support/isJSONEnvelope");
var setRequestCycle = require("./setRequestCycle");
/**
* The set response is responsible for doing the request loop for the set
* operation and subscribing to the follow up get.
*
* The constructors job is to parse out the arguments and put them in their
* groups. The following subscribe will do the actual cache set and dataSource
* operation remoting.
*
* @param {Model} model -
* @param {Array} args - The array of arguments that can be JSONGraph, JSON, or
* pathValues.
* @param {Boolean} isJSONGraph - if the request is a jsonGraph output format.
* @param {Boolean} isProgressive - progressive output.
* @augments ModelResponse
* @private
*/
var SetResponse = function SetResponse(model, args, isJSONGraph,
isProgressive) {
// The response properties.
this._model = model;
this._isJSONGraph = isJSONGraph || false;
this._isProgressive = isProgressive || false;
this._initialArgs = args;
this._value = [{}];
var groups = [];
var group, groupType;
var argIndex = -1;
var argCount = args.length;
// Validation of arguments have been moved out of this function.
while (++argIndex < argCount) {
var arg = args[argIndex];
var argType;
if (isArray(arg) || typeof arg === "string") {
arg = pathSyntax.fromPath(arg);
argType = "PathValues";
} else if (isPathValue(arg)) {
arg.path = pathSyntax.fromPath(arg.path);
argType = "PathValues";
} else if (isJSONGraphEnvelope(arg)) {
argType = "JSONGs";
} else if (isJSONEnvelope(arg)) {
argType = "PathMaps";
}
if (groupType !== argType) {
groupType = argType;
group = {
inputType: argType,
arguments: []
};
groups.push(group);
}
group.arguments.push(arg);
}
this._groups = groups;
};
SetResponse.prototype = Object.create(ModelResponse.prototype);
/**
* The subscribe function will setup the remoting of the operation and cache
* setting.
*
* @private
*/
SetResponse.prototype._subscribe = function _subscribe(observer) {
var groups = this._groups;
var model = this._model;
var isJSONGraph = this._isJSONGraph;
var isProgressive = this._isProgressive;
// Starts the async request cycle.
return setRequestCycle(
model, observer, groups, isJSONGraph, isProgressive, 1);
};
/**
* Makes the output of a get response JSONGraph instead of json.
* @private
*/
SetResponse.prototype._toJSONG = function _toJSONGraph() {
return new SetResponse(this._model, this._initialArgs,
true, this._isProgressive);
};
/**
* Progressively responding to data in the cache instead of once the whole
* operation is complete.
* @public
*/
SetResponse.prototype.progressively = function progressively() {
return new SetResponse(this._model, this._initialArgs,
this._isJSONGraph, true);
};
module.exports = SetResponse;