UNPKG

falcor

Version:

A JavaScript library for efficient data fetching.

54 lines (44 loc) 1.72 kB
var arrayFlatMap = require("./../../support/array-flat-map"); /** * Takes the groups that are created in the SetResponse constructor and sets * them into the cache. */ module.exports = function setGroupsIntoCache(model, groups) { var modelRoot = model._root; var errorSelector = modelRoot.errorSelector; var groupIndex = -1; var groupCount = groups.length; var requestedPaths = []; var optimizedPaths = []; var returnValue = { requestedPaths: requestedPaths, optimizedPaths: optimizedPaths }; // Takes each of the groups and normalizes their input into // requested paths and optimized paths. while (++groupIndex < groupCount) { var group = groups[groupIndex]; var inputType = group.inputType; var methodArgs = group.arguments; if (methodArgs.length > 0) { var operationName = "_set" + inputType; var operationFunc = model[operationName]; var successfulPaths = operationFunc(model, methodArgs, null, errorSelector); optimizedPaths.push.apply(optimizedPaths, successfulPaths[1]); if (inputType === "PathValues") { requestedPaths.push.apply(requestedPaths, methodArgs.map(pluckPath)); } else if (inputType === "JSONGs") { requestedPaths.push.apply(requestedPaths, arrayFlatMap(methodArgs, pluckEnvelopePaths)); } else { requestedPaths.push.apply(requestedPaths, successfulPaths[0]); } } } return returnValue; }; function pluckPath(pathValue) { return pathValue.path; } function pluckEnvelopePaths(jsonGraphEnvelope) { return jsonGraphEnvelope.paths; }