Skip to content

Commit

Permalink
Merge pull request #4042 from fivetanley/adapter-populated-record-arr…
Browse files Browse the repository at this point in the history
…ay-leaks

[BUGFIX beta] prevent calls to store.query leaking
  • Loading branch information
fivetanley committed Jan 5, 2016
2 parents 9615003 + 0eedecb commit 1c02f8f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
36 changes: 26 additions & 10 deletions addon/-private/system/record-array-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,17 +279,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);
}
}

}
},

Expand All @@ -316,3 +321,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;
}
24 changes: 24 additions & 0 deletions tests/integration/record-array-manager-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit 1c02f8f

Please sign in to comment.