Skip to content

Commit

Permalink
Merge pull request #651 from michaelbpaulson/0.x
Browse files Browse the repository at this point in the history
Expires in getValueSync and getCache with undefined keys.
  • Loading branch information
ThePrimeagen committed Dec 4, 2015
2 parents 4016bd3 + 15ef758 commit d372fc7
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 18 deletions.
8 changes: 7 additions & 1 deletion lib/get/getCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ function _copyCache(node, out, fromKey) {
Object.
keys(node).
filter(function(k) {
return !isInternalKey(k);
// Its not an internal key and the node has a value. In the cache
// there are 3 possibilities for values.
// 1: A branch node.
// 2: A $type-value node.
// 3: undefined
// We will strip out 3
return !isInternalKey(k) && node[k];
}).
forEach(function(key) {
var cacheNext = node[key];
Expand Down
5 changes: 3 additions & 2 deletions lib/get/getValueSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ module.exports = function getValueSync(model, simplePath, noClone) {

type = next.$type;

// Up to the last key we follow references
// Up to the last key we follow references, ensure that they are not
// expired either.
if (depth < len) {
if (type === $ref) {
if (type === $ref && !isExpired(next)) {
ref = followReference(model, root, root, next, next.value);
refNode = ref[0];

Expand Down
22 changes: 22 additions & 0 deletions test/falcor/bind/bind-cases.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ var Bound = Expected.Bound;
var noOp = function() {};
var chai = require("chai");
var expect = chai.expect;
var ref = Model.ref;
var atom = Model.atom;

describe('Deref', function() {
it('should deref to a branch node.', function(done) {
Expand Down Expand Up @@ -70,6 +72,26 @@ describe('Deref', function() {
}).
subscribe(noOp, done, done);
});

it('should not be able to get an item out of the core that is expired, but previously hard-linked.', function() {
// hardlinks
var model = new Model({
cache: {
lolomo: ref(['lolomos', 'abc'], {$expires: Date.now() + 1000}),
lolomos: {
abc: {
0: atom('foo')
}
}
}
});

// expires the hardlinked item.
model._root.cache.lolomo.$expires = Date.now() - 10;
var out = model._getValueSync(model, ['lolomo', 0]);

expect(out.value).to.equals(undefined);
});
});

function getCache() {
Expand Down
1 change: 0 additions & 1 deletion test/falcor/get/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ describe('Get', function() {
require('./get.dataSource-and-bind.spec');
require('./get.cacheAsDataSource.spec');
require('./get.pathSyntax.spec');
require('./get.getCache.spec');
require('./get.clone.spec');
require('./get.gen.spec');
});
1 change: 1 addition & 0 deletions test/get-core/edges.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var ref = jsonGraph.ref;
var $ref = require('./../../lib/types/ref');
var $atom = require('./../../lib/types/atom');
var _ = require('lodash');
var Model = require('./../../lib').Model;

describe('Edges', function() {
// PathMap ----------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
var cacheGenerator = require('./../../CacheGenerator');
var falcor = require("./../../../lib/");
var clean = require('./../../cleanData').clean;
var cacheGenerator = require('./../CacheGenerator');
var falcor = require("./../../lib/");
var clean = require('./../cleanData').clean;
var Model = falcor.Model;
var Rx = require('rx');
var noOp = function() {};
var Observable = Rx.Observable;
var _ = require('lodash');
var expect = require('chai').expect;
var $ref = falcor.Model.ref;
var $atom = falcor.Model.atom;
var $error = falcor.Model.error;
var atom = Model.atom;

describe('getCache', function() {

it("should serialize the cache", function() {
var model = new Model({ cache: cacheGenerator(0, 1) });
var cache = model.getCache();
Expand All @@ -25,5 +20,21 @@ describe('getCache', function() {
clean(cache);
expect(cache).to.deep.equals(cacheGenerator(3, 1));
});

it('should serialize a cache with undefined values.', function() {
var model = new Model({
cache: {
test: 'foo'
}
});

// mimicking cache clean-up
model._root.cache.testing = undefined;
var cache = model.getCache();
clean(cache);
expect(cache).to.deep.equals({
test: atom('foo', {$modelCreated: true})
});
});
});

1 change: 1 addition & 0 deletions test/get-core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ describe('Get Core', function() {
require('./errors.spec');
require('./deref.spec');
require('./edges.spec');
require('./get.cache.spec');
});
14 changes: 10 additions & 4 deletions test/getCoreRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ module.exports = function(testConfig) {
cache = cache();
}
var source = testConfig.source;
var model = new Model({
cache: cache,
source: source
});
var model;
if (testConfig.model) {
model = testConfig.model;
}
else {
model = new Model({
cache: cache,
source: source
});
}

if (testConfig.treatErrorsAsValues) {
model = model.treatErrorsAsValues();
Expand Down

0 comments on commit d372fc7

Please sign in to comment.