8000 disable pendingQueries optimizations when usePendingFind or usePendingFindAll are false by eldridge · Pull Request #492 · js-data/js-data · GitHub
[go: up one dir, main page]

Skip to content

disable pendingQueries optimizations when usePendingFind or usePendingFindAll are false #492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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