@stone-js/pipeline
Version:
An implementation based on the Chain of Responsibility (aka CoR) design pattern.
186 lines (185 loc) • 6.93 kB
TypeScript
import { HookName, MixedPipe, Promiseable, PipeExecutor, PipelineOptions, PipelineHookListener } from './declarations';
/**
* Class representing a Pipeline.
*
* @template T - The type of the passable object in the pipeline.
* @template R - The type of the return value from the pipeline execution.
*
* This class is responsible for managing and executing a series of operations
* on a set of passable values through multiple configurable pipes.
*/
export declare class Pipeline<T = unknown, R = T, Args extends any[] = any[]> {
/** The passable objects sent through the pipeline */
private passable?;
/** The method name to call on each pipe */
private method;
/** Flag indicating whether the pipeline should run synchronously or asynchronously */
private isSync;
/** The default priority for the pipes in the pipeline */
private _defaultPriority;
/** The raw pipes registered via `through`/`pipe`, normalized and sorted at execution time. */
private rawPipes;
/** The sorted metadata pipes that will be executed (computed at `then` time). */
private sortedMetaPipes;
/** The pipeline hooks */
private readonly hooks;
/** The resolver function used to resolve pipes before they are executed in the pipeline. */
private readonly resolver?;
/**
* Create a pipeline instance.
*
* @param options - Optional Pipeline options.
* @returns The pipeline instance.
*/
static create<T = unknown, R = T, Args extends any[] = any[]>(options?: PipelineOptions<T, R, Args>): Pipeline<T, R>;
/**
* Initialize a new Pipeline instance.
*
* @param options - Optional Pipeline options.
*/
protected constructor(options?: PipelineOptions<T, R, Args>);
/**
* Set the default priority for pipes in the pipeline.
*
* @param value - The priority value to set.
* @returns The current Pipeline instance.
*/
defaultPriority(value: number): this;
/**
* Set the passable objects being sent through the pipeline.
*
* @param passable - The object to pass through the pipeline.
* @returns The current Pipeline instance.
*/
send(passable: T): this;
/**
* Set the pipes for the pipeline.
*
* @param pipes - The pipes or MetaPipe instances.
* @returns The current Pipeline instance.
*/
through(...pipes: Array<MixedPipe<T, R, Args>>): this;
/**
* Add additional pipes to the pipeline.
*
* @param {...MixedPipe} pipe - A single pipe or a list of pipes to add.
* @returns The current Pipeline instance.
*/
pipe(...pipe: Array<MixedPipe<T, R, Args>>): this;
/**
* Normalize the raw pipes into sorted meta pipes.
*
* Applies the default priority only when a pipe does not specify one (an explicit
* `priority: undefined` no longer silently overrides the default), dedupes by module
* (last occurrence wins — override semantics), then orders by priority. Order semantics
* are preserved from prior versions: higher priority runs first, and for equal priorities
* the last-registered pipe runs first.
*
* @returns The sorted meta pipes.
*/
private buildSortedPipes;
/**
* Set the method to call on each pipe.
*
* @param method - The method name to use on each pipe.
* @returns The current Pipeline instance.
*/
via(method: string): this;
/**
* Configure the pipeline to run synchronously or asynchronously.
*
* @param value - Set true for sync, false for async (default is true).
* @returns The current Pipeline instance.
*/
sync(value?: boolean): this;
/**
* Add a hook to the pipeline.
*
* @param name - The name of the hook.
* @param listener - The hook listener function.
* @returns The current Pipeline instance.
*/
on(name: HookName, listener: PipelineHookListener<T, R, Args> | Array<PipelineHookListener<T, R, Args>>): this;
/**
* Run the pipeline with a final destination callback.
*
* @param destination - The final function to execute.
* @returns The result of the pipeline, either synchronously or as a Promise.
*/
then(destination: PipeExecutor<T, R>): Promiseable<R>;
/**
* Run the pipeline and return the result.
*
* @returns The result of the pipeline, either synchronously or as a Promise.
*/
thenReturn(): Promiseable<R>;
/**
* Get the asynchronous reducer to iterate over the pipes.
*
* @returns The asynchronous reducer callback.
*/
private asyncReducer;
/**
* Get the synchronous reducer to iterate over the pipes.
*
* @returns The synchronous reducer callback.
*/
private reducer;
/**
* Resolve and execute async pipe.
*
* @param currentPipe - The current pipe to execute (class or service alias string).
* @param passable - The passable object to send through the pipe.
* @param previousPipeExecutor - The previous pipe executor in the sequence.
* @returns The result of the pipe execution.
* @throws PipelineError If the pipe cannot be resolved or the method is missing.
*/
private executeAsyncPipe;
/**
* Resolve and execute a pipe.
*
* @param currentPipe - The current pipe to execute (class or service alias string).
* @param passable - The passable object to send through the pipe.
* @param previousPipeExecutor - The previous pipe executor in the sequence.
* @returns The result of the pipe execution.
* @throws PipelineError If the pipe cannot be resolved or the method is missing.
*/
private executePipe;
/**
* Resolve pipe.
*
* @param currentPipe - The current pipe to execute (class or service alias string).
* @returns The resolved pipe instance.
* @throws PipelineError If the pipe cannot be resolved or the method is missing.
*/
private resolvePipe;
/**
* Create an instance from the provided pipe.
*
* @param currentPipe - The pipe function to create an instance from.
* @returns The created instance or an object with the method.
*/
private createInstanceFromPipe;
/**
* Validate that the required method exists on the instance.
*
* @param instance - The instance to validate.
* @param currentPipe - The current pipe being executed.
* @throws {PipelineError} If the method does not exist on the instance.
*/
private validatePipeMethod;
/**
* Execute lifecycle async hooks.
*
* @param name - The hook's name.
* @param pipe - The current pipe instance.
*/
private executeAsyncHooks;
/**
* Execute lifecycle hooks.
*
* @param name - The hook's name.
* @param pipe - The current pipe instance.
*/
private executeHooks;
}