Skip to content

Commit

Permalink
Simplified parameter
Browse files Browse the repository at this point in the history
We can always use an object again if additional parameters become
necessary.
  • Loading branch information
Jeroen Cranendonk committed Jan 23, 2020
1 parent 695ffea commit c540f75
Show file tree
Hide file tree
Showing 10 changed files with 318 additions and 337 deletions.
11 changes: 3 additions & 8 deletions lib/request/GetRequestV2.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ var $error = require("./../types/error");
var emptyArray = [];
var InvalidSourceError = require("./../errors/InvalidSourceError");

/**
* @typedef DataSourceRequestOptions
* @property {number} retryCount The retry count for this get request based on the request cycle.
*/

/**
* Creates a new GetRequest. This GetRequest takes a scheduler and
* the request queue. Once the scheduler fires, all batched requests
Expand All @@ -24,17 +19,17 @@ var InvalidSourceError = require("./../errors/InvalidSourceError");
*
* @param {Scheduler} scheduler -
* @param {RequestQueueV2} requestQueue -
* @param {DataSourceRequestOptions} dsRequestOpts
* @param {number} attemptCount
*/
var GetRequestV2 = function(scheduler, requestQueue, dsRequestOpts) {
var GetRequestV2 = function(scheduler, requestQueue, attemptCount) {
this.sent = false;
this.scheduled = false;
this.requestQueue = requestQueue;
this.id = ++REQUEST_ID;
this.type = GetRequestType;

this._scheduler = scheduler;
this._dataSourceRequestOptions = dsRequestOpts;
this._attemptCount = attemptCount;
this._pathMap = {};
this._optimizedPaths = [];
this._requestedPaths = [];
Expand Down
25 changes: 10 additions & 15 deletions lib/request/RequestQueueV2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ var sendSetRequest = require("./sendSetRequest");
var GetRequest = require("./GetRequestV2");
var falcorPathUtils = require("falcor-path-utils");

/**
* @typedef DataSourceRequestOptions
* @property {number} retryCount The retry count for this request based on the get/set request cycle.
*/

/**
* The request queue is responsible for queuing the operations to
* the model"s dataSource.
Expand Down Expand Up @@ -35,31 +30,31 @@ RequestQueueV2.prototype = {
* is required the setRequest action is simplified significantly.
*
* @param {JSONGraphEnvelope} jsonGraph -
* @param {DataSourceRequestOptions} dsRequestOpts
* @param {number} attemptCount
* @param {Function} cb
*/
set: function(jsonGraph, dsRequestOpts, cb) {
set: function(jsonGraph, attemptCount, cb) {
if (this.model._enablePathCollapse) {
jsonGraph.paths = falcorPathUtils.collapse(jsonGraph.paths);
}

if (cb === undefined) {
cb = dsRequestOpts;
dsRequestOpts = undefined;
cb = attemptCount;
attemptCount = undefined;
}

return sendSetRequest(jsonGraph, this.model, dsRequestOpts, cb);
return sendSetRequest(jsonGraph, this.model, attemptCount, cb);
},

/**
* Creates a get request to the dataSource. Depending on the current
* scheduler is how the getRequest will be flushed.
* @param {Array} requestedPaths -
* @param {Array} optimizedPaths -
* @param {DataSourceRequestOptions} dsRequestOpts
* @param {number} attemptCount
* @param {Function} cb -
*/
get: function(requestedPaths, optimizedPaths, dsRequestOpts, cb) {
get: function(requestedPaths, optimizedPaths, attemptCount, cb) {
var self = this;
var disposables = [];
var count = 0;
Expand All @@ -71,8 +66,8 @@ RequestQueueV2.prototype = {
var request;

if (cb === undefined) {
cb = dsRequestOpts;
dsRequestOpts = undefined;
cb = attemptCount;
attemptCount = undefined;
}

for (i = 0, len = requests.length; i < len; ++i) {
Expand Down Expand Up @@ -117,7 +112,7 @@ RequestQueueV2.prototype = {
// After going through all the available requests if there are more
// paths to process then a new request must be made.
if (oRemainingPaths && oRemainingPaths.length) {
request = new GetRequest(self.scheduler, self, dsRequestOpts);
request = new GetRequest(self.scheduler, self, attemptCount);
requests[requests.length] = request;
++count;
var disposable = request.batch(rRemainingPaths, oRemainingPaths, refCountCallback);
Expand Down
2 changes: 1 addition & 1 deletion lib/request/flushGetRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ module.exports = function flushGetRequest(request, pathSetArrayBatch, callback)
// we cancel at the callback above.
var getRequest;
try {
getRequest = model._source.get(requestPaths, request._dataSourceRequestOptions);
getRequest = model._source.get(requestPaths, request._attemptCount);
} catch (e) {
callback(new InvalidSourceError());
return null;
Expand Down
11 changes: 3 additions & 8 deletions lib/request/sendSetRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,17 @@ var InvalidSourceError = require("./../errors/InvalidSourceError");
var emptyArray = [];
var emptyDisposable = {dispose: function() {}};

/**
* @typedef DataSourceRequestOptions
* @property {number} retryCount The retry count for this set request based on the request cycle.
*/

/**
* 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 {DataSourceRequestOptions} dsRequestOpts
* @param {number} attemptCount
* @param {Function} callback -
*/
var sendSetRequest = function(originalJsonGraph, model, dsRequestOpts, callback) {
var sendSetRequest = function(originalJsonGraph, model, attemptCount, callback) {
var paths = originalJsonGraph.paths;
var modelRoot = model._root;
var errorSelector = modelRoot.errorSelector;
Expand All @@ -34,7 +29,7 @@ var sendSetRequest = function(originalJsonGraph, model, dsRequestOpts, callback)
var setObservable;
try {
setObservable = model._source.
set(originalJsonGraph, dsRequestOpts);
set(originalJsonGraph, attemptCount);
} catch (e) {
callback(new InvalidSourceError());
return emptyDisposable;
Expand Down
2 changes: 1 addition & 1 deletion lib/response/get/getRequestCycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module.exports = function getRequestCycle(getResponse, model, results, observer,
}

var currentRequestDisposable = requestQueue.
get(boundRequestedMissingPaths, optimizedMissingPaths, { retryCount: count }, function(err, data, hasInvalidatedResult) {
get(boundRequestedMissingPaths, optimizedMissingPaths, count, function(err, data, hasInvalidatedResult) {
if (model._treatDataSourceErrorsAsJSONGraphErrors ? err instanceof InvalidSourceError : !!err) {
if (results.hasValues) {
observer.onNext(results.values && results.values[0]);
Expand Down
2 changes: 1 addition & 1 deletion lib/response/set/setRequestCycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module.exports = function setRequestCycle(model, observer, groups,
// If disposed before this point then the sendSetRequest will not
// further any callbacks. Therefore, if we are at this spot, we are
// not disposed yet.
set(currentJSONGraph, { retryCount: count }, function(error, jsonGraphEnv) {
set(currentJSONGraph, count, function(error, jsonGraphEnv) {
if (error instanceof InvalidSourceError) {
observer.onError(error);
return;
Expand Down
63 changes: 30 additions & 33 deletions test/data/LocalDataSource.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
var Rx = require("rx");
var Observable = Rx.Observable;
var falcor = require("./../../lib/");
var _ = require("lodash");
var noOp = function(a, b, c) { return c; };
const Rx = require("rx");
const falcor = require("./../../lib/");
const _ = require("lodash");
const noOp = function(a, b, c) { return c; };

var LocalSource = module.exports = function(cache, options) {
const LocalSource = module.exports = function(cache, options) {
this._options = _.extend({
miss: 0,
onGet: noOp,
Expand All @@ -14,33 +13,32 @@ var LocalSource = module.exports = function(cache, options) {
materialize: false
}, options);
this._missCount = 0;
this.model = new falcor.Model({cache: cache});
this.model = new falcor.Model({cache});

if (this._options.materialize) {
this.model = this.model._materialize();
}
};

LocalSource.prototype = {
setModel: function(modelOrCache) {
setModel(modelOrCache) {
if (modelOrCache instanceof falcor.Model) {
this.model = modelOrCache;
} else {
this.model = new falcor.Model({cache: modelOrCache});
}
},
get: function(paths, dsRequestOpts) {
var self = this;
var options = this._options;
var miss = options.miss;
var onGet = options.onGet;
var onResults = options.onResults;
var wait = options.wait;
var errorSelector = options.errorSelector;
return Rx.Observable.create(function(observer) {
get(paths, dsRequestOpts) {
const self = this;
const options = this._options;
const miss = options.miss;
const onGet = options.onGet;
const onResults = options.onResults;
const wait = options.wait;
const errorSelector = options.errorSelector;
return Rx.Observable.create(observer => {
function exec() {
var results;
var values = [{}];
const values = [{}];
if (self._missCount >= miss) {
onGet(self, paths, dsRequestOpts);
self.model._getPathValuesAsJSONG(self.model, paths, values, errorSelector);
Expand All @@ -49,7 +47,7 @@ LocalSource.prototype = {
}

// always output all the paths
var output = {
const output = {
// paths: paths,
jsonGraph: {}
};
Expand All @@ -68,20 +66,19 @@ LocalSource.prototype = {
}
});
},
set: function(jsongEnv, dsRequestOpts) {
var self = this;
var options = this._options;
var miss = options.miss;
var onSet = options.onSet;
var onResults = options.onResults;
var wait = options.wait;
var errorSelector = options.errorSelector;
return Rx.Observable.create(function(observer) {
set(jsongEnv, dsRequestOpts) {
const self = this;
const options = this._options;
const onSet = options.onSet;
const onResults = options.onResults;
const wait = options.wait;
const errorSelector = options.errorSelector;
return Rx.Observable.create(observer => {
function exec() {
var seed = [{}];
var tempModel = new falcor.Model({
const seed = [{}];
const tempModel = new falcor.Model({
cache: jsongEnv.jsonGraph,
errorSelector: errorSelector});
errorSelector});
jsongEnv = onSet(self, tempModel, jsongEnv, dsRequestOpts);

tempModel.set(jsongEnv).subscribe();
Expand All @@ -103,7 +100,7 @@ LocalSource.prototype = {
}
});
},
call: function(path, args, suffixes, paths) {
call(path, args, suffixes, paths) {
return Rx.Observable.empty();
}
};
Expand Down
Loading

0 comments on commit c540f75

Please sign in to comment.