UNPKG

@riogz/router

Version:

A simple, lightweight, powerful, view-agnostic, modular and extensible router

43 lines (42 loc) 1.6 kB
import { Router } from '../types/router'; /** * Enhances a router with route lifecycle management capabilities. * * This module provides functionality for: * - Route activation guards (canActivate) * - Route deactivation guards (canDeactivate) * - Route lifecycle hooks (onEnterRoute, onExitRoute, onRouteInActiveChain) * - Browser title management * - Factory and function storage for lifecycle handlers * * Route guards control whether navigation can proceed: * - canActivate: Called before entering a route * - canDeactivate: Called before leaving a route * * Lifecycle hooks provide notification points: * - onEnterRoute: Called when entering a route * - onExitRoute: Called when leaving a route * - onRouteInActiveChain: Called for routes that remain active during navigation * * @template Dependencies - Type of dependencies available to lifecycle handlers * @param router - Router instance to enhance with lifecycle capabilities * @returns Enhanced router with route lifecycle functionality * * @example * ```typescript * // Route guard * router.canActivate('admin', (router, deps) => (toState, fromState, done) => { * if (deps.auth.hasRole('admin')) { * done() // Allow navigation * } else { * done(new Error('Access denied')) // Block navigation * } * }) * * // Lifecycle hook * router.registerOnEnterRoute('user', (toState, fromState) => { * console.log(`Entering user route with ID: ${toState.params.id}`) * }) * ``` */ export default function withRouteLifecycle<Dependencies>(router: Router<Dependencies>): Router<Dependencies>;