rpc_ts
Version:
Remote Procedure Calls in TypeScript made simple
47 lines (46 loc) • 2.22 kB
TypeScript
import { BackoffOptions } from './backoff';
import { Stream } from './stream';
/**
* Retries a series of streams.
*
* @typeparam Message The type of the message transmitted by the stream.
* @param getStream A stream provider. The stream provider is called until a
* stream emits a 'complete' or 'canceled' event, or the maximum number of retries,
* as per the exponential backoff schedule, has been reached.
* @param backoffOptions Options for the exponential backoff.
* @param shouldRetry Determines whether an error should be retried (the
* function returns `true`) or the method should fail immediately.
*
* @return A stream that is in effect a "concatenation" of all the streams initiated
* by `getStream`, with special events emitted to inform on the retrying process.
*
* @example ```Typescript
* const stream = retryStream(() => streamFromArray([1, 2, 3], new Error('error)));
* stream.on('message', console.log).start();
* ```
*/
export declare function retryStream<Message>(getStream: () => Stream<Message>, backoffOptions?: Partial<BackoffOptions>, shouldRetry?: (err: Error) => boolean): RetryingStream<Message>;
/**
* A "concatenation" of all the streams initiated by [[retryStream]].
*/
export interface RetryingStream<Message> extends Stream<Message> {
start(): this;
cancel(): this;
/**
* Register an event listener.
*
* In addition to the events from the `Stream` interface, the following
* events are emitted:
*
* - `retryingError`: When an error occurred, with the following parameters:
* - `err`: The error that occurred
* - `retriesSinceLastReady`: The number of retries since a stream emitted
* the `ready` event.
* - `abandoned`: Whether the retrying stream gave up because of this error.
* - `error`: When an error occurred that made the retrying stream give up.
*/
on(event: 'ready' | 'complete' | 'canceled', callback: () => void): this;
on(event: 'message', callback: (message: Message) => void): this;
on(event: 'error', callback: (err: Error) => void): this;
on(event: 'retryingError', callback: (err: Error, retriesSinceLastReady: number, abandoned: boolean) => void): this;
}