@dvcol/common-utils
Version:
Typescript library for common utility functions and constants
95 lines (93 loc) • 4.09 kB
TypeScript
/**
* Represents a cancellable promise with a cancellation function.
*
* @template T - The type of the promise result.
*/
declare class CancellablePromise<T> extends Promise<T> {
private readonly _controller;
private readonly _debug;
readonly id: string;
/**
* Gets the AbortSignal associated with the AbortController, allowing external code to listen for cancellation events.
*
* @type {AbortSignal}
* @memberof CancellableFetch
*/
get signal(): AbortSignal;
/**
* Sets the AbortSignal associated with the AbortController, allowing external code to listen for cancellation events.
* @param signal - The AbortSignal to associate with the AbortController.
*
* @memberof CancellableFetch
*/
set signal(signal: AbortSignal);
/**
* Cancels the currently active fetch request by aborting the associated AbortController.
*
* @param {unknown} [reason] - An optional reason for aborting the fetch request.
* @memberof CancellableFetch
*/
cancel(reason?: unknown): void;
then<TResult1 = T, TResult2 = never>(...params: Parameters<Promise<T>['then']>): CancellablePromise<TResult1 | TResult2>;
catch<TResult = never>(onRejected?: ((reason: any) => PromiseLike<TResult> | TResult) | undefined | null): CancellablePromise<T | TResult>;
finally(onFinally?: (() => void) | undefined | null): CancellablePromise<T>;
static isCancellable<T>(promise: Promise<T> | CancellablePromise<T>): promise is CancellablePromise<T>;
static resolve<T>(value?: T): CancellablePromise<void>;
static resolve<T>(value: T): CancellablePromise<Awaited<T>>;
static from<T>(promise: Promise<T> | CancellablePromise<T>): CancellablePromise<T>;
}
/**
* A wrapper class for making cancellable fetch requests using the Fetch API.
*
* @class CancellableFetch
*/
declare class CancellableFetch {
private readonly _controller;
private readonly _debug;
readonly id: string;
/**
* Gets the AbortSignal associated with the AbortController, allowing external code to listen for cancellation events.
*
* @readonly
* @type {AbortSignal}
* @memberof CancellableFetch
*/
get signal(): AbortSignal;
/**
* Creates an instance of CancellableFetch.
*
* @param {boolean} [debug=false] - Indicates whether debug information should be logged when a fetch request is cancelled.
* @memberof CancellableFetch
*/
constructor(debug?: boolean);
/**
* Performs a fetch request with optional cancellation support.
*
* @param {RequestInfo | URL} input - The resource URL or a Request object.
* @param {Omit<RequestInit, 'signal'>} [init] - Optional RequestInit options, excluding the 'signal' property.
* @returns {Promise<Response>} A Promise that resolves to the Response to that request.
* @memberof CancellableFetch
* @throws {Error} If an error occurs during the fetch operation, other than an 'AbortError'.
*/
fetch(input: RequestInfo | URL, init?: Omit<RequestInit, 'signal'>): Promise<Response>;
/**
* Performs a fetch request using a new instance of CancellableFetch.
*
* @static
* @template T - The type of the promise result.
* @param {RequestInfo | URL} input - The resource URL or a Request object.
* @param {Omit<RequestInit, 'signal'>} [init] - Optional RequestInit options, excluding the 'signal' property.
* @returns {CancellablePromise<T>} A cancellable promise that resolves to the Response to the fetch request.
* @memberof CancellableFetch
*/
static fetch<T extends Response = Response>(input: RequestInfo | URL, init?: Omit<RequestInit, 'signal'>): CancellablePromise<T>;
/**
* Cancels the currently active fetch request by aborting the associated AbortController.
*
* @param {unknown} [reason] - An optional reason for aborting the fetch request.
*
* @memberof CancellableFetch
*/
cancel(reason?: unknown): void;
}
export { CancellableFetch, CancellablePromise };