Poller¶
shared.createPoller(fn, intervalMs, opts) builds a self-guarding interval. It never overlaps a slow async fn, and a thrown error never stops the loop.
Decision¶
| When | Use |
|---|---|
| Refreshing a status line, a queue, or a progress readout while the page is open | Poller. |
| A repeating timer on a config page | Poller, never setInterval. An interval keeps running after the page hides. |
| Watching a scheduled task | Task Progress Button. |
Usage¶
var poller = page.createPoller(function () { return shared.apiRequest('Status').then(render); }, 5000);
poller.start();
Use page.createPoller so stop is registered for viewhide. Call poller.stop() yourself when you build one with shared.createPoller.
States and Modifiers¶
- The handle is
{ start(immediate), stop, isRunning }.start()ticks at once unlessimmediateisfalse, then ticks everyintervalMs(default 3000). startandstopare idempotent.- With
pauseWhenHidden: truea tick is skipped whiledocument.hiddenis true, and one tick runs at once when the page becomes visible again.
Gotchas¶
- Errors are swallowed, so a failing endpoint fails silently. Report the failure inside
fn.
Example¶
Example Code¶
<!-- title: Poller -->
<div style="display: flex; flex-wrap: wrap; gap: 10px; align-items: center;">
<button type="button" id="btnStart" class="raised stolon-button stolon-primary stolon-small"><span>Start</span></button>
<button type="button" id="btnStop" class="raised stolon-button stolon-secondary stolon-small"><span>Stop</span></button>
<span id="ticks" class="stolon-mono">0 ticks</span>
<span id="running" class="stolon-mono">stopped</span>
</div>
<script type="module">
const base = await window.__baseReady;
const shared = base.createShared(document.getElementById('view'), 'preview-plugin', 'Preview');
let ticks = 0;
const poller = shared.createPoller(() => {
ticks++;
document.getElementById('ticks').textContent = ticks + ' ticks';
// A slow tick never overlaps the next one.
return new Promise((resolve) => setTimeout(resolve, 1500));
}, 1000);
const show = () => { document.getElementById('running').textContent = poller.isRunning() ? 'running' : 'stopped'; };
shared.bindClick('btnStart', () => { poller.start(); show(); });
shared.bindClick('btnStop', () => { poller.stop(); show(); });
</script>