Skip to content

Commit

Permalink
models/crate: Make owners getter rely on loadOwnersTask
Browse files Browse the repository at this point in the history
This fixes the following deprecations:
- The toArray method on ember-data's PromiseManyArray is deprecated.
- The `toArray` method on the class ManyArray is deprecated.

by explicitly loading `owners` with a task.
  • Loading branch information
eth3lbert committed Dec 13, 2024
1 parent 6f078e8 commit 4c0c72c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
14 changes: 11 additions & 3 deletions app/models/crate.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Model, { attr, hasMany } from '@ember-data/model';
import { assert } from '@ember/debug';
import { waitForPromise } from '@ember/test-waiters';
import { cached } from '@glimmer/tracking';

import { task } from 'ember-concurrency';

import { apiAction } from '@mainmatter/ember-api-actions';

export default class Crate extends Model {
Expand Down Expand Up @@ -66,9 +69,9 @@ export default class Crate extends Model {
}

get owners() {
let teams = this.owner_team.toArray() ?? [];
let users = this.owner_user.toArray() ?? [];
return [...teams, ...users];
let { last } = this.loadOwnersTask;
assert('`loadOwnersTask.perform()` must be called before accessing `owners`', last != null);
return last?.value ?? [];
}

async follow() {
Expand Down Expand Up @@ -100,6 +103,11 @@ export default class Crate extends Model {
throw response;
}
}

loadOwnersTask = task(async () => {
let [teams, users] = await Promise.all([this.owner_team, this.owner_user]);
return [...(teams ?? []), ...(users ?? [])];
});
}

function compareVersionBySemver(a, b) {
Expand Down
5 changes: 5 additions & 0 deletions app/routes/crate/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ export default class VersionRoute extends Route {
});

let { crate, version } = model;

waitForPromise(crate.loadOwnersTask.perform()).catch(() => {
// ignored
});

