Skip to content

Commit

Permalink
removing async computed from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ef4 committed May 16, 2024
1 parent 991f0cc commit fec5e23
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 419 deletions.
22 changes: 0 additions & 22 deletions docs/computed-fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,6 @@ When any field consumed by the computed field changes, the value will [rerender]
Computed fields are eagerly evaluated, they do not need to be consumed for `computeVia` to run.
## Async computation
The calculation can be async:
```typescript
@field slowName = contains(StringCard, {
computeVia: async function () {
await new Promise((resolve) => setTimeout(resolve, 10));
return this.firstName;
}
});
```
The values of async computed fields are available synchronously in other `computedVia` functions. Since async field values are not guaranteed to be present, you can use card API [`getIfReady`](https://github.com/cardstack/boxel/blob/307d78676ebdb93cee75d61b8812914013a094a7/packages/base/card-api.gts#L2112) avoid errors about the field not being ready:
```javascript
await getIfReady(this, 'fullName');
// 'Carl Stack'
await getIfReady(this, 'fullName');
// {type: 'not-ready', fieldName: 'fullName', instance: …}
```
## Computed `linksTo` and `linksToMany`
While `computeVia` can currently only be applied to `contains`/`containsMany` fields, there’s a plan to let it work for `linksTo` and `linksToMany` in the future.
1 change: 1 addition & 0 deletions packages/host/app/services/render-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export default class RenderService extends Service {
return parseCardHtml(html);
}

// TODO delete me
private async resolveField(
params: Omit<RenderCardParams, 'format'> & { fieldName: string },
): Promise<void> {
Expand Down
6 changes: 2 additions & 4 deletions packages/host/tests/cards/person.gts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ export class Person extends CardDef {
@field email = contains(StringCard);
@field posts = contains(NumberCard);
@field fullName = contains(StringCard, {
computeVia: async function (this: Person) {
await new Promise((resolve) => setTimeout(resolve, 10));
computeVia: function (this: Person) {
return `${this.firstName ?? ''} ${this.lastName ?? ''}`;
},
});
Expand Down Expand Up @@ -46,8 +45,7 @@ export class PersonField extends FieldDef {
@field email = contains(StringCard);
@field posts = contains(NumberCard);
@field fullName = contains(StringCard, {
computeVia: async function (this: Person) {
await new Promise((resolve) => setTimeout(resolve, 10));
computeVia: function (this: Person) {
return `${this.firstName ?? ''} ${this.lastName ?? ''}`;
},
});
Expand Down
Loading

0 comments on commit fec5e23

Please sign in to comment.