Skip to content

Commit dbad60f

Browse files
committed
feat(router): 重构导航类型和结果,统一使用 NavigationType 枚举
1 parent 624a7c0 commit dbad60f

File tree

2 files changed

+85
-87
lines changed

2 files changed

+85
-87
lines changed

packages/router/src/next/router.ts

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { rawLocationToURL } from './location';
22
import { type RouteMatchFunc, createMatcher } from './matcher';
33
import { createRoute } from './route';
4-
import type {
5-
Route,
6-
RouteInput,
7-
RouteResult,
8-
RouterOptions,
9-
RouterRawLocation
4+
import {
5+
type Navigation,
6+
type NavigationResult,
7+
NavigationType,
8+
type RouterOptions,
9+
type RouterRawLocation
1010
} from './types';
1111

1212
export class Router {
@@ -16,22 +16,8 @@ export class Router {
1616
this.options = options;
1717
this.matcher = createMatcher(options.routes);
1818
}
19-
private _update(input: RouteInput): Promise<RouteResult> {
20-
// switch (input.type) {
21-
// case 'push':
22-
// break;
23-
// // case 'pushLayer':
24-
// // const result = await XXX('')
25-
// // if (XXXX) {
26-
// // return this._update({})
27-
// // }
28-
// case 'reload':
29-
// // 1、销毁实例,再创建实例
30-
// // 2. replace('sss') ->
31-
// break;
32-
// }
33-
}
34-
public async parseRoute(raw: RouterRawLocation): Promise<RouteResult> {
19+
private _update(input: Navigation): Promise<NavigationResult> {}
20+
public async parseRoute(raw: RouterRawLocation): Promise<NavigationResult> {
3521
const { base, normalizeURL, externalUrlHandler } = this.options;
3622
let location = rawLocationToURL(raw, base);
3723
if (normalizeURL) {
@@ -41,15 +27,15 @@ export class Router {
4127
if (location.origin !== base.origin) {
4228
externalUrlHandler?.(location);
4329
return {
44-
type: 'external'
30+
type: NavigationType.external
4531
};
4632
}
4733
// 匹配路由
4834
const matched = this.matcher(location, base);
4935
// 没有匹配任何路由
5036
if (matched.matches.length === 0) {
5137
return {
52-
type: 'notFound'
38+
type: NavigationType.notFound
5339
};
5440
}
5541
// 重新构造 URL 参数
@@ -67,54 +53,53 @@ export class Router {
6753
Object.assign(matched.params, raw.params);
6854
}
6955
const route = createRoute(raw, location, base, matched);
70-
console.log('route', route);
7156
}
7257
public push(location: RouterRawLocation) {
7358
return this._update({
74-
type: 'push',
59+
type: NavigationType.push,
7560
location
7661
});
7762
}
7863
public replace(options: RouterRawLocation) {
7964
return this._update({
80-
type: 'replace',
65+
type: NavigationType.replace,
8166
location
8267
});
8368
}
8469
public go(index: number) {
8570
return this._update({
86-
type: 'go',
71+
type: NavigationType.go,
8772
index
8873
});
8974
}
9075
public forward() {
9176
return this._update({
92-
type: 'forward'
77+
type: NavigationType.forward
9378
});
9479
}
9580
public back() {
9681
return this._update({
97-
type: 'back'
82+
type: NavigationType.back
9883
});
9984
}
10085
public pushLayer() {
10186
return this._update({
102-
type: 'pushLayer'
87+
type: NavigationType.pushLayer
10388
});
10489
}
10590
public openWindow() {
10691
return this._update({
107-
type: 'openWindow'
92+
type: NavigationType.openWindow
10893
});
10994
}
11095
public reload() {
11196
return this._update({
112-
type: 'reload'
97+
type: NavigationType.reload
11398
});
11499
}
115100
public forceReload() {
116101
return this._update({
117-
type: 'forceReload'
102+
type: NavigationType.forceReload
118103
});
119104
}
120105
}

packages/router/src/next/types.ts

Lines changed: 66 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,99 @@
1-
// 路由输入
1+
export enum NavigationType {
2+
push = 'push',
3+
replace = 'replace',
4+
go = 'go',
5+
forward = 'forward',
6+
back = 'back',
7+
pushLayer = 'pushLayer',
8+
openWindow = 'openWindow',
9+
reload = 'reload',
10+
forceReload = 'forceReload',
11+
// 导航结果类型
12+
aborted = 'aborted',
13+
redirect = 'redirect',
14+
external = 'external',
15+
notFound = 'notFound'
16+
}
217

3-
export interface RouteInputPush {
4-
type: 'push';
18+
export interface NavigationPush {
19+
type: NavigationType.push;
520
location: RouterRawLocation;
621
}
7-
export interface RouteInputReplace {
8-
type: 'replace';
22+
export interface NavigationReplace {
23+
type: NavigationType.replace;
924
location: RouterRawLocation;
1025
}
11-
export interface RouteInputGo {
12-
type: 'go';
26+
export interface NavigationGo {
27+
type: NavigationType.go;
1328
index: number;
1429
}
1530

16-
export interface RouteInputForward {
17-
type: 'forward';
31+
export interface NavigationForward {
32+
type: NavigationType.forward;
1833
}
19-
export interface RouteInputBack {
20-
type: 'back';
34+
export interface NavigationBack {
35+
type: NavigationType.back;
2136
}
2237

23-
export interface RouteInputPushLayer {
24-
type: 'pushLayer';
38+
export interface NavigationPushLayer {
39+
type: NavigationType.pushLayer;
2540
}
26-
export interface RouteInputOpenWindow {
27-
type: 'openWindow';
41+
export interface NavigationOpenWindow {
42+
type: NavigationType.openWindow;
2843
name?: string;
2944
windowFeatures?: string;
3045
}
3146

32-
export interface RouteInputReload {
33-
type: 'reload';
47+
export interface NavigationReload {
48+
type: NavigationType.reload;
3449
location?: RouterRawLocation;
3550
}
3651

37-
export interface RouteInputForceReload {
38-
type: 'forceReload';
52+
export interface NavigationForceReload {
53+
type: NavigationType.forceReload;
3954
url?: string;
4055
}
4156

42-
export type RouteInput =
43-
| RouteInputPush
44-
| RouteInputReplace
45-
| RouteInputGo
46-
| RouteInputForward
47-
| RouteInputBack
48-
| RouteInputPushLayer
49-
| RouteInputOpenWindow
50-
| RouteInputReload
51-
| RouteInputForceReload;
52-
53-
// 路由输出
54-
55-
export interface RouteResultPush {
56-
type: 'push';
57+
export type Navigation =
58+
| NavigationPush
59+
| NavigationReplace
60+
| NavigationGo
61+
| NavigationForward
62+
| NavigationBack
63+
| NavigationPushLayer
64+
| NavigationOpenWindow
65+
| NavigationReload
66+
| NavigationForceReload;
67+
68+
export interface NavigationResultPush {
69+
type: NavigationType.push;
5770
}
58-
export interface RouteResultReplace {
59-
type: 'replace';
71+
export interface NavigationResultReplace {
72+
type: NavigationType.replace;
6073
}
61-
export interface RouteResultAborted {
62-
type: 'aborted';
74+
export interface NavigationResultAborted {
75+
type: NavigationType.aborted;
6376
}
64-
export interface RouteResultRedirect {
65-
type: 'redirect';
77+
export interface NavigationResultRedirect {
78+
type: NavigationType.redirect;
6679
}
67-
export interface RouteResultUpdateIndex {
68-
type: 'redirect';
80+
export interface NavigationResultUpdateIndex {
81+
type: NavigationType.redirect;
6982
}
70-
export interface RouteResultExternal {
71-
type: 'external';
83+
export interface NavigationResultExternal {
84+
type: NavigationType.external;
7285
}
73-
export interface RouteResultNotFound {
74-
type: 'notFound';
86+
export interface NavigationResultNotFound {
87+
type: NavigationType.notFound;
7588
}
7689

77-
export type RouteResult =
78-
| RouteResultPush
79-
| RouteResultReplace
80-
| RouteResultAborted
81-
| RouteResultRedirect
82-
| RouteResultExternal
83-
| RouteResultNotFound;
90+
export type NavigationResult =
91+
| NavigationResultPush
92+
| NavigationResultReplace
93+
| NavigationResultAborted
94+
| NavigationResultRedirect
95+
| NavigationResultExternal
96+
| NavigationResultNotFound;
8497

8598
// 构造实例选项
8699
export interface RouterOptions {

0 commit comments

Comments
 (0)