From f3fe6d460f16f032096b486ad1c75afcb488713e Mon Sep 17 00:00:00 2001 From: eth3lbert Date: Fri, 13 Dec 2024 13:59:48 +0800 Subject: [PATCH] models/crate: Load `crate.versions` explicitly with a task A debugging assert is also included to inform missing `perform()` called before accessing getters. This fix the following deprecations: - The toArray method on ember-data's PromiseManyArray is deprecated. - The `toArray` method on the class ManyArray is deprecated. --- app/models/crate.js | 16 +++++++++++++--- app/routes/crate/versions.js | 12 ++++++++++++ tests/components/version-list-row-test.js | 6 +++--- tests/models/version-test.js | 8 ++++---- 4 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 app/routes/crate/versions.js diff --git a/app/models/crate.js b/app/models/crate.js index 2c8db61646..caed897c92 100644 --- a/app/models/crate.js +++ b/app/models/crate.js @@ -38,12 +38,16 @@ export default class Crate extends Model { @hasMany('dependency', { async: true, inverse: null }) reverse_dependencies; @cached get versionIdsBySemver() { - let versions = this.versions.toArray() ?? []; + let { last } = this.loadVersionsTask; + assert('`loadVersionsTask.perform()` must be called before calling `versionIdsBySemver`', last != null); + let versions = last?.value.slice() ?? []; return versions.sort(compareVersionBySemver).map(v => v.id); } @cached get versionIdsByDate() { - let versions = this.versions.toArray() ?? []; + let { last } = this.loadVersionsTask; + assert('`loadVersionsTask.perform()` must be called before calling `versionIdsByDate`', last != null); + let versions = last?.value.slice() ?? []; return versions.sort(compareVersionByDate).map(v => v.id); } @@ -52,7 +56,9 @@ export default class Crate extends Model { } @cached get versionsObj() { - let versions = this.versions.toArray() ?? []; + let { last } = this.loadVersionsTask; + assert('`loadVersionsTask.perform()` must be called before calling `versionsObj`', last != null); + let versions = last?.value.slice() ?? []; return Object.fromEntries(versions.map(v => [v.id, v])); } @@ -118,6 +124,10 @@ export default class Crate extends Model { let [teams, users] = await Promise.all([this.owner_team, this.owner_user]); return [...(teams ?? []), ...(users ?? [])]; }); + + loadVersionsTask = task(async () => { + return (await this.versions) ?? []; + }); } function compareVersionBySemver(a, b) { diff --git a/app/routes/crate/versions.js b/app/routes/crate/versions.js new file mode 100644 index 0000000000..1144952055 --- /dev/null +++ b/app/routes/crate/versions.js @@ -0,0 +1,12 @@ +import Route from '@ember/routing/route'; +import { waitForPromise } from '@ember/test-waiters'; + +export default class VersionsRoute extends Route { + setupController(controller) { + super.setupController(...arguments); + let crate = this.modelFor('crate'); + controller.set('crate', crate); + // TODO: Add error handling + waitForPromise(crate.loadVersionsTask.perform()); + } +} diff --git a/tests/components/version-list-row-test.js b/tests/components/version-list-row-test.js index 8c9990284f..e651767cc1 100644 --- a/tests/components/version-list-row-test.js +++ b/tests/components/version-list-row-test.js @@ -18,7 +18,7 @@ module('Component | VersionList::Row', function (hooks) { let store = this.owner.lookup('service:store'); let crateRecord = await store.findRecord('crate', crate.name); - let versions = (await crateRecord.versions).slice(); + let versions = (await crateRecord.loadVersionsTask.perform()).slice(); await crateRecord.loadOwnerUserTask.perform(); this.firstVersion = versions[0]; this.secondVersion = versions[1]; @@ -39,7 +39,7 @@ module('Component | VersionList::Row', function (hooks) { let store = this.owner.lookup('service:store'); let crateRecord = await store.findRecord('crate', crate.name); - this.version = (await crateRecord.versions).slice()[0]; + this.version = (await crateRecord.loadVersionsTask.perform()).slice()[0]; await crateRecord.loadOwnerUserTask.perform(); await render(hbs``); @@ -73,7 +73,7 @@ module('Component | VersionList::Row', function (hooks) { let store = this.owner.lookup('service:store'); let crateRecord = await store.findRecord('crate', crate.name); - let versions = (await crateRecord.versions).slice(); + let versions = (await crateRecord.loadVersionsTask.perform()).slice(); await crateRecord.loadOwnerUserTask.perform(); this.firstVersion = versions[0]; this.secondVersion = versions[1]; diff --git a/tests/models/version-test.js b/tests/models/version-test.js index 3cac01fcc6..92203fc2e2 100644 --- a/tests/models/version-test.js +++ b/tests/models/version-test.js @@ -167,7 +167,7 @@ module('Model | Version', function (hooks) { } let crateRecord = await this.store.findRecord('crate', crate.name); - let versions = (await crateRecord.versions).slice(); + let versions = (await crateRecord.loadVersionsTask.perform()).slice(); assert.deepEqual( versions.map(it => ({ num: it.num, isHighestOfReleaseTrack: it.isHighestOfReleaseTrack })), @@ -198,7 +198,7 @@ module('Model | Version', function (hooks) { this.server.create('version', { crate, num: '0.4.2', yanked: true }); let crateRecord = await this.store.findRecord('crate', crate.name); - let versions = (await crateRecord.versions).slice(); + let versions = (await crateRecord.loadVersionsTask.perform()).slice(); assert.deepEqual( versions.map(it => ({ num: it.num, isHighestOfReleaseTrack: it.isHighestOfReleaseTrack })), @@ -216,7 +216,7 @@ module('Model | Version', function (hooks) { this.server.create('version', { crate, num: '0.4.1' }); let crateRecord = await this.store.findRecord('crate', crate.name); - let versions = (await crateRecord.versions).slice(); + let versions = (await crateRecord.loadVersionsTask.perform()).slice(); assert.deepEqual( versions.map(it => ({ num: it.num, isHighestOfReleaseTrack: it.isHighestOfReleaseTrack })), @@ -229,7 +229,7 @@ module('Model | Version', function (hooks) { this.server.create('version', { crate, num: '0.4.2' }); this.server.create('version', { crate, num: '0.4.3', yanked: true }); crateRecord = await this.store.findRecord('crate', crate.name, { reload: true }); - versions = (await crateRecord.versions).slice(); + versions = (await crateRecord.loadVersionsTask.perform()).slice(); assert.deepEqual( versions.map(it => ({ num: it.num, isHighestOfReleaseTrack: it.isHighestOfReleaseTrack })),