typed-patterns
Version:
Basic design patterns
70 lines • 4.32 kB
TypeScript
import { AsyncProcessor } from '../behavioral/chain-of-responsibility';
import { NameSpace } from './name.space';
import { TypedFunction } from './typed.function';
/**
* Callback function type
*/
export declare type Callback<Result extends any[] = any[], Exception extends Error = Error> = (error?: Exception | null, ...result: Result) => void;
/**
* Asynchronous worker function type
*/
export declare type AsyncWorker<Result extends any[] = any[], Exception extends Error = Error, Parameter = void> = Parameter extends void ? (callback?: Callback<Result, Exception>) => void : (parameter: Parameter, callback?: Callback<Result, Exception>) => void;
/**
* Collection of asynchronous workers
*/
export declare type WorkersCollection<Results, Exception extends Error, Parameter> = {
[key in keyof Results]: AsyncWorker<[Results[key]], Exception, Parameter>;
};
/**
* Fails specified callback
* @param [callback] Callback function
* @param error Exception object
*/
export declare function fail<Result extends any[], Exception extends Error>(callback: Callback<Result, Exception> | undefined, error: Exception): void;
/**
* Successes specified callback
* @param [callback] Callback function
* @param result Array of result items
*/
export declare function success<Result extends any[], Exception extends Error>(callback: Callback<Result, Exception> | undefined, ...result: Result): void;
/**
* Joins onSuccess and onFail callback into classical asynchronous callback function
* @param onSuccess Callback function that must be called on success result
* @param onFail Callback function that must be called on error
*/
export declare function join<Result extends any[], Exception extends Error>(onSuccess: TypedFunction<Result, void>, onFail: TypedFunction<[Exception], void>): Callback<Result, Exception>;
/**
* Returns callback that wraps array of success results into single tuple
* @param callback Callback that accepts array of results as tuple
*/
export declare function wrapCallback<Result extends any[], Exception extends Error>(callback: Callback<[Result], Exception>): Callback<Result, Exception>;
/**
* Returns callback that unwraps single tuple to an array of success results
* @param callback Callback that accepts array of success results
*/
export declare function unwrapCallback<Result extends any[], Exception extends Error>(callback: Callback<Result, Exception>): Callback<[Result], Exception>;
/**
* Returns asynchronous worker function that wraps array of results into single tuple
* @param worker Asynchronous worker function that provides array of results to a callback
*/
export declare function wrapWorker<Result extends any[], Exception extends Error>(worker: AsyncWorker<Result, Exception, void>): AsyncWorker<[Result], Exception, void>;
/**
* Returns asynchronous worker function that unwraps single result to an array of results
* @param worker Asynchronous worker function that provides single result as tuple
*/
export declare function unwrapWorker<Result extends any[], Exception extends Error>(worker: AsyncWorker<[Result], Exception, void>): AsyncWorker<Result, Exception, void>;
/**
* Collects result provided by collection of asynchronous workers
* @param collection Collection of asynchronous workers
* @see WorkersCollection
*/
export declare function collect<Results extends any[], Exception extends Error>(collection: WorkersCollection<Results, Exception, void>): AsyncWorker<[Results], Exception, void>;
export declare function collect<Results extends NameSpace, Exception extends Error>(collection: WorkersCollection<Results, Exception, void>): AsyncWorker<[Results], Exception, void>;
/**
* Returns asynchronous processor functions that uses type-guard for accepting specific context
* and delegates work to specified processor
* @param guard Type-guard function for specific context acceptance
* @param processor Asynchronous processor function for specific context
*/
export declare function guardAsyncProcessor<BaseContext, SpecificContext extends BaseContext, Result extends any[] = any[], Exception extends Error = Error>(guard: (context: BaseContext) => context is SpecificContext, processor: AsyncProcessor<SpecificContext, Result, Exception>): AsyncProcessor<BaseContext, Result, Error>;
//# sourceMappingURL=async.helpers.d.ts.map