Skip to content

Commit

Permalink
[bugfix beta] invalidate link promise on inverse unload (#5377)
Browse files Browse the repository at this point in the history
* unload should invalidate async hasMany

* observing a hasMany and reading it immediately triggers an error

* [bugfix beta] Invalidate link on inverse unload

Invalidates link promises when inverse records are unloaded.  Subsequent
fetches of a `hasMany` unloaded in this way will load from the link
again, rather than loading whatever ids were returned from the prior
link load.

[fix #5354]
  • Loading branch information
hjdivad authored and igorT committed Mar 14, 2018
1 parent c609ef1 commit 872f015
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions addon/-private/system/relationships/state/relationship.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export default class Relationship {
}

inverseDidDematerialize(inverseInternalModel) {
this.linkPromise = null;
if (!this.isAsync) {
// unloading inverse of a sync relationship is treated as a client-side
// delete, so actually remove the models don't merely invalidate the cp
Expand Down
81 changes: 81 additions & 0 deletions tests/integration/records/unload-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2008,3 +2008,84 @@ test('1 sync : many async unload sync side', function(assert) {
})
);
});

test('unload invalidates link promises', function(assert) {
let isUnloaded = false;
env.adapter.coalesceFindRequests = false;

env.adapter.findRecord = (/* store, type, id */) => {
assert.notOk('Records only expected to be loaded via link');
};

env.adapter.findHasMany = (store, snapshot, link) => {
assert.equal(snapshot.modelName, 'person', 'findHasMany(_, snapshot) is correct');
assert.equal(link, 'boats', 'findHasMany(_, _, link) is correct');

let relationships = {
person: {
data: {
type: 'person',
id: 1
}
}
};

let data = [
{
id: 3,
type: 'boat',
relationships
}
];

if (!isUnloaded) {
data.unshift({
id: 2,
type: 'boat',
relationships
});
}

return {
data
};
};

let person = run(() =>
env.store.push({
data: {
id: 1,
type: 'person',
relationships: {
boats: {
links: { related: 'boats' }
}
}
}
})
);
let boats, boat2, boat3;

return run(() =>
person.get('boats').then((asyncRecords) => {
boats = asyncRecords;
[boat2, boat3] = boats.toArray();
}).then(() => {
assert.deepEqual(person.hasMany('boats').ids(), ['2', '3'], 'initially relationship established rhs');
assert.equal(boat2.belongsTo('person').id(), '1', 'initially relationship established rhs');
assert.equal(boat3.belongsTo('person').id(), '1', 'initially relationship established rhs');

isUnloaded = true;
run(() => {
boat2.unloadRecord();
person.get('boats');
});

assert.deepEqual(boats.mapBy('id'), ['3'], 'unloaded boat is removed from ManyArray');
}).then(() => {
return run(() => person.get('boats'));
}).then(newBoats => {
assert.equal(newBoats.length, 1, 'new ManyArray has only 1 boat after unload');
})
);
});

0 comments on commit 872f015

Please sign in to comment.