poll Plugin
Poll until a condition is satisfied or a timeout occurs
poll plugin
poll pluginPoll until a condition is satisfied or a timeout occurs.
| Poll |
|---|
namespace poll |
| timing utility |
Configuration
poll: {
retries: 600,
interval: 100 // millis
}poll.retries
poll.retries- Type
number - Optional
- Default value
600
The maximum number of retries before giving up. This config option can be overridden by passing a retries option to the .poll() method.
poll.interval
poll.interval- Type
number - Optional
- Default value
100
How frequently to poll, in milliseconds. This config option can be overridden by passing an interval option to the .poll() method.
Plugin API
poll(options: PollOptions): void
poll(options: PollOptions): voidPoll until a condition becomes true, or until a timeout occurs.
parameters
- options
PollOptions- the polling options
returns
void
examples
jstag.use(plugin => {
plugin.poll({
property: 'jQuery',
success() {
console.log('jQuery is loaded');
},
failure() {
console.error("Couldn't load jQuery");
}
});
});Exhibit: Using the property option to poll for jQuery to load.
jstag.use(plugin => {
plugin.poll({
property: 'currentUser',
receiver: window.MyCoolApp,
success() {
console.log('the user information is loaded from the API');
},
failure() {
console.error("Couldn't load user information");
}
});
});Exhibit: Using the property config option in concert with the receiver option to poll for a hypothetical Single Page App's user context to be loaded asynchronously from the backend.
Interfaces
PollOptions
PollOptionsProperties
property
property- Type
string - Optional
- Defaults to
undefined
A property to poll for. Simple use-cases of polling usually involve simply waiting for another library to define a property on the global object, so this option is often sufficient. Optionally, a receiver can be passed separately which will be used as the lookup context for the property.
receiver
receiver- Type
any - Optional
- Defaults to
window
An optional receiver which will be used as the lookup context for the property
retries
retries- Type
number - Optional
- Defaults to
config.poll.retries
The maximum number of retries before giving up.
interval
interval- Type
number - Optional
- Defaults to
config.poll.interval
How frequently to poll, in milliseconds.
success
success- Type
PollCallback - Optional
- Defaults to
undefined
The callback to call when the predicate is satisfied.
failure
failure- Type
PollCallback - Optional
- Defaults to
undefined
The callback to call when the retries are exhausted and the predicate has not been satisfied.
PollCallback
PollCallback- Type
() => void
Mechanism
This plugin works by using the setInterval host capability to schedule a timer. Each time the timer fires, the predicate is called. If the predicate returns true, the interval timer is canceled and the success callback is invoked. If the predicate returns false and the number of retries is reached, the interval timer is canceled and the failure callback is invoked.
Events
| Event | Fires when | Payload |
|---|---|---|
poll.started | Polling begins | None |
poll.succeeded | Polling succeeds | None |
poll.failed | Retries are exhausted before predicate is satisfied | None |
poll.stopped | Polling stops irrespective of whether the predicate is ever satisfied | None |
Updated 13 days ago
