Skip to content

Commit

Permalink
if usePendingFind or usePendingFindAll is disabled, disable storing p…
Browse files Browse the repository at this point in the history
…ending queries in the corresponding pendingQueries cache (ref #364)
  • Loading branch information
Mike Eldridge committed Feb 2, 2018
1 parent 1978ef8 commit 1a83f93
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 9 deletions.
4 changes: 2 additions & 2 deletions bower.json
Expand Up @@ -28,7 +28,7 @@
],
"devDependencies": {
"observe-js": "0.5.5",
"js-data-http": ">=2.0.0",
"js-data-localstorage": ">=2.0.0"
"js-data-http": "^2.0.0",
"js-data-localstorage": "^2.0.0"
}
}
13 changes: 10 additions & 3 deletions src/datastore/async_methods/find.js
Expand Up @@ -54,8 +54,9 @@ module.exports = function find (resourceName, id, options) {
}
}).then(function (item) {
if (!item) {
let query
const usePendingFind = DSUtils.isFunction(options.usePendingFind) ? options.usePendingFind.call(this, resourceName, id, options) : options.usePendingFind
if (!(id in resource.pendingQueries) && usePendingFind) {
if (!(id in resource.pendingQueries) || !usePendingFind) {
let promise
let strategy = options.findStrategy || options.strategy

Expand All @@ -80,7 +81,7 @@ module.exports = function find (resourceName, id, options) {
promise = _this.adapters[adapter].find(definition, id, options)
}

resource.pendingQueries[id] = promise
query = promise
.then(function (data) { return options.afterFind.call(data, options, data) })
.then(function (data) {
// Query is no longer pending
Expand All @@ -97,8 +98,14 @@ module.exports = function find (resourceName, id, options) {
return definition.createInstance(data, options.orig())
}
})

if (usePendingFind) {
resource.pendingQueries[id] = query
}
} else {
query = resource.pendingQueries[id]
}
return resource.pendingQueries[id]
return query
} else {
// resolve immediately with the item
return item
Expand Down
13 changes: 10 additions & 3 deletions src/datastore/async_methods/findAll.js
Expand Up @@ -97,8 +97,9 @@ module.exports = function findAll (resourceName, params, options) {
}
}).then(function (items) {
if (!items) {
let query
const usePendingFindAll = DSUtils.isFunction(options.usePendingFindAll) ? options.usePendingFindAll.call(this, resourceName, params, options) : options.usePendingFindAll
if (!(queryHash in resource.pendingQueries) && usePendingFindAll) {
if (!(queryHash in resource.pendingQueries) || !usePendingFindAll) {
let promise
let strategy = options.findAllStrategy || options.strategy

Expand All @@ -123,7 +124,7 @@ module.exports = function findAll (resourceName, params, options) {
promise = _this.adapters[adapter].findAll(definition, params, options)
}

resource.pendingQueries[queryHash] = promise
query = promise
.then(function (data) { return options.afterFindAll.call(data, options, data) })
.then(function (data) {
// Query is no longer pending
Expand All @@ -140,9 +141,15 @@ module.exports = function findAll (resourceName, params, options) {
return data
}
})

if (usePendingFindAll) {
resource.pendingQueries[queryHash] = query
}
} else {
query = resource.pendingQueries[queryHash]
}

return resource.pendingQueries[queryHash]
return query
} else {
// resolve immediately with the items
return items
Expand Down
42 changes: 41 additions & 1 deletion test/browser/datastore/async_methods/find.test.js
Expand Up @@ -15,7 +15,7 @@ describe('DS#find', function () {
// Should have no effect because there is already a pending query
return Post.find(5)
.then(function (post) {
assert.deepEqual(JSON.stringify(post), JSON.stringify(p1));
assert.deepEqual(JSON.stringify(post), JSON.stringify(p1), 'Post.find result');
assert.deepEqual(JSON.stringify(Post.get(5)), JSON.stringify(p1), 'The post is now in the datastore');
assert.isNumber(Post.lastModified(5));
assert.isNumber(Post.lastSaved(5));
Expand Down Expand Up @@ -435,4 +435,44 @@ describe('DS#find', function () {
assert.equal(lifecycle.deserialize.callCount, 2, 'deserialize should have been called');
});
});

it('should track pending queries if usePendingFind is enabled', function () {
var _this = this;

Post.ejectAll();

var promise = Post.find(5, { usePendingFind : true });

setTimeout(function () {
assert.equal(1, _this.requests.length, 'has pending request');
assert.equal(_this.requests[0].url, 'http://test.js-data.io/posts/5', 'pending request url');
assert.equal(_this.requests[0].method, 'GET', 'pending request method');
assert.deepEqual(Object.keys(store.store.post.pendingQueries), ['5'], 'pending query list contains hashed query');
_this.requests[0].respond(200, {'Content-Type': 'application/json'}, JSON.stringify(p1));
}, 100);

return promise.then(function (data) {
assert.equal(JSON.stringify(data), JSON.stringify(p1), 'response data');
});
});

it('should not track pending queries if usePendingFind is disabled', function () {
var _this = this;

Post.ejectAll();

var promise = Post.find(5, { usePendingFind : false });

setTimeout(function () {
assert.equal(1, _this.requests.length, 'has pending request');
assert.equal(_this.requests[0].url, 'http://test.js-data.io/posts/5', 'pending request url');
assert.equal(_this.requests[0].method, 'GET', 'pending request method');
assert.deepEqual(Object.keys(store.store.post.pendingQueries), [], 'pending query list is empty');
_this.requests[0].respond(200, {'Content-Type': 'application/json'}, JSON.stringify(p1));
}, 100);

return promise.then(function (data) {
assert.equal(JSON.stringify(data), JSON.stringify(p1), 'response data');
});
});
});
40 changes: 40 additions & 0 deletions test/browser/datastore/async_methods/findAll.test.js
Expand Up @@ -465,4 +465,44 @@ describe('DS#findAll', function () {
assert.equal(p.length, 3);
});
});

it('should track pending queries if usePendingFindAll is enabled', function () {
var _this = this;

Post.ejectAll();

var promise = Post.findAll(null, { usePendingFindAll : true });

setTimeout(function () {
assert.equal(1, _this.requests.length, 'has pending request');
assert.equal(_this.requests[0].url, 'http://test.js-data.io/posts', 'pending request url');
assert.equal(_this.requests[0].method, 'GET', 'pending request method');
assert.deepEqual(Object.keys(store.store.post.pendingQueries), ['{}'], 'pending query list contains hashed query');
_this.requests[0].respond(200, {'Content-Type': 'application/json'}, JSON.stringify([p1, p2, p3, p4]));
}, 100);

return promise.then(function (data) {
assert.equal(JSON.stringify(data), JSON.stringify([p1, p2, p3, p4]));
});
});

it('should not track pending queries if usePendingFindAll is disabled', function () {
var _this = this;

Post.ejectAll();

var promise = Post.findAll(null, { usePendingFindAll : false });

setTimeout(function () {
assert.equal(1, _this.requests.length, 'has pending request');
assert.equal(_this.requests[0].url, 'http://test.js-data.io/posts', 'pending request url');
assert.equal(_this.requests[0].method, 'GET', 'pending request method');
assert.deepEqual(Object.keys(store.store.post.pendingQueries), [], 'pending query list is empty');
_this.requests[0].respond(200, {'Content-Type': 'application/json'}, JSON.stringify([p1, p2, p3, p4]));
}, 100);

return promise.then(function (data) {
assert.equal(JSON.stringify(data), JSON.stringify([p1, p2, p3, p4]));
});
});
});

0 comments on commit 1a83f93

Please sign in to comment.