-
If a component could have a parent with a |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Async is determined by the provider; the consumer has no knowledge of how the provider gets its data. In these cases it's probably best to give the consumer a default. @controller
@providable
class ServerState extends HTMLElement {
@provideAsync get hitCount(): Promise<number> {
return (async () => {
const res = await fetch('/hitcount')
const json = await res.json()
return json.hits
})()
}
}
@controller
class HitCount extends HTMLElement {
@consume hitCount = 0;
} If you want your consumer to have side-effects when it is set - as well as having a default - it's not quite so straightforward. One way to do this would be to encapsulate the value as a private field: @controller
@providable
class ServerState extends HTMLElement {
@provideAsync get hitCount(): Promise<number> {
return (async () => {
const res = await fetch('/hitcount')
const json = await res.json()
return json.hits
})()
}
}
@controller
class HitCount extends HTMLElement {
#hitCount = 0;
get hitCount() {
return this.#hitCount
}
@consume set hitCount(value: number) {
this.#hitCount = 0;
// Trigger a side effect on change
this.render();
}
render() {
this.textContent = `${this.hitCount} hits!`;
}
} I hope this answers your question sufficiently. |
Beta Was this translation helpful? Give feedback.
Async is determined by the provider; the consumer has no knowledge of how the provider gets its data. In these cases it's probably best to give the consumer a default.
If you want your consumer to have side-effects when it is set - as well as having a default - it's not quite so straightforward. One way to do this would be to encapsulate the value as a private …