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

{beta} Beta has many sync #5155

Merged
merged 1 commit into from
Aug 30, 2017
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
31 changes: 13 additions & 18 deletions addon/-private/system/model/internal-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ function areAllModelsUnloaded(internalModels) {
return true;
}

function destroyRelationship(rel) {
if (rel._inverseIsAsync()) {
rel.removeInternalModelFromInverse(rel.inverseInternalModel);
rel.removeInverseRelationships();
} else {
rel.removeCompletelyFromInverse();
}
}
// this (and all heimdall instrumentation) will be stripped by a babel transform
// https://github.com/heimdalljs/babel5-plugin-strip-heimdall
const {
Expand Down Expand Up @@ -432,9 +440,7 @@ export default class InternalModel {
_directlyRelatedInternalModels() {
let array = [];
this._relationships.forEach((name, rel) => {
let local = rel.members.toArray();
let server = rel.canonicalMembers.toArray();
array = array.concat(local, server);
array = array.concat(rel.members.list, rel.canonicalMembers.list);
});
return array;
}
Expand Down Expand Up @@ -486,6 +492,7 @@ export default class InternalModel {
once all models that refer to it via some relationship are also unloaded.
*/
unloadRecord() {
if (this.isDestroyed) { return; }
this.send('unloadRecord');
this.dematerializeRecord();

Expand Down Expand Up @@ -535,7 +542,6 @@ export default class InternalModel {
if (this.isDestroyed) { return; }

this._cleanupOrphanedInternalModels();

}

_cleanupOrphanedInternalModels() {
Expand Down Expand Up @@ -942,26 +948,15 @@ export default class InternalModel {
and destroys any ManyArrays.
*/
destroyRelationships() {
this._relationships.forEach((name, rel) => {
if (rel._inverseIsAsync()) {
rel.removeInternalModelFromInverse(this);
rel.removeInverseRelationships();
} else {
rel.removeCompletelyFromInverse();
}
});
let relationships = this._relationships;
relationships.forEach((name, rel) => destroyRelationship(rel));

let implicitRelationships = this._implicitRelationships;
this.__implicitRelationships = null;
Object.keys(implicitRelationships).forEach((key) => {
let rel = implicitRelationships[key];

if (rel._inverseIsAsync()) {
rel.removeInternalModelFromInverse(this);
rel.removeInverseRelationships();
} else {
rel.removeCompletelyFromInverse();
}
destroyRelationship(rel);

rel.destroy();
});
Expand Down
7 changes: 4 additions & 3 deletions addon/-private/system/relationships/state/relationship.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,13 @@ export default class Relationship {
let allMembers =
// we actually want a union of members and canonicalMembers
// they should be disjoint but currently are not due to a bug
this.members.toArray().concat(this.canonicalMembers.toArray());
this.members.list.concat(this.canonicalMembers.list);

allMembers.forEach(inverseInternalModel => {
for (let i = 0; i < allMembers.length; i++) {
let inverseInternalModel = allMembers[i];
let relationship = inverseInternalModel._relationships.get(this.inverseKey);
relationship.inverseDidDematerialize();
});
}
}

inverseDidDematerialize() {}
Expand Down
23 changes: 11 additions & 12 deletions tests/integration/records/rematerialize-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,10 @@ module("integration/unload - Rematerializing Unloaded Records", {
});

test("a sync belongs to relationship to an unloaded record can restore that record", function(assert) {
let adam, bob;

// disable background reloading so we do not re-create the relationship.
env.adapter.shouldBackgroundReloadRecord = () => false;

run(function() {
let adam = run(() => {
env.store.push({
data: {
type: 'person',
Expand All @@ -79,10 +77,11 @@ test("a sync belongs to relationship to an unloaded record can restore that reco
}
}
});
adam = env.store.peekRecord('person', 1);

return env.store.peekRecord('person', 1);
});

run(function() {
let bob = run(() => {
env.store.push({
data: {
type: 'car',
Expand All @@ -98,7 +97,8 @@ test("a sync belongs to relationship to an unloaded record can restore that reco
}
}
});
bob = env.store.peekRecord('car', 1);

return env.store.peekRecord('car', 1);
});

let person = env.store.peekRecord('person', 1);
Expand All @@ -107,9 +107,7 @@ test("a sync belongs to relationship to an unloaded record can restore that reco
assert.equal(env.store.hasRecordForId('person', 1), true, 'The person is in the store');
assert.equal(env.store._internalModelsFor('person').has(1), true, 'The person internalModel is loaded');

run(function() {
person.unloadRecord();
});
run(() => person.unloadRecord());

assert.equal(env.store.hasRecordForId('person', 1), false, 'The person is unloaded');
assert.equal(env.store._internalModelsFor('person').has(1), true, 'The person internalModel is retained');
Expand Down Expand Up @@ -141,7 +139,7 @@ test("a sync belongs to relationship to an unloaded record can restore that reco
});

test("an async has many relationship to an unloaded record can restore that record", function(assert) {
assert.expect(14);
assert.expect(15);

// disable background reloading so we do not re-create the relationship.
env.adapter.shouldBackgroundReloadRecord = () => false;
Expand Down Expand Up @@ -189,7 +187,7 @@ test("an async has many relationship to an unloaded record can restore that reco
};
}

run(function() {
run(() => {
env.store.push({
data: {
type: 'person',
Expand All @@ -209,7 +207,7 @@ test("an async has many relationship to an unloaded record can restore that reco
});
});

run(function() {
run(() => {
env.store.push({
data: [BOAT_ONE, BOAT_TWO]
});
Expand All @@ -228,6 +226,7 @@ test("an async has many relationship to an unloaded record can restore that reco
assert.equal(boats.get('length'), 2, 'Before unloading boats.length is correct');

run(() => boaty.unloadRecord());
assert.equal(boats.get('length'), 1, 'after unloading boats.length is correct');

assert.equal(env.store.hasRecordForId('boat', 1), false, 'The boat is unloaded');
assert.equal(env.store._internalModelsFor('boat').has(1), true, 'The boat internalModel is retained');
Expand Down
Loading