Skip to content

Commit

Permalink
models/crate: Load crate.versions explicitly with a task
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
eth3lbert committed Dec 13, 2024
1 parent 5c55262 commit f3fe6d4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
16 changes: 13 additions & 3 deletions app/models/crate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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]));
}

Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions app/routes/crate/versions.js
Original file line number Diff line number Diff line change
@@ -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());
}
}
6 changes: 3 additions & 3 deletions tests/components/version-list-row-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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`<VersionList::Row @version={{this.version}} />`);
Expand Down Expand Up @@ -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];
Expand Down
8 changes: 4 additions & 4 deletions tests/models/version-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })),
Expand Down Expand Up @@ -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 })),
Expand All @@ -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 })),
Expand All @@ -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 })),
Expand Down

0 comments on commit f3fe6d4

Please sign in to comment.