import { genString } from 'crayon-kit' ; import { History } from '../platform/history' ; import { Location } from "../platform/locator" ; import { RouteMap } from "../platform/route-map" ; import { getSharedState , SharedState , setSharedState } from '../platform/shared-state' ; import { Router , RouterEventType } from "../platform/router" ; export const create = ( id = genString . ofLength ( 10 ) , _window = window , sharedState ? ) : Router => { if ( sharedState === undefined ) { sharedState = getSharedState ( _window ) ; } if ( sharedState === undefined ) { const history = new History ( _window ) ; sharedState = new SharedState ( history ) ; setSharedState ( sharedState ) ; } const history = sharedState . history ; const routeMap = new RouteMap ( ) ; const locator = new Location ( _window ) ; const router = new Router ( id , locator , routeMap , history , sharedState . events ) ; sharedState . addRouter ( router ) ; onRouterDestroy ( router , sharedState ) ; return router ; } ; const onRouterDestroy = async ( router , sharedState ) : Promise < void > => { await sharedState . events . first ( event => event . type === RouterEventType . Destroyed && event . id === router . id ) ; sharedState ! . removeRouter ( router ) ; } ;