@riogz/router
Version:
A simple, lightweight, powerful, view-agnostic, modular and extensible router
57 lines (56 loc) • 1.98 kB
TypeScript
/**
* Resolves a sequence of functions (guards, middleware, lifecycle hooks) during route transitions.
*
* This function handles the execution of async functions in sequence, managing:
* - Function execution order
* - State changes during execution
* - Error handling and propagation
* - Cancellation support
* - Promise resolution
* - Boolean return value handling
*
* Functions can return:
* - Boolean: true to continue, false to stop with error
* - Promise: resolved value determines continuation
* - State object: updates the current state
* - void: waits for done callback to be called
*
* @param functions - Array of functions or object with named functions to execute
* @param context - Execution context containing cancellation check and state information
* @param context.isCancelled - Function to check if operation was cancelled
* @param context.toState - Target state being navigated to
* @param context.fromState - Current state being navigated from
* @param context.errorKey - Optional key to include in error objects for debugging
* @param callback - Callback function called when all functions complete or an error occurs
*
* @example
* ```typescript
* // Resolve array of middleware functions
* resolve(
* [middleware1, middleware2, middleware3],
* { isCancelled, toState, fromState },
* (err, finalState) => {
* if (err) {
* console.error('Middleware failed:', err)
* } else {
* console.log('All middleware executed:', finalState)
* }
* }
* )
*
* // Resolve object of named guard functions
* resolve(
* { 'route1': guard1, 'route2': guard2 },
* { isCancelled, toState, fromState, errorKey: 'segment' },
* (err, finalState) => {
* // Handle completion
* }
* )
* ```
*/
export default function resolve(functions: any, { isCancelled, toState, fromState, errorKey }: {
isCancelled: any;
toState: any;
fromState: any;
errorKey?: any;
}, callback: any): void;