falcor
Version:
A JavaScript library for efficient data fetching.
70 lines (58 loc) • 2 kB
JavaScript
var isArray = Array.isArray;
var ModelResponse = require("./ModelResponse");
var isPathValue = require("./../support/isPathValue");
var isJSONEnvelope = require("./../support/isJSONEnvelope");
var empty = {dispose: function() {}};
function InvalidateResponse(model, args) {
// TODO: This should be removed. There should only be 1 type of arguments
// coming in, but we have strayed from documentation.
this._model = model;
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)) {
argType = "PathValues";
} else if (isPathValue(arg)) {
argType = "PathValues";
} else if (isJSONEnvelope(arg)) {
argType = "PathMaps";
} else {
throw new Error("Invalid Input");
}
if (groupType !== argType) {
groupType = argType;
group = {
inputType: argType,
arguments: []
};
groups.push(group);
}
group.arguments.push(arg);
}
this._groups = groups;
}
InvalidateResponse.prototype = Object.create(ModelResponse.prototype);
InvalidateResponse.prototype.progressively = function progressively() {
return this;
};
InvalidateResponse.prototype._toJSONG = function _toJSONG() {
return this;
};
InvalidateResponse.prototype._subscribe = function _subscribe(observer) {
var model = this._model;
this._groups.forEach(function(group) {
var inputType = group.inputType;
var methodArgs = group.arguments;
var operationName = "_invalidate" + inputType;
var operationFunc = model[operationName];
operationFunc(model, methodArgs);
});
observer.onCompleted();
return empty;
};
module.exports = InvalidateResponse;