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

dirty check to prevent liveRecordArrays being rebuilt too often #4661

Merged
merged 1 commit into from
Nov 16, 2016
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
38 changes: 28 additions & 10 deletions addon/-private/system/record-array-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default Ember.Object.extend({
});

this.liveRecordArrays = MapWithDefault.create({
defaultValue: typeClass => this.createRecordArray(typeClass)
defaultValue: modelClass => this.createRecordArray(modelClass)
});

this.changedRecords = [];
Expand Down Expand Up @@ -187,9 +187,27 @@ export default Ember.Object.extend({
}
},

populateLiveRecordArray(array, modelName) {
syncLiveRecordArray(array, modelClass) {
let hasNoPotentialDeletions = this.changedRecords.length === 0;
let typeMap = this.store.typeMapFor(modelClass);
let hasNoInsertionsOrRemovals = typeMap.records.length === array.length;

/*
Ideally the recordArrayManager has knowledge of the changes to be applied to
liveRecordArrays, and is capable of strategically flushing those changes and applying
small diffs if desired. However, until we've refactored recordArrayManager, this dirty
check prevents us from unnecessarily wiping out live record arrays returned by peekAll.
*/
if (hasNoPotentialDeletions && hasNoInsertionsOrRemovals) {
return;
}

this.populateLiveRecordArray(array, modelClass);
},

populateLiveRecordArray(array, modelClass) {
heimdall.increment(populateLiveRecordArray);
let typeMap = this.store.typeMapFor(modelName);
let typeMap = this.store.typeMapFor(modelClass);
let records = typeMap.records;
let record;

Expand All @@ -211,20 +229,20 @@ export default Ember.Object.extend({

@method updateFilter
@param {Array} array
@param {String} modelName
@param {Class} modelClass
@param {Function} filter
*/
updateFilter(array, modelName, filter) {
updateFilter(array, modelClass, filter) {
heimdall.increment(updateFilter);
let typeMap = this.store.typeMapFor(modelName);
let typeMap = this.store.typeMapFor(modelClass);
let records = typeMap.records;
let record;

for (let i = 0; i < records.length; i++) {
record = records[i];

if (!record.isDeleted() && !record.isEmpty()) {
this.updateFilterRecordArray(array, filter, modelName, record);
this.updateFilterRecordArray(array, filter, modelClass, record);
}
}
},
Expand All @@ -246,13 +264,13 @@ export default Ember.Object.extend({
Create a `DS.RecordArray` for a type.

@method createRecordArray
@param {Class} typeClass
@param {Class} modelClass
@return {DS.RecordArray}
*/
createRecordArray(typeClass) {
createRecordArray(modelClass) {
heimdall.increment(createRecordArray);
return RecordArray.create({
type: typeClass,
type: modelClass,
content: Ember.A(),
store: this.store,
isLoaded: true,
Expand Down
6 changes: 3 additions & 3 deletions addon/-private/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -1596,10 +1596,10 @@ Store = Service.extend({
heimdall.increment(peekAll);
assert("You need to pass a model name to the store's peekAll method", isPresent(modelName));
assert('Passing classes to store methods has been removed. Please pass a dasherized string instead of '+ Ember.inspect(modelName), typeof modelName === 'string');
var typeClass = this.modelFor(modelName);
let modelClass = this.modelFor(modelName);
let liveRecordArray = this.recordArrayManager.liveRecordArrayFor(modelClass);

var liveRecordArray = this.recordArrayManager.liveRecordArrayFor(typeClass);
this.recordArrayManager.populateLiveRecordArray(liveRecordArray, typeClass);
this.recordArrayManager.syncLiveRecordArray(liveRecordArray, modelClass);

return liveRecordArray;
},
Expand Down