Skip to content
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

Properly handle single results in LiveQuery #186

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 8 additions & 7 deletions addon/-private/live-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ export default ReadOnlyArrayProxy.extend({
content: computed('_content', {
get() {
if (get(this, '_content') === null) {
let results = this._sourceCache.query(this._query);

let content;
if (isArray(results)) {
content = results.map(r => this._identityMap.lookup(r))
} else if (typeof results === 'object') {
content = Object.keys(results).map(r => this._identityMap.lookup(results[r]))
let result = this._sourceCache.query(this._query);
let content = this._identityMap.lookupQueryResult(this._query, result);
if (content) {
if (!isArray(content)) {
content = [content];
}
} else {
content = [];
}
// eslint-disable-next-line ember/no-side-effects
set(this, '_content', content);
Expand Down
13 changes: 13 additions & 0 deletions tests/integration/cache-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ module('Integration - Cache', function(hooks) {
assert.notOk(planets.includes(jupiter));
});

test('liveQuery - findRelatedRecord updates when matching record is added', async function(assert) {
const callisto = await store.addRecord({ type: 'moon', name: 'Callisto' });
const planets = cache.liveQuery(q => q.findRelatedRecord(callisto, 'planet'));
assert.equal(planets.length, 0);

const jupiter = await store.addRecord({ id: 'jupiter', type: 'planet', name: 'Jupiter' });
callisto.set('planet', jupiter);
await store.requestQueue.process();

assert.equal(planets.length, 1);
assert.ok(planets.includes(jupiter));
});

test('#retrieveAttribute', async function(assert) {
const jupiter = await store.addRecord({type: 'planet', name: 'Jupiter'});
assert.equal(cache.retrieveAttribute(jupiter, 'name'), 'Jupiter');
Expand Down