Skip to content

Commit

Permalink
feedback better unload
Browse files Browse the repository at this point in the history
  • Loading branch information
hjdivad committed Oct 21, 2016
1 parent 347c3d7 commit 28a64fe
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
41 changes: 27 additions & 14 deletions addon/-private/system/model/internal-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ function retrieveFromCurrentState(key) {
};
}

function areAllModelsUnloaded(models) {
for (let i=0; i<models.length; ++i) {
let record = models[i].record;
if (record && !(record.get('isDestroyed') || record.get('isDestroying'))) {
return false;
}
}
return true;
}

// 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 @@ -256,6 +266,16 @@ InternalModel.prototype = {
return array;
},

/**
Computes the set of internal models reachable from this internal model.
Reachability is determind over the relatinoship graph (ie a graph where
nodes are internal models and edges are belongs to or has many
relationships.
@return {Array} An array including `this` and all internal models reachable
from `this`.
*/
allRelatedInternalModels() {
let array = [];
let queue = [];
Expand All @@ -266,12 +286,13 @@ InternalModel.prototype = {
let node = queue.shift();
array.push(node);
let related = node.directlyRelatedInternalModels();
related.forEach(internalModel => {
for (let i=0; i<related.length; ++i) {
let internalModel = related[i];
if (internalModel._bfsId < bfsId) {
queue.push(internalModel);
internalModel._bfsId = bfsId;
}
});
}
}
return array;
},
Expand All @@ -280,21 +301,13 @@ InternalModel.prototype = {
this.send('unloadRecord');
this.dematerializeRecord();
let relatedInternalModels = this.allRelatedInternalModels();
let allUnloaded = true;
let record;
for (let i=0; i < relatedInternalModels.length; i++) {
record = relatedInternalModels[i].record;
if (record && !(record.get('isDestroyed') || record.get('isDestroying'))) {
allUnloaded = false;
break;
}
}
if (allUnloaded) {
relatedInternalModels.forEach((internalModel) => {
if (areAllModelsUnloaded(relatedInternalModels)) {
for (let i=0; i<relatedInternalModels.length; ++i) {
let internalModel = relatedInternalModels[i];
if (!internalModel.isDestroying) {
internalModel.destroy();
}
});
}
}
},

Expand Down
1 change: 0 additions & 1 deletion addon/-private/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,6 @@ Store = Service.extend({

var resolver = Ember.RSVP.defer('Fetching ' + typeClass + 'with id: ' + internalModel.id);
var pendingFetchItem = {
// TODO: s/record/internalModel
record: internalModel,
resolver: resolver,
options: options
Expand Down

0 comments on commit 28a64fe

Please sign in to comment.