@@ -2,7 +2,7 @@ import { createBrowserRouter, createHashRouter, createMemoryRouter } from 'react
22import type { Location , RouteObject } from 'react-router-dom' ;
33import type { ElegantConstRoute } from '@ohh-889/react-auto-route' ;
44import type { BlockerFunction , RouterState } from '@remix-run/router' ;
5- import type { RouteLocationNamedRaw , RouteLocationNormalizedLoaded } from './types' ;
5+ import type { AfterEach , BeforeEach , Init , RouteLocationNamedRaw , RouteLocationNormalizedLoaded } from './types' ;
66import CreateRouterMatcher from './matcher' ;
77import { cleanParams , findParentNames , getFullPath , removeElement } from './utils/auxi' ;
88import { START_LOCATION_NORMALIZED } from './location' ;
@@ -18,18 +18,18 @@ const historyCreatorMap = {
1818
1919type HistoryCreator = typeof historyCreatorMap ;
2020
21- type Mode = keyof HistoryCreator ;
21+ export type Mode = keyof HistoryCreator ;
2222
23- type Options = Parameters < HistoryCreator [ Mode ] > [ 1 ] ;
23+ export type Options = Parameters < HistoryCreator [ Mode ] > [ 1 ] ;
2424
2525export interface RouterOptions {
2626 mode : Mode ;
2727 initRoutes : ElegantConstRoute [ ] ;
2828 opt : Options ;
2929 getReactRoutes : ( route : ElegantConstRoute ) => RouteObject ;
30- init : ( ) => Promise < void > ;
31- afterEach : any ;
32- beforeEach : any ;
30+ init : Init ;
31+ afterEach : AfterEach ;
32+ beforeEach : BeforeEach ;
3333}
3434
3535export function createRouter ( { beforeEach, initRoutes, mode, opt, getReactRoutes, init, afterEach } : RouterOptions ) {
@@ -43,13 +43,12 @@ export function createRouter({ beforeEach, initRoutes, mode, opt, getReactRoutes
4343
4444 let currentRoute = START_LOCATION_NORMALIZED ;
4545
46- // eslint-disable-next-line @typescript-eslint/no-unused-vars
47- let listeners : any ;
48-
49- reactRouter . subscribe ( afterRouteChange ) ;
46+ let listeners : ( ( ) => void ) [ ] = [ ] ;
5047
5148 reactRouter . getBlocker ( 'beforeGuard' , onBeforeRouteChange ) ;
5249
50+ reactRouter . subscribe ( afterRouteChange ) ;
51+
5352 /**
5453 * Adds React routes to the router.
5554 *
@@ -91,8 +90,6 @@ export function createRouter({ beforeEach, initRoutes, mode, opt, getReactRoutes
9190 return true ;
9291 }
9392
94- if ( nextLocation . state === 'reload' ) return false ;
95-
9693 if ( to . redirect ) {
9794 if ( to . redirect . startsWith ( '/' ) ) {
9895 if ( to . redirect === currentRoute . fullPath ) {
@@ -107,10 +104,10 @@ export function createRouter({ beforeEach, initRoutes, mode, opt, getReactRoutes
107104 }
108105 }
109106
110- return beforeEach ( to , currentRoute , next ) ;
107+ return beforeEach ( to , currentRoute , blockerOrJump ) ;
111108 }
112109
113- function next ( param ?: boolean | string | Location | RouteLocationNamedRaw ) {
110+ function blockerOrJump ( param ?: undefined | null | boolean | string | Location | RouteLocationNamedRaw ) {
114111 if ( ! param ) return false ;
115112 if ( typeof param === 'string' ) {
116113 reactRouter . navigate ( param ) ;
@@ -207,10 +204,14 @@ export function createRouter({ beforeEach, initRoutes, mode, opt, getReactRoutes
207204
208205 async function initReady ( ) : Promise < boolean > {
209206 return new Promise ( ( resolved , reject ) => {
210- init ( )
211- . then ( ( ) => {
212- reactRouter . initialize ( ) ;
213- resolved ( true ) ;
207+ init ( resolve ( reactRouter . state . location ) , blockerOrJump )
208+ . then ( res => {
209+ if ( ! res ) {
210+ reactRouter . initialize ( ) ;
211+ resolved ( true ) ;
212+ } else {
213+ reject ( new Error ( 'init failed' ) ) ;
214+ }
214215 } )
215216 . catch ( e => {
216217 reject ( e ) ;
@@ -279,12 +280,13 @@ export function createRouter({ beforeEach, initRoutes, mode, opt, getReactRoutes
279280 function resetRoute ( ) {
280281 // Resets the route matcher so it can begin matching new routes again.
281282 matcher . resetMatcher ( ) ;
283+ reactRouter . _internalSetRoutes ( initReactRoutes ) ;
282284 }
283285
284286 function subscribe ( listener : ( ) => void ) {
285- listeners = [ listener ] ;
287+ listeners = [ ... listeners , listener ] ;
286288 return ( ) => {
287- listeners = [ ] ;
289+ listeners = listeners . filter ( l => l !== listener ) ;
288290 } ;
289291 }
290292
@@ -328,3 +330,5 @@ export function createRouter({ beforeEach, initRoutes, mode, opt, getReactRoutes
328330
329331 return router ;
330332}
333+
334+ export type Router = ReturnType < typeof createRouter > ;
0 commit comments