A generic interface for a poller that periodically executes a callback and notifies subscribers of the result. The poller manages its lifecycle automatically, ensuring that polling starts when the first subscriber registers and stops when the last subscriber unsubscribes.

The poller caches the most recent value from the callback and immediately delivers it to any new subscriber without waiting for the next polling cycle. This ensures that subscribers always receive the latest available data as soon as they register.

Consumers are only concerned with subscribing to updates. The poller implementation handles polling intervals, error management, and subscriber lifecycle internally, making it simple and reliable for end users.

Example

// Add a subscriber
const unsubscribe = poller.subscribe((result) => {
console.log('Received data:', result);
});

// Remove the subscriber
unsubscribe();

Type Parameters

  • T

Hierarchy

  • Poller

Methods

Methods

  • Subscribes a callback to receive updates from the poller.

    When the first subscriber registers, the poller automatically starts polling. Any new subscriber immediately receives the latest cached value, and all subscribers are notified of subsequent polling results.

    Returns

    A function to unsubscribe from the poller.

    Parameters

    • callback: ((value?: T) => void)

      A function that will be called with the latest value from the poller callback. If no value is available, the callback will receive undefined.

        • (value?: T): void
        • Parameters

          • Optional value: T

          Returns void

    Returns (() => void)

      • (): void
      • Subscribes a callback to receive updates from the poller.

        When the first subscriber registers, the poller automatically starts polling. Any new subscriber immediately receives the latest cached value, and all subscribers are notified of subsequent polling results.

        Returns

        A function to unsubscribe from the poller.

        Returns void

  • Immediately forces the poller to trigger its callback and reset the internal timer

    Returns void