This repository was archived by the owner on Jun 23, 2019. It is now read-only.

Description
Description
On the assumption of polling always happens on a defined function at an interval, a callback consumed for polling will have no received arguments. But this could not be the case for certain use cases where polling could be done with different value of offsets, say, fire requests for the next 100 items then the next 200 items.
Initial implementation
const poller = new PollingObserver((data /**, list, observer */) => {
return 'complete' === data.status || data.items.length > 99;
});
const pollingFn = async () => {
const r = await fetch('https://example.com/some-api');
return r.json();
};
const opts = { interval: 2e3, timeout: 30e3 };
poller.observe(pollingFn, opts);
Proposed implementation with feature to pass arguments from conditionCallback
const poller = new PollingObserver((data /**, list, observer */) => {
const { totalCount, status, items } = data;
const done = 'complete' === status || items.length > 99;
/**
* `done` {@type boolean} If true, the polling should stop.
* `value` {@type any} Arguments to be passed to polling callback on each execution.
*/
return { done, value: { totalCount } };
});
const pollingFn = async (args) => {
const { totalCount } = args; // Destructure `totalCount` sent from `conditionCallback`
const r = await fetch(`https://example.com/some-api?from=${totalCount || 0}`);
return r.json();
};
const opts = { interval: 2e3, timeout: 30e3 };
poller.observe(pollingFn, opts);