UNPKG

pollsky

Version:

Chained Polling Library for Node.js: Friendly API with no external dependencies.

105 lines (104 loc) 2.99 kB
import { TimeUnit } from "./types"; /** * ⛓ Chained Polling Library for Node.js * @public */ declare class Pollsky<T> { /** * Asynchronous function to be polled until `conditionFn()` has returned `true` value * or, if defined, until the master timeout has been called. */ private asyncFn; /** * The number of times the `asyncFn()` has invoked before `conditionFn()` * returns true. */ private retries; /** * */ private failures; /** * Time between the end of last retry and the next one. * @default { value: 1000, unit: 'milliseconds' } */ private pollingInterval; /** * How long `asyncFn()` is invoked until the master timeout has been called. */ private atMostDuration?; /** * Timeout object of master timeout. */ private atMostTimeout?; /** * */ private atMostTimeoutToBeCalled?; /** * Duration of "at least condition" that makes polling continue even if * the `conditionFn` is already met. */ private atLeastDuration?; /** * Timeout object of at least condition. */ private atLeastTimeout?; /** * Instructs Pollsky to ignore errors thrown by `asyncFn`. */ private isIgnoreErrors; /** * Instructs Pollsky to return a value even if master timeout is called. */ private isDontThrowError; /** * Static method created only to be exported, and thus achieve "cleaner" API. */ static wait<T>(asyncFn: () => Promise<T>): Pollsky<T>; private constructor(); /** * Starts polling. */ until(conditionFn: (value: T) => boolean): Promise<T>; /** * Stops polling with failure when timeout is called. */ atMost(interval: number, unit: TimeUnit): Pollsky<T>; /** * Makes polling wait at least a certain amount of time until the result is returned. */ atLeast(interval: number, unit: TimeUnit): Pollsky<T>; /** * Changes the default interval duration. */ withInterval(interval: number, unit: TimeUnit): Pollsky<T>; /** * Causes errors thrown by `asyncFn` being ignored. */ dontThrowError(): Pollsky<T>; /** * Causes errors thrown by `asyncFn` being ignored. */ ignoreErrors(): Pollsky<T>; /** * The core method in this project, it basically contains the entire logic * required for polling. Execute the given `asyncFn` function, checks if * all of the conditions are met and handles the errors if needed be. */ private poll; /** * Performs a single execution of `asyncFn` and wraps it with `try catch` * in order to throw custom error and handle it properly. */ private executeAsyncFn; /** * Checks if all conditions are met and if not, throws a custom error. */ private checkConditions; /** * Waits certain amout of time. */ private wait; } export default Pollsky;