Skip to content

Commit 2e536ff

Browse files
committed
models/crate: Make owners getter rely on loadOwnersTask
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.
1 parent 74c2d22 commit 2e536ff

File tree

5 files changed

+27
-13
lines changed

5 files changed

+27
-13
lines changed

app/models/crate.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import Model, { attr, hasMany } from '@ember-data/model';
2+
import { assert } from '@ember/debug';
23
import { waitForPromise } from '@ember/test-waiters';
34
import { cached } from '@glimmer/tracking';
45

56
import { apiAction } from '@mainmatter/ember-api-actions';
7+
import { task } from 'ember-concurrency';
68

79
export default class Crate extends Model {
810
@attr name;
@@ -66,9 +68,9 @@ export default class Crate extends Model {
6668
}
6769

6870
get owners() {
69-
let teams = this.owner_team.toArray() ?? [];
70-
let users = this.owner_user.toArray() ?? [];
71-
return [...teams, ...users];
71+
let { last } = this.loadOwnersTask;
72+
assert('`loadOwnersTask.perform()` must be called before accessing `owners`', last != null);
73+
return last?.value ?? [];
7274
}
7375

7476
async follow() {
@@ -100,6 +102,11 @@ export default class Crate extends Model {
100102
throw response;
101103
}
102104
}
105+
106+
loadOwnersTask = task(async () => {
107+
let [teams, users] = await Promise.all([this.owner_team, this.owner_user]);
108+
return [...(teams ?? []), ...(users ?? [])];
109+
});
103110
}
104111

105112
function compareVersionBySemver(a, b) {

app/routes/crate/version.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ export default class VersionRoute extends Route {
5656
});
5757

5858
let { crate, version } = model;
59+
60+
waitForPromise(crate.loadOwnersTask.perform()).catch(() => {
61+
// ignored
62+
});
63+
5964
if (!crate.documentation || crate.documentation.startsWith('https://docs.rs/')) {
6065
version.loadDocsStatusTask.perform().catch(error => {
6166
// report unexpected errors to Sentry and ignore `ajax()` errors

tests/components/crate-sidebar/playground-button-test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module('Component | CrateSidebar | Playground Button', function (hooks) {
3434
let store = this.owner.lookup('service:store');
3535
this.crate = await store.findRecord('crate', crate.name);
3636
this.version = (await this.crate.versions).firstObject;
37+
await this.crate.loadOwnersTask.perform();
3738

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

5052
let expectedHref =
5153
'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';
@@ -64,6 +66,7 @@ module('Component | CrateSidebar | Playground Button', function (hooks) {
6466
let store = this.owner.lookup('service:store');
6567
this.crate = await store.findRecord('crate', crate.name);
6668
this.version = (await this.crate.versions).firstObject;
69+
await this.crate.loadOwnersTask.perform();
6770

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

8690
await render(hbs`<CrateSidebar @crate={{this.crate}} @version={{this.version}} />`);
8791
assert.dom('[data-test-playground-button]').doesNotExist();

tests/components/crate-sidebar/toml-snippet-test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module('Component | CrateSidebar | toml snippet', function (hooks) {
1818
let store = this.owner.lookup('service:store');
1919
this.crate = await store.findRecord('crate', crate.name);
2020
this.version = (await this.crate.versions).firstObject;
21+
await this.crate.loadOwnersTask.perform();
2122

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

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

5154
await render(hbs`<CrateSidebar @crate={{this.crate}} @version={{this.version}} />`);
5255
assert.dom('[title="Copy Cargo.toml snippet to clipboard"]').exists().hasText('foo = "1.0.0-alpha"');

tests/components/owners-list-test.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ module('Component | OwnersList', function (hooks) {
2020

2121
let store = this.owner.lookup('service:store');
2222
this.crate = await store.findRecord('crate', crate.name);
23-
await this.crate.hasMany('owner_team').load();
24-
await this.crate.hasMany('owner_user').load();
23+
await this.crate.loadOwnersTask.perform();
2524

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

4544
let store = this.owner.lookup('service:store');
4645
this.crate = await store.findRecord('crate', crate.name);
47-
await this.crate.hasMany('owner_team').load();
48-
await this.crate.hasMany('owner_user').load();
46+
await this.crate.loadOwnersTask.perform();
4947

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

7169
let store = this.owner.lookup('service:store');
7270
this.crate = await store.findRecord('crate', crate.name);
73-
await this.crate.hasMany('owner_team').load();
74-
await this.crate.hasMany('owner_user').load();
71+
await this.crate.loadOwnersTask.perform();
7572

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

9491
let store = this.owner.lookup('service:store');
9592
this.crate = await store.findRecord('crate', crate.name);
96-
await this.crate.hasMany('owner_team').load();
97-
await this.crate.hasMany('owner_user').load();
93+
await this.crate.loadOwnersTask.perform();
9894

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

121117
let store = this.owner.lookup('service:store');
122118
this.crate = await store.findRecord('crate', crate.name);
123-
await this.crate.hasMany('owner_team').load();
124-
await this.crate.hasMany('owner_user').load();
119+
await this.crate.loadOwnersTask.perform();
125120

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

0 commit comments

Comments
 (0)