@daiso-tech/core
Version:
The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.
89 lines (88 loc) • 3.07 kB
TypeScript
/**
* @module Async
*/
import type { TimeSpan } from "../../../utilities/_module-exports.js";
import { type AsyncMiddlewareFn, type HookContext } from "../../../utilities/_module-exports.js";
import { type Invokable } from "../../../utilities/_module-exports.js";
/**
*
* IMPORT_PATH: `"@daiso-tech/core/async"`
* @group Middleware
*/
export type OnTimeoutData<TParameters extends unknown[] = unknown[], TContext extends HookContext = HookContext> = {
maxTime: TimeSpan;
args: TParameters;
context: TContext;
};
/**
*
* IMPORT_PATH: `"@daiso-tech/core/async"`
* @group Middleware
*/
export type OnTimeout<TParameters extends unknown[] = unknown[], TContext extends HookContext = HookContext> = Invokable<[data: OnTimeoutData<TParameters, TContext>]>;
/**
*
* IMPORT_PATH: `"@daiso-tech/core/async"`
* @group Middleware
*/
export type AbortSignalBinder<TParameters extends unknown[] = unknown[]> = Invokable<[arguments_: TParameters, signal: AbortSignal], TParameters>;
/**
*
* IMPORT_PATH: `"@daiso-tech/core/async"`
* @group Middleware
*/
export type TimeoutSettings<TParameters extends unknown[] = unknown[], TContext extends HookContext = HookContext> = {
time: TimeSpan;
signalBinder?: AbortSignalBinder<TParameters>;
/**
* Callback function that will be called when the timeout occurs.
*/
onTimeout?: OnTimeout<TParameters, TContext>;
};
/**
* The `timeout` middleware automatically cancels functions after a specified time period, throwing an error when aborted.
* Note the original function continues executing (even if the promise fails), you'll need to provide a settings.signalBinder to forward the `AbortSignal`.
*
* IMPORT_PATH: `"@daiso-tech/core/async"`
* @group Middleware
*
* @throws {TimeoutAsyncError} {@link TimeoutAsyncError}
*
* @example
* ```ts
* import { timeout } from "@daiso-tech/core/async";
* import { AsyncHooks, TimeSpan } from "@daiso-tech/core/utilities";
*
* const abortController = new AbortController();
*
* const promise = new AsyncHooks(async (url: string, signal?: AbortSignal): Promise<unknown> => {
* const response = await fetch(url, {
* signal
* });
* const json = await response.json();
* if (!response.ok) {
* throw json
* }
* return json;
* }, timeout({
* time: TimeSpan.fromSeconds(2),
* // With the defined signalBinder the HTTP request will be arboted when timed out or when the inputed `AbortSignal` is called.
* signalBinder: ([url, fetchSignal], timeoutSignal) => {
* return [
* url,
* AbortSignal.any([
* fetchSignal,
* timeoutSignal
* ].filter(signal => signal !== undefined))
* ] as const;
* }
* }))
* .invoke("ENDPOINT", abortController.signal);
*
* abortController.abort();
*
* // An error will be thrown.
* await promise;
* ```
*/
export declare function timeout<TParameters extends unknown[], TReturn, TContext extends HookContext>(settings: NoInfer<TimeoutSettings<TParameters, TContext>>): AsyncMiddlewareFn<TParameters, TReturn, TContext>;