Skip to content

Commit

Permalink
Cherrypick fixed from emberjs#5378
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Plock committed Mar 28, 2018
1 parent a47480f commit a252af0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 36 deletions.
4 changes: 3 additions & 1 deletion addon/-private/system/model/internal-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ export default class InternalModel {
// models to rematerialize their records.

return this._isDematerializing ||
this.hasScheduledDestroy() ||
this.isDestroyed ||
this.currentState.stateName === 'root.deleted.saved' ||
this.isEmpty();
Expand Down Expand Up @@ -385,9 +386,10 @@ export default class InternalModel {
this._isDematerializing = true;
this._record.destroy();
this.destroyRelationships();
this.updateRecordArrays();
this.resetRecord();
}

this.updateRecordArrays();
}

deleteRecord() {
Expand Down
82 changes: 47 additions & 35 deletions addon/-private/system/record-array-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,45 +96,49 @@ export default class RecordArrayManager {
emberRun.schedule('actions', this, this._flush);
}

_flush() {
heimdall.increment(_flush);

let pending = this._pending;
this._pending = Object.create(null);
_flushPendingInternalModelsForModelName(modelName, internalModels) {
let modelsToRemove = [];

for (let modelName in pending) {
let internalModels = pending[modelName];
for (let j = 0; j < internalModels.length; j++) {
let internalModel = internalModels[j];
// mark internalModels, so they can once again be processed by the
// recordArrayManager
internalModel._pendingRecordArrayManagerFlush = false;
// build up a set of models to ensure we have purged correctly;
if (internalModel.isHiddenFromRecordArrays()) {
modelsToRemove.push(internalModel);
}
for (let j = 0; j < internalModels.length; j++) {
let internalModel = internalModels[j];
// mark internalModels, so they can once again be processed by the
// recordArrayManager
internalModel._pendingRecordArrayManagerFlush = false;
// build up a set of models to ensure we have purged correctly;
if (internalModel.isHiddenFromRecordArrays()) {
modelsToRemove.push(internalModel);
}
}

// process filteredRecordArrays
if (this._filteredRecordArrays[modelName]) {
let recordArrays = this.filteredRecordArraysFor(modelName);
for (let i = 0; i < recordArrays.length; i++) {
this.updateFilterRecordArray(recordArrays[i], modelName, internalModels);
}
// process filteredRecordArrays
if (this._filteredRecordArrays[modelName]) {
let recordArrays = this.filteredRecordArraysFor(modelName);
for (let i = 0; i < recordArrays.length; i++) {
this.updateFilterRecordArray(recordArrays[i], modelName, internalModels);
}
}

let array = this._liveRecordArrays[modelName];
if (array) {
// TODO: skip if it only changed
// process liveRecordArrays
this.updateLiveRecordArray(array, internalModels);
}
let array = this._liveRecordArrays[modelName];
if (array) {
// TODO: skip if it only changed
// process liveRecordArrays
this.updateLiveRecordArray(array, internalModels);
}

// process adapterPopulatedRecordArrays
if (modelsToRemove.length > 0) {
removeFromAdapterPopulatedRecordArrays(modelsToRemove);
}
// process adapterPopulatedRecordArrays
if (modelsToRemove.length > 0) {
removeFromAdapterPopulatedRecordArrays(modelsToRemove);
}
}

_flush() {
heimdall.increment(_flush);

let pending = this._pending;
this._pending = Object.create(null);

for (let modelName in pending) {
this._flushPendingInternalModelsForModelName(modelName, pending[modelName]);
}
}

Expand Down Expand Up @@ -177,10 +181,11 @@ export default class RecordArrayManager {
if (shouldBeRemoved.length > 0) { array._removeInternalModels(shouldBeRemoved); }
}

// TODO: remove, utilize existing flush code but make it flush sync based on 1 modelName
_syncLiveRecordArray(array, modelName) {
assert(`recordArrayManger.syncLiveRecordArray expects modelName not modelClass as the second param`, typeof modelName === 'string');
let hasNoPotentialDeletions = Object.keys(this._pending).length === 0;
let pending = this._pending[modelName];
let hasPendingChanges = Array.isArray(pending);
let hasNoPotentialDeletions = !hasPendingChanges || pending.length === 0;
let map = this.store._internalModelsFor(modelName);
let hasNoInsertionsOrRemovals = get(map, 'length') === get(array, 'length');

Expand All @@ -194,6 +199,11 @@ export default class RecordArrayManager {
return;
}

if (hasPendingChanges) {
this._flushPendingInternalModelsForModelName(modelName, pending);
delete pending[modelName];
}

let internalModels = this._visibleInternalModelsByType(modelName);
let modelsToAdd = [];
for (let i = 0; i < internalModels.length; i++) {
Expand All @@ -205,7 +215,9 @@ export default class RecordArrayManager {
}
}

array._pushInternalModels(modelsToAdd);
if (modelsToAdd.length) {
array._pushInternalModels(modelsToAdd);
}
}

/**
Expand Down

0 comments on commit a252af0

Please sign in to comment.