@riogz/router
Version:
A simple, lightweight, powerful, view-agnostic, modular and extensible router
44 lines (43 loc) • 1.35 kB
TypeScript
import { Router } from '../types/router';
/**
* Enhances a router with lifecycle management capabilities.
*
* This module provides functionality for:
* - Starting the router with initial state
* - Stopping the router and cleaning up
* - Managing router started state
* - Handling initial navigation and default routes
* - Error handling for startup scenarios
*
* The router lifecycle includes:
* 1. Start: Initialize router with a path, state, or default route
* 2. Running: Router is active and handling navigation
* 3. Stop: Router is stopped and state is cleared
*
* @template Dependencies - Type of dependencies available in the router
* @param router - Router instance to enhance with lifecycle capabilities
* @returns Enhanced router with start/stop functionality
*
* @example
* ```typescript
* // Start with current browser path
* router.start('/users/123', (err, state) => {
* if (err) {
* console.error('Failed to start router:', err)
* } else {
* console.log('Router started with state:', state)
* }
* })
*
* // Start with state object
* router.start({
* name: 'user',
* params: { id: '123' },
* path: '/users/123'
* })
*
* // Stop the router
* router.stop()
* ```
*/
export default function withRouterLifecycle<Dependencies>(router: Router<Dependencies>): Router<Dependencies>;