import { wrPoll } from 'ngwr/platform';wrPoll
Reloads a resource on an interval: one timer shared across the app, paused while the page is hidden, a loading resource skipped on its turn.
Installation
Import the class. This entry point has no stylesheet.
Usage
Takes resource(), rxResource() and httpResource() alike. Needs an injection context and stops with it. Lives in ngwr/platform, since it reads WrPlatform.pageVisible.
import { httpResource } from '@angular/common/http';
import { wrPoll } from 'ngwr/platform';
@Component({
template: `<wr-table [items]="rows()" [loading]="orders.isLoading()" />`,
})
export class Orders {
readonly orders = httpResource<Order[]>(() => '/api/orders');
readonly rows = wrResourceValue(this.orders, []);
constructor() {
wrPoll(this.orders, 15_000);
}
}Rules
| When | What happens |
|---|---|
| Two resources, two intervals | One timer, aimed at the earliest due resource. |
| The tab is hidden | Polling pauses. On return, a resource overdue by its interval reloads once. |
| Still loading on its turn | Skipped, not queued: it waits a full interval again. |
In the error state | Keeps polling. That is how it recovers. |
| On the server | Nothing is scheduled, so prerender never waits. |
API
| Name | Description | Type | Default |
|---|---|---|---|
wrPoll(resource, intervalMs) | Reloads resource every intervalMs while the page is visible, on the one shared timer. Needs an injection context and stops with it. Throws in dev mode on an interval that is not positive. | <T>(resource: WritableResource<T>, intervalMs: number) => void | - |