@@ -13,21 +13,24 @@ import { ForgetRegisterError } from '@redux-model/web/core/exceptions/ForgetRegi
1313import { Model } from '@redux-model/web' ;
1414import { getHistory , setHistory } from './history' ;
1515
16+ export type RouterLocation = Location ;
17+ export type RouterAction = Action ;
18+
1619interface Data {
17- location : Location ,
18- action : Action ,
20+ location : RouterLocation ,
21+ action : RouterAction ,
1922}
2023
21- type UnsubscribeToken = string ;
22-
23- interface Subscriber {
24- path : Path ;
25- reg : RegExp ;
26- keys : Key [ ] ;
27- fn : ( params : any , location : Location , action : Action ) => void ;
28- token : UnsubscribeToken ;
24+ type Subscriber = {
25+ path : Path | object ;
26+ reg ?: RegExp ;
27+ keys ?: Key [ ] ;
28+ fn : Function ;
29+ token : string ;
2930}
3031
32+ const LISTEN_ALL = { } ;
33+
3134class RouterModel extends Model < Data > {
3235 protected isInitialized = false ;
3336
@@ -59,11 +62,27 @@ class RouterModel extends Model<Data> {
5962 this . getHistory ( ) . goForward ( ) ;
6063 } ;
6164
62- public subscribe < Params = any > ( path : Path , fn : ( params : Params , location : Location , action : Action ) => void ) : UnsubscribeToken {
65+ public listenPath < Params = Record < string , string > > (
66+ path : Path ,
67+ fn : ( params : Params , location : RouterLocation , action : RouterAction ) => void
68+ ) : string {
6369 const token = `un_${ this . pathListeners . length } _${ Math . random ( ) } ` ;
6470 const keys : Key [ ] = [ ] ;
6571 const reg = pathToRegexp ( path , keys ) ;
66- const subscriber = { path, fn, reg, keys, token } ;
72+ const subscriber : Subscriber = { path, fn, reg, keys, token } ;
73+
74+ this . pathListeners . push ( subscriber ) ;
75+
76+ if ( this . isInitialized ) {
77+ this . publishOne ( subscriber , this . data . location , this . data . action ) ;
78+ }
79+
80+ return token ;
81+ }
82+
83+ public listenAll ( fn : ( location : RouterLocation , action : RouterAction ) => void ) : string {
84+ const token = `un_${ this . pathListeners . length } _${ Math . random ( ) } ` ;
85+ const subscriber : Subscriber = { path : LISTEN_ALL , fn, token } ;
6786
6887 this . pathListeners . push ( subscriber ) ;
6988
@@ -74,7 +93,7 @@ class RouterModel extends Model<Data> {
7493 return token ;
7594 }
7695
77- public unsubscribe ( token : string ) : void {
96+ public unlisten ( token ? : string ) : void {
7897 this . pathListeners = this . pathListeners . filter ( ( item ) => {
7998 return item . token !== token ;
8099 } ) ;
@@ -134,26 +153,30 @@ class RouterModel extends Model<Data> {
134153 return super . register ( ) ;
135154 }
136155
137- protected publishAll ( location : Location , action : Action ) {
156+ protected publishAll ( location : RouterLocation , action : RouterAction ) {
138157 this . pathListeners . forEach ( ( subscriber ) => {
139158 this . publishOne ( subscriber , location , action ) ;
140159 } ) ;
141160 }
142161
143- protected publishOne ( { fn, reg, keys } : Subscriber , location : Location , action : Action ) {
144- const result = reg . exec ( location . pathname ) ;
162+ protected publishOne ( subscriber : Subscriber , location : RouterLocation , action : RouterAction ) {
163+ if ( subscriber . path === LISTEN_ALL ) {
164+ return subscriber . fn ( location , action ) ;
165+ }
166+
167+ const result = subscriber . reg ! . exec ( location . pathname ) ;
145168
146169 if ( result === null ) {
147170 return ;
148171 }
149172
150173 const params : Record < string , string > = { } ;
151174
152- keys . forEach ( ( { name } , index ) => {
175+ subscriber . keys ! . forEach ( ( { name } , index ) => {
153176 params [ name ] = result [ index + 1 ] ;
154177 } ) ;
155178
156- fn ( params , location , action ) ;
179+ subscriber . fn ( params , location , action ) ;
157180 }
158181
159182 protected onReducerCreated ( store ) : void {
0 commit comments