UNPKG

2.16 kBTypeScriptView Raw
1import "./concat";
2import "./endonerror";
3import "./filter";
4import "./flatmap";
5import Observable from "./observable";
6import { EventStream } from "./observable";
7export interface RetryContext {
8 error: any;
9 retriesDone: number;
10}
11/**
12 * Options object for [Bacon.retry](../globals.html#retry).
13 */
14export interface RetryOptions<V> {
15 /**
16 * Required. A function that produces an Observable. The function gets attempt number (starting from zero) as its argument.
17 */
18 source: (attemptNumber: number) => Observable<V>;
19 /**
20 * Required. The number of times to retry the `source` function _in addition to the initial attempt_. The default value is 0 (zero) for retrying indefinitely.
21 */
22 retries?: number;
23 /**
24 * Optional. A function that returns the time in milliseconds to wait before retrying. Defaults to `0`. The function is given a context object with the keys ```error``` (the error that occurred) and `retriesDone` (the number of retries already performed) to help determine the appropriate delay e.g. for an incremental backoff.
25 */
26 delay?(context: RetryContext): number;
27 /**
28 * Optional. A function returning `true` to continue retrying, `false` to stop. Defaults to `true`. The error that occurred is given as a parameter. For example, there is usually no reason to retry a 404 HTTP error, whereas a 500 or a timeout might work on the next attempt.
29 */
30 isRetryable?(error: any): boolean;
31}
32/**
33 Used to retry the call when there is an [`Error`](classes/error.html) event in the stream produced by the `source` function.
34
35 ```js
36 var triggeringStream, ajaxCall // <- ajaxCall gives Errors on network or server errors
37 ajaxResult = triggeringStream.flatMap(function(url) {
38 return Bacon.retry({
39 source: function(attemptNumber) { return ajaxCall(url) },
40 retries: 5,
41 isRetryable: function (error) { return error.httpStatusCode !== 404; },
42 delay: function(context) { return 100; } // Just use the same delay always
43 })
44})
45 ```
46 * @param options (click for details)
47 */
48export default function retry<V>(options: RetryOptions<V>): EventStream<V>;