diff --git a/addon/-private/system/record-array-manager.js b/addon/-private/system/record-array-manager.js index d64943b4fc5..e18ba5eb8b8 100644 --- a/addon/-private/system/record-array-manager.js +++ b/addon/-private/system/record-array-manager.js @@ -278,17 +278,22 @@ export default Ember.Object.extend({ var typeClass = array.type; // unregister filtered record array - var recordArrays = this.filteredRecordArrays.get(typeClass); - var index = recordArrays.indexOf(array); - if (index !== -1) { - recordArrays.splice(index, 1); - - // unregister live record array - } else if (this.liveRecordArrays.has(typeClass)) { - var liveRecordArrayForType = this.liveRecordArrayFor(typeClass); - if (array === liveRecordArrayForType) { - this.liveRecordArrays.delete(typeClass); + const recordArrays = this.filteredRecordArrays.get(typeClass); + const removedFromFiltered = remove(recordArrays, array); + + // remove from adapter populated record array + const removedFromAdapterPopulated = remove(this._adapterPopulatedRecordArrays, array); + + if (!removedFromFiltered && !removedFromAdapterPopulated) { + + // unregister live record array + if (this.liveRecordArrays.has(typeClass)) { + var liveRecordArrayForType = this.liveRecordArrayFor(typeClass); + if (array === liveRecordArrayForType) { + this.liveRecordArrays.delete(typeClass); + } } + } }, @@ -315,3 +320,14 @@ function flatten(list) { return result; } + +function remove(array, item) { + const index = array.indexOf(item); + + if (index !== -1) { + array.splice(index, 1); + return true; + } + + return false; +} diff --git a/tests/integration/record-array-manager-test.js b/tests/integration/record-array-manager-test.js index fafc0a741f2..408d76b299e 100644 --- a/tests/integration/record-array-manager-test.js +++ b/tests/integration/record-array-manager-test.js @@ -171,3 +171,27 @@ test("Should not filter a store.peekAll() array when a record property is change assert.equal(updateFilterRecordArray.called.length, 0); }); + +test('#GH-4041 store#query AdapterPopulatedRecordArrays are removed from their managers instead of retained when #destroy is called', function(assert) { + run(() => { + store.push({ + data: { + type: 'car', + id: '1', + attributes: { + make: 'Honda', + model: 'fit' + } + } + }); + }); + const query = {}; + + var adapterPopulated = manager.createAdapterPopulatedRecordArray(Car, query); + + run(() => { + adapterPopulated.destroy(); + }); + + assert.equal(manager._adapterPopulatedRecordArrays.length, 0); +});