BetaBetaSubscribes a callback to receive updates from the poller.
A new subscriber immediately receives the latest cached value, and all subscribers are notified of subsequent polling results.
The subscription is active until the returned unsubscribe function is called. Unsubscribe during component cleanup (disconnectedCallback) to prevent memory leaks and unnecessary polling when the component is removed from the DOM.
A function that will be called with the latest value
from the poller callback. If no value is available yet, the callback will
receive undefined. The callback is invoked:
A function to unsubscribe from the poller. Call this function to stop receiving updates and clean up the subscription.
import { State } from '@stencil/core';
import {
LimeWebComponentPlatform,
PlatformServiceName,
} from '@limetech/lime-web-components';
class MyComponent {
public platform: LimeWebComponentPlatform;
@State()
private taskCount: number;
private unsubscribe: () => void;
public connectedCallback() {
const http = this.platform.get(PlatformServiceName.Http);
const poller = this.platform
.get(PlatformServiceName.PollerFactory)
.create(
(): Promise<number> => http.get('my_addon/task_count'),
30_000
);
this.unsubscribe = poller.subscribe((taskCount) => {
this.taskCount = taskCount ?? 0;
});
}
public disconnectedCallback() {
this.unsubscribe();
}
}
BetaImmediately forces the poller to execute its callback.
This method triggers an out-of-band poll, resetting the internal timer so that the next regular poll happens at the full interval from now. The trigger happens asynchronously, and all subscribers are notified with the result once the poll completes.
Useful for:
import { PlatformServiceName } from '@limetech/lime-web-components';
const http = platform.get(PlatformServiceName.Http);
const poller = platform
.get(PlatformServiceName.PollerFactory)
.create(() => http.get('my_addon/import_status'), 60_000);
// Refresh now instead of waiting for the next interval
poller.trigger();
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.
Common use cases:
Example: Basic subscription and cleanup