From 1d9116c33bdb384576f3ff99e55b9468817fa25c Mon Sep 17 00:00:00 2001 From: Derek Wickern Date: Tue, 6 Feb 2018 14:58:39 -0800 Subject: [PATCH 1/2] Fix unloadAll to also unload live records --- addon/-private/system/model/internal-model.js | 1 + addon/-private/system/record-array-manager.js | 7 ++++ tests/integration/records/unload-test.js | 40 +++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/addon/-private/system/model/internal-model.js b/addon/-private/system/model/internal-model.js index d9ccb8f8e2f..8d6a6ffa2b8 100644 --- a/addon/-private/system/model/internal-model.js +++ b/addon/-private/system/model/internal-model.js @@ -501,6 +501,7 @@ export default class InternalModel { if (this.isDestroyed) { return; } this.send('unloadRecord'); this.dematerializeRecord(); + this.store.recordArrayManager.recordWasUnloaded(this); if (this._scheduledDestroy === null) { // TODO: use run.schedule once we drop 1.13 diff --git a/addon/-private/system/record-array-manager.js b/addon/-private/system/record-array-manager.js index 9802e02ce79..89e0901ff4b 100644 --- a/addon/-private/system/record-array-manager.js +++ b/addon/-private/system/record-array-manager.js @@ -76,6 +76,13 @@ export default class RecordArrayManager { this.internalModelDidChange(internalModel); } + recordWasUnloaded(internalModel) { + let array = this._liveRecordArrays[internalModel.modelName]; + if (array) { + array._removeInternalModels([internalModel]); + } + } + internalModelDidChange(internalModel) { heimdall.increment(recordDidChange); diff --git a/tests/integration/records/unload-test.js b/tests/integration/records/unload-test.js index 3afc287f901..e903d50f484 100644 --- a/tests/integration/records/unload-test.js +++ b/tests/integration/records/unload-test.js @@ -2008,3 +2008,43 @@ test('1 sync : many async unload sync side', function(assert) { }) ); }); + +test("unload live records", function(assert) { + env.adapter.findAll = () => { + return { + data: [{ + type: 'person', + id: '1', + attributes: { + name: 'Adam Sunderland' + } + }, { + type: 'person', + id: '2', + attributes: { + name: 'Bob Bobson' + } + }] + } + }; + + return env.store.findAll('person').then(records => { + assert.equal(records.get('length'), 2, 'two records returned from findAll'); + + const peeked = env.store.peekAll('person'); + assert.equal(peeked.get('length'), 2, 'two records returned from peekAll'); + + run(function() { + env.store.unloadAll('person'); + }); + + assert.equal(records.get('length'), 0, 'findAll array was cleared'); + assert.equal(peeked.get('length'), 0, 'peekAll array was cleared'); + + return env.store.findAll('person').then(recordsAgain => { + assert.equal(recordsAgain.get('length'), 2, 'two records returned from new findAll'); + assert.equal(records.get('length'), 2, 'old findAll array has two records'); + assert.equal(peeked.get('length'), 2, 'old peekAll array has two records'); + }); + }); +}); From f99b70c1214f3979d13ac39ece433913eefdbb96 Mon Sep 17 00:00:00 2001 From: Derek Wickern Date: Tue, 6 Feb 2018 15:45:33 -0800 Subject: [PATCH 2/2] fix watchModelTypes test there's now an additional "updated" event when the record is unloaded during tear-down of the test suite --- tests/integration/debug-adapter-test.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/integration/debug-adapter-test.js b/tests/integration/debug-adapter-test.js index 5833c5506c8..4510c6d6006 100644 --- a/tests/integration/debug-adapter-test.js +++ b/tests/integration/debug-adapter-test.js @@ -70,7 +70,7 @@ test('Watching Model Types', function(assert) { assert.equal(types[0].count, 1); } - debugAdapter.watchModelTypes(added, updated); + const release = debugAdapter.watchModelTypes(added, updated); run(() => { store.push({ @@ -83,6 +83,8 @@ test('Watching Model Types', function(assert) { } }); }); + + release(); }); test("Watching Records", function(assert) {