File tree Expand file tree Collapse file tree 3 files changed +66
-11
lines changed Expand file tree Collapse file tree 3 files changed +66
-11
lines changed Original file line number Diff line number Diff line change 1
1
import { Navigation } from './navigation' ;
2
2
import { parsedOptions } from './options' ;
3
3
import { parseRoute } from './route' ;
4
- import {
5
- type NavigationAction ,
6
- type NavigationResult ,
7
- NavigationType ,
8
- type Route ,
9
- type RouteState ,
10
- type RouterOptions ,
11
- type RouterParsedOptions ,
12
- type RouterRawLocation
4
+ import { NavigationType } from './types' ;
5
+ import type {
6
+ NavigationAction ,
7
+ NavigationResult ,
8
+ RegisteredConfig ,
9
+ RegisteredConfigMap ,
10
+ Route ,
11
+ RouteState ,
12
+ RouterOptions ,
13
+ RouterParsedOptions ,
14
+ RouterRawLocation
13
15
} from './types' ;
14
16
15
17
export class Router {
@@ -259,5 +261,36 @@ export class Router {
259
261
}
260
262
public destroy ( ) {
261
263
this . _navigation . destroy ( ) ;
264
+ this . _destroyAllApp ( ) ;
265
+ this . _registeredCfgMap = { } ;
266
+ }
267
+
268
+ protected _registeredCfgMap : RegisteredConfigMap = { } ;
269
+
270
+ public register (
271
+ appName : string ,
272
+ generator : ( router : Router ) => RegisteredConfig
273
+ ) {
274
+ this . _registeredCfgMap [ appName ] = {
275
+ appName,
276
+ mounted : false ,
277
+ generator
278
+ } ;
279
+ }
280
+
281
+ protected _destroyApp ( cfg : string | RegisteredConfigMap [ string ] ) {
282
+ if ( typeof cfg === 'string' ) {
283
+ cfg = this . _registeredCfgMap [ cfg ] ;
284
+ }
285
+ if ( ! cfg ?. mounted ) return ;
286
+ cfg . config ?. destroy ?.( ) ;
287
+ cfg . mounted = false ;
288
+ cfg . config = void 0 ;
289
+ }
290
+
291
+ protected _destroyAllApp ( ) {
292
+ for ( const appType in this . _registeredCfgMap ) {
293
+ this . _destroyApp ( appType ) ;
294
+ }
262
295
}
263
296
}
Original file line number Diff line number Diff line change 1
- const isDev = process . env . NODE_ENV !== 'production' ;
2
1
const warn = ( ...message : any [ ] ) => {
3
- if ( ! isDev ) return ;
2
+ if ( process . env . NODE_ENV === 'production' ) return ;
4
3
console . warn ( '[Task Warning]' , ...message ) ;
5
4
} ;
6
5
@@ -19,6 +18,11 @@ export class Tasks<T extends Function> {
19
18
protected handlers : T [ ] = [ ]
20
19
) { }
21
20
21
+ public add ( ...handlers : T [ ] ) {
22
+ this . handlers . push ( ...handlers ) ;
23
+ return this ;
24
+ }
25
+
22
26
public status : TaskStatus = 'initial' ;
23
27
24
28
public abort ( ) {
Original file line number Diff line number Diff line change 1
1
import type { MatchFunction } from 'path-to-regexp' ;
2
+ import type { Router } from './router' ;
3
+
2
4
export enum NavigationType {
3
5
// Action 类型
4
6
push = 'push' ,
@@ -212,4 +214,20 @@ export interface RouteMatchResult {
212
214
213
215
export type RouteMatcher = ( targetURL : URL , baseURL : URL ) => RouteMatchResult ;
214
216
217
+ export interface RegisteredConfig {
218
+ mount ?: ( ) => void ;
219
+ update ?: ( ) => void ;
220
+ destroy ?: ( ) => void ;
221
+ renderToString ?: ( ) => string | Promise < string > ;
222
+ }
223
+
224
+ export type RegisteredConfigMap = {
225
+ [ AppName in string ] ?: {
226
+ appName : AppName ;
227
+ mounted : boolean ;
228
+ generator : ( router : Router ) => RegisteredConfig ;
229
+ config ?: RegisteredConfig ;
230
+ } ;
231
+ } ;
232
+
215
233
// 旧字段 + 新字段
You can’t perform that action at this time.
0 commit comments