UNPKG

@riogz/router

Version:

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

42 lines (41 loc) 1.43 kB
import { Router } from '../types/router'; /** * Enhances a router with observable capabilities and event management. * * This module provides: * - Event listener management (add/remove/invoke) * - Observable pattern implementation * - Subscription management for route changes * - RxJS compatibility through $$observable symbol * * The router emits events for: * - Router start/stop * - Transition start/success/error/cancel * - Route changes * * Supports both function-based and object-based observers for RxJS compatibility. * * @template Dependencies - Type of dependencies available in the router * @param router - Router instance to enhance with observable capabilities * @returns Enhanced router with event and subscription functionality * * @example * ```typescript * // Subscribe to route changes * const unsubscribe = router.subscribe(({ route, previousRoute }) => { * console.log(`Navigated from ${previousRoute?.name} to ${route.name}`) * }) * * // Listen to specific events * router.addEventListener('$$success', (toState, fromState) => { * console.log('Navigation successful') * }) * * // RxJS-style subscription * const subscription = router.subscribe({ * next: ({ route }) => console.log('New route:', route.name) * }) * subscription.unsubscribe() * ``` */ export default function withObservability<Dependencies>(router: Router<Dependencies>): Router<Dependencies>;