Skip to content

Commit

Permalink
Merge pull request #386 from michaelbpaulson/master
Browse files Browse the repository at this point in the history
SetRequest now collapses jsongEnv paths.
  • Loading branch information
ThePrimeagen committed Aug 9, 2015
2 parents b833ba0 + d5b0ee7 commit 0c38886
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 26 deletions.
2 changes: 2 additions & 0 deletions lib/request/RequestQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var prefix = require("./../internal/prefix");
var getType = require("./../support/get-type");
var isObject = require("./../support/is-object");
var arrayClone = require("./../support/array-clone");
var falcorPathUtils = require("falcor-path-utils");

/* eslint-disable no-labels block-scoped-var */
function RequestQueue(model, scheduler) {
Expand Down Expand Up @@ -74,6 +75,7 @@ RequestQueue.prototype.get = function getRequest(paths) {
};

RequestQueue.prototype.set = function setRequest(jsonGraphEnvelope) {
jsonGraphEnvelope.paths = falcorPathUtils.collapse(jsonGraphEnvelope.paths);
return SetRequest.create(this.model, jsonGraphEnvelope);
};

Expand Down
1 change: 1 addition & 0 deletions lib/request/SetRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var arrayMap = require("./../support/array-map");

var setJsonGraphAsJsonDense = require("./../set/set-json-graph-as-json-dense");
var setJsonValuesAsJsonDense = require("./../set/set-json-values-as-json-dense");
var collapse = require("falcor-path-utils").collapse;

var emptyArray = new Array(0);

Expand Down
1 change: 1 addition & 0 deletions test/falcor/set/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
describe('Set', function() {
require('./set.cache-only.spec');
require('./set.dataSource-only.spec');
require('./set.dataSource-and-bind.spec');
require('./set.dataSource-and-cache.spec');
require('./set.cacheAsDataSource-and-cache.spec');
Expand Down
45 changes: 25 additions & 20 deletions test/falcor/set/set.dataSource-and-cache.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var LocalDataSource = require('../../data/LocalDataSource');
var ErrorDataSource = require('../../data/ErrorDataSource');
var $error = require('./../../../lib/types/error');
var expect = require('chai').expect;
var sinon = require('sinon');

describe('DataSource and Cache', function() {
xit('should accept jsongraph without paths from the datasource', function(done) {
Expand Down Expand Up @@ -111,44 +112,48 @@ describe('DataSource and Cache', function() {
});
it('should perform multiple trips to a dataSource.', function(done) {
var count = 0;
var onSet = sinon.spy(function(source, tmp, jsongEnv) {
count++;
if (count === 1) {

// Don't do it this way, it will cause memory leaks.
model._root.cache.genreList[1][1] = undefined;
return {
jsonGraph: jsongEnv.jsonGraph,
paths: [jsongEnv.paths[0]]
};
}
return jsongEnv;
});
var model = new Model({
source: new LocalDataSource(Cache(), {
onSet: function(source, tmp, jsongEnv) {
count++;
if (count === 1) {

// Don't do it this way, it will cause memory leaks.
model._root.cache.genreList[0][1] = undefined;
return {
jsonGraph: jsongEnv.jsonGraph,
paths: [jsongEnv.paths[0]]
};
}
return jsongEnv;
}
onSet: onSet
})
});
var onNext = sinon.spy();
model.
set(
{path: ['genreList', 0, 0, 'summary'], value: 1337},
{path: ['genreList', 0, 1, 'summary'], value: 7331}).
doAction(function(x) {
testRunner.compare({
{path: ['genreList', 1, 1, 'summary'], value: 7331}).
doAction(onNext, noOp, function() {
expect(onSet.calledTwice, 'onSet to be called 2x').to.be.ok;
expect(onNext.calledOnce, 'onNext to be called 1x').to.be.ok;
expect(onNext.getCall(0).args[0]).to.deep.equals({
json: {
genreList: {
0: {
0: {
summary: 1337
},
}
},
1: {
1: {
summary: 7331
}
}
}
}
}, x);
}, noOp, function() {
testRunner.compare(2, count);
});
}).
subscribe(noOp, done, done);
});
Expand Down
56 changes: 56 additions & 0 deletions test/falcor/set/set.dataSource-only.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
var falcor = require("./../../../lib/");
var Model = falcor.Model;
var noOp = function() {};
var expect = require('chai').expect;
var sinon = require('sinon');
var LocalDataSource = require('./../../data/LocalDataSource');
var Cache = require('./../../data/Cache');

describe('DataSource.', function() {
it('should validate args are sent to the dataSource collapsed.', function(done) {
var onSet = sinon.spy(function(source, tmpGraph, jsonGraphFromSet) {
return jsonGraphFromSet;
});
var dataSource = new LocalDataSource(Cache(), {
onSet: onSet
});
var model = new Model({
source: dataSource
});
model.
set({
json: {
videos: {
1234: {
rating: 5
},
444: {
rating: 3
}
}
}
}).
doAction(noOp, noOp, function() {
expect(onSet.calledOnce).to.be.ok;

var cleaned = onSet.getCall(0).args[2];
cleaned.paths[0][1] = cleaned.paths[0][1].concat();
expect(cleaned).to.deep.equals({
jsonGraph: {
videos: {
1234: {
rating: 5
},
444: {
rating: 3
}
}
},
paths: [
['videos', [444, 1234], 'rating']
]
});
}).
subscribe(noOp, done, done);
});
});
9 changes: 3 additions & 6 deletions test/integration/call.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ describe('call', function() {
var onNext = sinon.spy();
model.
call("genreList[0].titles.push", args, ['name']).
doAction(onNext).
doAction(noOp, noOp, function() {
doAction(onNext, noOp, function() {
expect(onNext.calledOnce).to.be.ok;
expect(onNext.getCall(0).args[0]).to.deep.equals({
json: {
Expand All @@ -51,8 +50,7 @@ describe('call', function() {
titles: {
2: {
name: 'House of Cards'
},
push: undefined
}
},
}
}
Expand Down Expand Up @@ -93,8 +91,7 @@ describe('call', function() {
titles: {
2: {
name: undefined
},
push: undefined
}
},
}
}
Expand Down

0 comments on commit 0c38886

Please sign in to comment.