Skip to content

Commit

Permalink
[bugfix beta] Invalidate link on inverse unload
Browse files Browse the repository at this point in the history
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 #5353]
  • Loading branch information
hjdivad committed Mar 13, 2018
1 parent 3de24bb commit dc1310a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 41 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
48 changes: 7 additions & 41 deletions tests/integration/records/unload-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2009,33 +2009,12 @@ test('1 sync : many async unload sync side', function(assert) {
);
});

test('unload should work with observers on hasMany', function(assert) {
test('unload invalidates link promises', function(assert) {
let isUnloaded = false;
env.adapter.coalesceFindRequests = false;

env.adapter.findRecord = (store, type, id) => {
assert.equal(type, Boat, 'findRecord(_, type) is correct');

if (id === '2' && isUnloaded) {
throw "not found";
}

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

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

env.adapter.findHasMany = (store, snapshot, link) => {
Expand Down Expand Up @@ -2080,27 +2059,13 @@ test('unload should work with observers on hasMany', function(assert) {
relationships: {
boats: {
links: { related: 'boats' }
// data: [{
// id: 2,
// type: 'boat'
// },{
// id: 3,
// type: 'boat'
// },
// ]
}
}
}
})
);
let boats, boat2, boat3;

person.addObserver('boats.[]', function() {
person.get('boats');
//NOTE: using a runloop seems to fix it
// run(() => person.get('boats'));
});

return run(() =>
person.get('boats').then((asyncRecords) => {
boats = asyncRecords;
Expand All @@ -2111,11 +2076,12 @@ test('unload should work with observers on hasMany', function(assert) {
assert.equal(boat3.belongsTo('person').id(), '1', 'initially relationship established rhs');

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

assert.deepEqual(boats.mapBy('id'), ['3'], 'unloaded boat is removed from ManyArray');
//TODO: remove reload if ED fixes invalidate bug
return person.hasMany('boats').reload();
}).then(() => {
return run(() => person.get('boats'));
}).then(newBoats => {
Expand Down

0 comments on commit dc1310a

Please sign in to comment.