Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bf23e58

Browse files
committed
add docs for provideAsync
1 parent f732fcd commit bf23e58

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

docs/_guide/providable.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The [Provider pattern](https://www.patterns.dev/posts/provider-pattern/) allows
88

99
Say for example a set of your components are built to perform actions on a user, but need a User ID. One way to handle this is to set the User ID as an attribute on each element, but this can lead to a lot of duplication. Instead these actions can request the ID from a parent component, which can provide the User ID without creating an explicit relationship (which can lead to brittle code).
1010

11-
The `@providable` ability allows a Catalyst controller to become a provider or consumer (or both) of one or many properties. To provide a property to nested controllers that ask for it, mark a property as `@provide`. To consume a property from a parent, mark a property as `@consume`. Let's try implementing the user actions using `@providable`:
11+
The `@providable` ability allows a Catalyst controller to become a provider or consumer (or both) of one or many properties. To provide a property to nested controllers that ask for it, mark a property as `@provide` or `@provideAsync`. To consume a property from a parent, mark a property as `@consume`. Let's try implementing the user actions using `@providable`:
1212

1313
```typescript
1414
import {providable, consume, provide, controller} from '@github/catalyst'
@@ -60,6 +60,8 @@ class UserRow extends HTMLElement {
6060
</user-row>
6161
```
6262

63+
### Combining Providables with Attributes
64+
6365
This shows how the basic pattern works, but `UserRow` having fixed strings isn't very useful. The `@provide` decorator can be combined with other decorators to make it more powerful, for example `@attr`:
6466

6567
```typescript
@@ -83,6 +85,8 @@ class UserRow extends HTMLElement {
8385
</user-row>
8486
```
8587

88+
### Providing advanced values
89+
8690
Values aren't just limited to strings, they can be any type; for example functions, classes, or even other controllers! We could implement a custom dialog component which exists as a sibling and invoke it using providers and `@target`:
8791

8892

@@ -142,4 +146,38 @@ class FollowUser extends HTMLElement {
142146
</user-list>
143147
```
144148

149+
### Asynchronous Providers
150+
151+
Sometimes you might want to have a provider do some asynchronous work - such as fetch some data over the network, and only provide the fully resolved value. In this case you can use the `@provideAsync` decorator. This decorator resolves the value before giving it to the consumer, so the consumer never deals with the Promise!
152+
153+
```ts
154+
import {providable, consume, provideAsync, target, attr, controller} from '@github/catalyst'
155+
156+
@controller
157+
@providable
158+
class ServerState extends HTMLElement {
159+
@provideAsync get hitCount(): Promise<number> {
160+
return (async () => {
161+
const res = await fetch('/hitcount')
162+
const json = await res.json()
163+
return json.hits
164+
})()
165+
}
166+
}
167+
168+
@controller
169+
class HitCount extends HTMLElement {
170+
@consume set hitCount(count: number) {
171+
this.innerHTML = html`${count} hits!`
172+
}
173+
}
174+
```
175+
```html
176+
<server-state>
177+
<hit-count>
178+
Loading...
179+
</hit-count>
180+
</server-state>
181+
```
182+
145183
If you're interested to find out how the Provider pattern works, you can look at the [context community-protocol as part of webcomponents-cg](https://github.com/webcomponents-cg/community-protocols/blob/main/proposals/context.md).

0 commit comments

Comments
 (0)