UI: Ember data upgrade prep: cleanup store and lazyPaginatedQuery (#28690)

* revert hacks necessary for ember-data 4.11

* remove stubbed unloadAll and unloadRecord methods

* remove clearAllDatasets and just use clearDataset()
This commit is contained in:
claire bontempo 2024-10-11 14:18:56 -07:00 committed by GitHub
parent ef967a0d8c
commit 217380cf87
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 24 additions and 66 deletions

View file

@ -108,7 +108,7 @@ export default Component.extend({
const currentRoute = owner.lookup(`router:main`).currentRouteName;
try {
this.store.clearAllDatasets();
this.store.clearDataset();
yield this.router.transitionTo(currentRoute);
this.logAndOutput(null, { type: 'success', content: 'The current screen has been refreshed!' });
} catch (error) {

View file

@ -32,7 +32,7 @@ export default class GeneratedItemList extends Component {
@action
refreshItemList() {
const route = getOwner(this).lookup(`route:${this.router.currentRouteName}`);
this.store.clearAllDatasets();
this.store.clearDataset();
route.refresh();
}
}

View file

@ -38,12 +38,12 @@ export default Route.extend(ListRoute, {
willTransition(transition) {
window.scrollTo(0, 0);
if (!transition || transition.targetName !== this.routeName) {
this.store.clearAllDatasets();
this.store.clearDataset();
}
return true;
},
reload() {
this.store.clearAllDatasets();
this.store.clearDataset();
this.refresh();
},
},

View file

@ -38,12 +38,12 @@ export default Route.extend(ListRoute, {
willTransition(transition) {
window.scrollTo(0, 0);
if (transition.targetName !== this.routeName) {
this.store.clearAllDatasets();
this.store.clearDataset();
}
return true;
},
reload() {
this.store.clearAllDatasets();
this.store.clearDataset();
this.refresh();
},
},

View file

@ -104,7 +104,7 @@ export default Route.extend({
willTransition(transition) {
window.scrollTo(0, 0);
if (transition.targetName !== this.routeName) {
this.store.clearAllDatasets();
this.store.clearDataset();
}
return true;
},

View file

@ -46,12 +46,12 @@ export default Route.extend(ListRoute, {
willTransition(transition) {
window.scrollTo(0, 0);
if (transition.targetName !== this.routeName) {
this.store.clearAllDatasets();
this.store.clearDataset();
}
return true;
},
reload() {
this.store.clearAllDatasets();
this.store.clearDataset();
this.refresh();
},
},

View file

@ -74,12 +74,12 @@ export default Route.extend(UnloadModel, {
willTransition(transition) {
window.scrollTo(0, 0);
if (!transition || transition.targetName !== this.routeName) {
this.store.clearAllDatasets();
this.store.clearDataset();
}
return true;
},
reload() {
this.store.clearAllDatasets();
this.store.clearDataset();
this.refresh();
},
},

View file

@ -65,12 +65,12 @@ export default Route.extend(ClusterRoute, ListRoute, {
willTransition(transition) {
window.scrollTo(0, 0);
if (!transition || transition.targetName !== this.routeName) {
this.store.clearAllDatasets();
this.store.clearDataset();
}
return true;
},
reload() {
this.store.clearAllDatasets();
this.store.clearDataset();
this.refresh();
},
},

View file

@ -163,7 +163,7 @@ export default Route.extend({
const has404 = this.has404;
// only clear store cache if this is a new model
if (secret !== controller?.baseKey?.id) {
this.store.clearAllDatasets();
this.store.clearDataset();
}
controller.set('hasModel', true);
controller.setProperties({
@ -220,12 +220,12 @@ export default Route.extend({
willTransition(transition) {
window.scrollTo(0, 0);
if (transition.targetName !== this.routeName) {
this.store.clearAllDatasets();
this.store.clearDataset();
}
return true;
},
reload() {
this.store.clearAllDatasets();
this.store.clearDataset();
this.refresh();
},
},

View file

@ -6,7 +6,7 @@
import Store, { CacheHandler } from '@ember-data/store';
import RequestManager from '@ember-data/request';
import { LegacyNetworkHandler } from '@ember-data/legacy-compat';
import { run, schedule } from '@ember/runloop';
import { schedule } from '@ember/runloop';
import { resolve, Promise } from 'rsvp';
import { dasherize } from '@ember/string';
import { assert } from '@ember/debug';
@ -159,20 +159,10 @@ export default class StoreService extends Store {
return resp;
}
forceUnload(modelName) {
// Hack to get unloadAll to work correctly until we update to ember-data@4.12
// so that all the records are properly unloaded and we don't get ghost records
this.peekAll(modelName).length;
// force destroy queue to flush https://github.com/emberjs/data/issues/5447
run(() => this.unloadAll(modelName));
}
// pushes records into the store and returns the result
fetchPage(modelName, query) {
const response = this.constructResponse(modelName, query);
this.forceUnload(modelName);
// Hack to ensure the pushed records below all get in the store. remove with update to ember-data@4.12
this.peekAll(modelName).length;
this.unloadAll(modelName);
return new Promise((resolve) => {
// push subset of records into the store
schedule('destroy', () => {
@ -185,8 +175,6 @@ export default class StoreService extends Store {
'query'
)
);
// Hack to make sure all records get in model correctly. remove with update to ember-data@4.12
this.peekAll(modelName).length;
const model = this.peekAll(modelName).slice();
model.set('meta', response.meta);
resolve(model);
@ -218,34 +206,4 @@ export default class StoreService extends Store {
}
this.lazyCaches.clear();
}
clearAllDatasets() {
this.clearDataset();
}
/**
* this is designed to be a temporary workaround to an issue in the test environment after upgrading to Ember 4.12
* when performing an unloadAll or unloadRecord for auth-method or secret-engine models within the app code an error breaks the tests
* after the test run is finished during teardown an unloadAll happens and the error "Expected a stable identifier" is thrown
* it seems that when the unload happens in the app, for some reason the mount-config relationship models are not unloaded
* then when the unloadAll happens a second time during test teardown there seems to be an issue since those records should already have been unloaded
* when logging in the teardownRecord hook, it appears that other embedded inverse: null relationships such as replication-attributes are torn down when the parent model is unloaded
* the following fixes the issue by explicitly unloading the mount-config models associated to the parent
* this should be looked into further to find the root cause, at which time these overrides may be removed
*/
unloadAll(modelName) {
const hasMountConfig = ['auth-method', 'secret-engine'];
if (hasMountConfig.includes(modelName)) {
this.peekAll(modelName).forEach((record) => this.unloadRecord(record));
} else {
super.unloadAll(modelName);
}
}
unloadRecord(record) {
const hasMountConfig = ['auth-method', 'secret-engine'];
if (record && hasMountConfig.includes(record.constructor.modelName) && record.config) {
super.unloadRecord(record.config);
}
super.unloadRecord(record);
}
}

View file

@ -35,12 +35,12 @@ export default Mixin.create({
willTransition(transition) {
window.scrollTo(0, 0);
if (transition.targetName !== this.routeName) {
this.store.clearAllDatasets();
this.store.clearDataset();
}
return true;
},
reload() {
this.store.clearAllDatasets();
this.store.clearDataset();
this.refresh();
},
},

View file

@ -31,12 +31,12 @@ export default Route.extend(ListRoute, {
willTransition(transition) {
window.scrollTo(0, 0);
if (transition.targetName !== this.routeName) {
this.store.clearAllDatasets();
this.store.clearDataset();
}
return true;
},
reload() {
this.store.clearAllDatasets();
this.store.clearDataset();
this.refresh();
},
},

View file

@ -68,14 +68,14 @@ module('Unit | Service | store', function (hooks) {
assert.notOk(this.store.get('lazyCaches').has('transit-key'), 'cache is no longer stored');
});
test('store.clearAllDatasets', function (assert) {
test('store.clearDataset with no args clears entire cache', function (assert) {
const arr = ['one', 'two'];
const arr2 = ['one', 'two', 'three', 'four'];
this.store.storeDataset('data', { id: 1 }, {}, arr);
this.store.storeDataset('transit-key', { id: 2 }, {}, arr2);
assert.strictEqual(this.store.get('lazyCaches').size, 2, 'it stores both keys');
this.store.clearAllDatasets();
this.store.clearDataset();
assert.strictEqual(this.store.get('lazyCaches').size, 0, 'deletes all of the keys');
assert.notOk(this.store.get('lazyCaches').has('transit-key'), 'first cache key is no longer stored');
assert.notOk(this.store.get('lazyCaches').has('data'), 'second cache key is no longer stored');