if (!crate.documentation || crate.documentation.startsWith('https://docs.rs/')) {
version.loadDocsStatusTask.perform().catch(error => {
// report unexpected errors to Sentry and ignore `ajax()` errors
Expand Down
4 changes: 4 additions & 0 deletions tests/components/crate-sidebar/playground-button-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module('Component | CrateSidebar | Playground Button', function (hooks) {
let store = this.owner.lookup('service:store');
this.crate = await store.findRecord('crate', crate.name);
this.version = (await this.crate.versions).firstObject;
await this.crate.loadOwnersTask.perform();

await render(hbs`<CrateSidebar @crate={{this.crate}} @version={{this.version}} />`);
assert.dom('[data-test-playground-button]').doesNotExist();
Expand All @@ -46,6 +47,7 @@ module('Component | CrateSidebar | Playground Button', function (hooks) {
let store = this.owner.lookup('service:store');
this.crate = await store.findRecord('crate', crate.name);
this.version = (await this.crate.versions).firstObject;
await this.crate.loadOwnersTask.perform();

let expectedHref =
'https://play.rust-lang.org/?edition=2021&code=use%20aho_corasick%3B%0A%0Afn%20main()%20%7B%0A%20%20%20%20%2F%2F%20try%20using%20the%20%60aho_corasick%60%20crate%20here%0A%7D';
Expand All @@ -64,6 +66,7 @@ module('Component | CrateSidebar | Playground Button', function (hooks) {
let store = this.owner.lookup('service:store');
this.crate = await store.findRecord('crate', crate.name);
this.version = (await this.crate.versions).firstObject;
await this.crate.loadOwnersTask.perform();

render(hbs`<CrateSidebar @crate={{this.crate}} @version={{this.version}} />`);
await waitFor('[data-test-owners]');
Expand All @@ -82,6 +85,7 @@ module('Component | CrateSidebar | Playground Button', function (hooks) {
let store = this.owner.lookup('service:store');
this.crate = await store.findRecord('crate', crate.name);
this.version = (await this.crate.versions).firstObject;
await this.crate.loadOwnersTask.perform();

await render(hbs`<CrateSidebar @crate={{this.crate}} @version={{this.version}} />`);
assert.dom('[data-test-playground-button]').doesNotExist();
Expand Down
3 changes: 3 additions & 0 deletions tests/components/crate-sidebar/toml-snippet-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module('Component | CrateSidebar | toml snippet', function (hooks) {
let store = this.owner.lookup('service:store');
this.crate = await store.findRecord('crate', crate.name);
this.version = (await this.crate.versions).firstObject;
await this.crate.loadOwnersTask.perform();

await render(hbs`<CrateSidebar @crate={{this.crate}} @version={{this.version}} />`);
assert.dom('[title="Copy command to clipboard"]').exists().hasText('cargo add foo');
Expand All @@ -35,6 +36,7 @@ module('Component | CrateSidebar | toml snippet', function (hooks) {
let store = this.owner.lookup('service:store');
this.crate = await store.findRecord('crate', crate.name);
this.version = (await this.crate.versions).firstObject;
await this.crate.loadOwnersTask.perform();

await render(hbs`<CrateSidebar @crate={{this.crate}} @version={{this.version}} />`);
assert.dom('[title="Copy Cargo.toml snippet to clipboard"]').exists().hasText('foo = "1.0.0"');
Expand All @@ -47,6 +49,7 @@ module('Component | CrateSidebar | toml snippet', function (hooks) {
let store = this.owner.lookup('service:store');
this.crate = await store.findRecord('crate', crate.name);
this.version = (await this.crate.versions).firstObject;
await this.crate.loadOwnersTask.perform();

await render(hbs`<CrateSidebar @crate={{this.crate}} @version={{this.version}} />`);
assert.dom('[title="Copy Cargo.toml snippet to clipboard"]').exists().hasText('foo = "1.0.0-alpha"');
Expand Down
15 changes: 5 additions & 10 deletions tests/components/owners-list-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ module('Component | OwnersList', function (hooks) {

let store = this.owner.lookup('service:store');
this.crate = await store.findRecord('crate', crate.name);
await this.crate.hasMany('owner_team').load();
await this.crate.hasMany('owner_user').load();
await this.crate.loadOwnersTask.perform();

await render(hbs`<OwnersList @owners={{this.crate.owners}} />`);
assert.dom('[data-test-owners="detailed"]').exists();
Expand All @@ -44,8 +43,7 @@ module('Component | OwnersList', function (hooks) {

let store = this.owner.lookup('service:store');
this.crate = await store.findRecord('crate', crate.name);
await this.crate.hasMany('owner_team').load();
await this.crate.hasMany('owner_user').load();
await this.crate.loadOwnersTask.perform();

await render(hbs`<OwnersList @owners={{this.crate.owners}} />`);
assert.dom('[data-test-owners="detailed"]').exists();
Expand All @@ -70,8 +68,7 @@ module('Component | OwnersList', function (hooks) {

let store = this.owner.lookup('service:store');
this.crate = await store.findRecord('crate', crate.name);
await this.crate.hasMany('owner_team').load();
await this.crate.hasMany('owner_user').load();
await this.crate.loadOwnersTask.perform();

await render(hbs`<OwnersList @owners={{this.crate.owners}} />`);
assert.dom('[data-test-owners="detailed"]').exists();
Expand All @@ -93,8 +90,7 @@ module('Component | OwnersList', function (hooks) {

let store = this.owner.lookup('service:store');
this.crate = await store.findRecord('crate', crate.name);
await this.crate.hasMany('owner_team').load();
await this.crate.hasMany('owner_user').load();
await this.crate.loadOwnersTask.perform();

await render(hbs`<OwnersList @owners={{this.crate.owners}} />`);
assert.dom('[data-test-owners="basic"]').exists();
Expand All @@ -120,8 +116,7 @@ module('Component | OwnersList', function (hooks) {

let store = this.owner.lookup('service:store');
this.crate = await store.findRecord('crate', crate.name);
await this.crate.hasMany('owner_team').load();
await this.crate.hasMany('owner_user').load();
await this.crate.loadOwnersTask.perform();

await render(hbs`<OwnersList @owners={{this.crate.owners}} />`);
assert.dom('[data-test-owners="detailed"]').exists();
Expand Down

0 comments on commit 4c0c72c

Please sign in to comment.