UNPKG

falcor

Version:

A JavaScript library for efficient data fetching.

82 lines (68 loc) 2.7 kB
var setJSONGraphs = require("./../set/setJSONGraphs"); var setPathValues = require("./../set/setPathValues"); var InvalidSourceError = require("./../errors/InvalidSourceError"); var emptyArray = []; var emptyDisposable = {dispose: function() {}}; /** * A set request is not an object like GetRequest. It simply only needs to * close over a couple values and its never batched together (at least not now). * * @private * @param {JSONGraphEnvelope} jsonGraph - * @param {Model} model - * @param {number} attemptCount * @param {Function} callback - */ var sendSetRequest = function(originalJsonGraph, model, attemptCount, callback) { var paths = originalJsonGraph.paths; var modelRoot = model._root; var errorSelector = modelRoot.errorSelector; var comparator = modelRoot.comparator; var boundPath = model._path; var resultingJsonGraphEnvelope; // This is analogous to GetRequest _merge / flushGetRequest // SetRequests are just considerably simplier. var setObservable; try { setObservable = model._source. set(originalJsonGraph, attemptCount); } catch (e) { callback(new InvalidSourceError()); return emptyDisposable; } var disposable = setObservable. subscribe(function onNext(jsonGraphEnvelope) { // When disposed, no data is inserted into. This can sync resolve // and if thats the case then its undefined. if (disposable && disposable.disposed) { return; } // onNext will insert all data into the model then save the json // envelope from the incoming result. model._path = emptyArray; var successfulPaths = setJSONGraphs(model, [{ paths: paths, jsonGraph: jsonGraphEnvelope.jsonGraph }], null, errorSelector, comparator); jsonGraphEnvelope.paths = successfulPaths[1]; model._path = boundPath; resultingJsonGraphEnvelope = jsonGraphEnvelope; }, function onError(dataSourceError) { if (disposable && disposable.disposed) { return; } model._path = emptyArray; setPathValues(model, paths.map(function(path) { return { path: path, value: dataSourceError }; }), null, errorSelector, comparator); model._path = boundPath; callback(dataSourceError); }, function onCompleted() { callback(null, resultingJsonGraphEnvelope); }); return disposable; }; module.exports = sendSetRequest;