UtilAdded in v15.5Optional

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.

import { wrPoll } from 'ngwr/platform';

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);
  }
}
loads: 1
Every two seconds while this tab is visible. Switch away for a while: the count stops, then moves once on return.

Rules

WhenWhat happens
Two resources, two intervalsOne timer, aimed at the earliest due resource.
The tab is hiddenPolling pauses. On return, a resource overdue by its interval reloads once.
Still loading on its turnSkipped, not queued: it waits a full interval again.
In the error stateKeeps polling. That is how it recovers.
On the serverNothing is scheduled, so prerender never waits.

API

NameDescriptionTypeDefault
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-

See also