@esmx/router 3.0.0-rc.17 → 3.0.0-rc.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +70 -0
- package/README.zh-CN.md +70 -0
- package/dist/error.d.ts +23 -0
- package/dist/error.mjs +61 -0
- package/dist/increment-id.d.ts +7 -0
- package/dist/increment-id.mjs +11 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.mjs +14 -3
- package/dist/index.test.mjs +8 -0
- package/dist/location.d.ts +15 -0
- package/dist/location.mjs +53 -0
- package/dist/location.test.d.ts +8 -0
- package/dist/location.test.mjs +370 -0
- package/dist/matcher.d.ts +3 -0
- package/dist/matcher.mjs +44 -0
- package/dist/matcher.test.mjs +1492 -0
- package/dist/micro-app.d.ts +18 -0
- package/dist/micro-app.dom.test.d.ts +1 -0
- package/dist/micro-app.dom.test.mjs +532 -0
- package/dist/micro-app.mjs +80 -0
- package/dist/navigation.d.ts +43 -0
- package/dist/navigation.mjs +143 -0
- package/dist/navigation.test.d.ts +1 -0
- package/dist/navigation.test.mjs +681 -0
- package/dist/options.d.ts +4 -0
- package/dist/options.mjs +88 -0
- package/dist/route-task.d.ts +40 -0
- package/dist/route-task.mjs +75 -0
- package/dist/route-task.test.d.ts +1 -0
- package/dist/route-task.test.mjs +673 -0
- package/dist/route-transition.d.ts +53 -0
- package/dist/route-transition.mjs +307 -0
- package/dist/route-transition.test.d.ts +1 -0
- package/dist/route-transition.test.mjs +146 -0
- package/dist/route.d.ts +72 -0
- package/dist/route.mjs +194 -0
- package/dist/route.test.d.ts +1 -0
- package/dist/route.test.mjs +1664 -0
- package/dist/router-back.test.d.ts +1 -0
- package/dist/router-back.test.mjs +361 -0
- package/dist/router-forward.test.d.ts +1 -0
- package/dist/router-forward.test.mjs +376 -0
- package/dist/router-go.test.d.ts +1 -0
- package/dist/router-go.test.mjs +73 -0
- package/dist/router-guards-cleanup.test.d.ts +1 -0
- package/dist/router-guards-cleanup.test.mjs +437 -0
- package/dist/router-link.d.ts +10 -0
- package/dist/router-link.mjs +126 -0
- package/dist/router-push.test.d.ts +1 -0
- package/dist/router-push.test.mjs +115 -0
- package/dist/router-replace.test.d.ts +1 -0
- package/dist/router-replace.test.mjs +114 -0
- package/dist/router-resolve.test.d.ts +1 -0
- package/dist/router-resolve.test.mjs +393 -0
- package/dist/router-restart-app.dom.test.d.ts +1 -0
- package/dist/router-restart-app.dom.test.mjs +616 -0
- package/dist/router-window-navigation.test.d.ts +1 -0
- package/dist/router-window-navigation.test.mjs +359 -0
- package/dist/router.d.ts +109 -102
- package/dist/router.mjs +260 -361
- package/dist/types.d.ts +246 -0
- package/dist/types.mjs +18 -0
- package/dist/util.d.ts +26 -0
- package/dist/util.mjs +53 -0
- package/dist/util.test.d.ts +1 -0
- package/dist/util.test.mjs +1020 -0
- package/package.json +10 -13
- package/src/error.ts +84 -0
- package/src/increment-id.ts +12 -0
- package/src/index.test.ts +9 -0
- package/src/index.ts +54 -3
- package/src/location.test.ts +406 -0
- package/src/location.ts +96 -0
- package/src/matcher.test.ts +1685 -0
- package/src/matcher.ts +59 -0
- package/src/micro-app.dom.test.ts +708 -0
- package/src/micro-app.ts +101 -0
- package/src/navigation.test.ts +858 -0
- package/src/navigation.ts +195 -0
- package/src/options.ts +131 -0
- package/src/route-task.test.ts +901 -0
- package/src/route-task.ts +105 -0
- package/src/route-transition.test.ts +178 -0
- package/src/route-transition.ts +425 -0
- package/src/route.test.ts +2014 -0
- package/src/route.ts +308 -0
- package/src/router-back.test.ts +487 -0
- package/src/router-forward.test.ts +506 -0
- package/src/router-go.test.ts +91 -0
- package/src/router-guards-cleanup.test.ts +595 -0
- package/src/router-link.ts +235 -0
- package/src/router-push.test.ts +140 -0
- package/src/router-replace.test.ts +139 -0
- package/src/router-resolve.test.ts +475 -0
- package/src/router-restart-app.dom.test.ts +783 -0
- package/src/router-window-navigation.test.ts +457 -0
- package/src/router.ts +289 -470
- package/src/types.ts +341 -0
- package/src/util.test.ts +1262 -0
- package/src/util.ts +116 -0
- package/dist/history/abstract.d.ts +0 -29
- package/dist/history/abstract.mjs +0 -107
- package/dist/history/base.d.ts +0 -79
- package/dist/history/base.mjs +0 -275
- package/dist/history/html.d.ts +0 -22
- package/dist/history/html.mjs +0 -183
- package/dist/history/index.d.ts +0 -7
- package/dist/history/index.mjs +0 -16
- package/dist/matcher/create-matcher.d.ts +0 -5
- package/dist/matcher/create-matcher.mjs +0 -218
- package/dist/matcher/create-matcher.spec.mjs +0 -0
- package/dist/matcher/index.d.ts +0 -1
- package/dist/matcher/index.mjs +0 -1
- package/dist/task-pipe/index.d.ts +0 -1
- package/dist/task-pipe/index.mjs +0 -1
- package/dist/task-pipe/task.d.ts +0 -30
- package/dist/task-pipe/task.mjs +0 -66
- package/dist/utils/bom.d.ts +0 -5
- package/dist/utils/bom.mjs +0 -10
- package/dist/utils/encoding.d.ts +0 -48
- package/dist/utils/encoding.mjs +0 -44
- package/dist/utils/guards.d.ts +0 -9
- package/dist/utils/guards.mjs +0 -12
- package/dist/utils/index.d.ts +0 -7
- package/dist/utils/index.mjs +0 -27
- package/dist/utils/path.d.ts +0 -60
- package/dist/utils/path.mjs +0 -281
- package/dist/utils/path.spec.mjs +0 -27
- package/dist/utils/scroll.d.ts +0 -25
- package/dist/utils/scroll.mjs +0 -59
- package/dist/utils/utils.d.ts +0 -16
- package/dist/utils/utils.mjs +0 -11
- package/dist/utils/warn.d.ts +0 -2
- package/dist/utils/warn.mjs +0 -12
- package/src/history/abstract.ts +0 -149
- package/src/history/base.ts +0 -408
- package/src/history/html.ts +0 -228
- package/src/history/index.ts +0 -20
- package/src/matcher/create-matcher.spec.ts +0 -3
- package/src/matcher/create-matcher.ts +0 -293
- package/src/matcher/index.ts +0 -1
- package/src/task-pipe/index.ts +0 -1
- package/src/task-pipe/task.ts +0 -97
- package/src/utils/bom.ts +0 -14
- package/src/utils/encoding.ts +0 -153
- package/src/utils/guards.ts +0 -25
- package/src/utils/index.ts +0 -27
- package/src/utils/path.spec.ts +0 -32
- package/src/utils/path.ts +0 -417
- package/src/utils/scroll.ts +0 -120
- package/src/utils/utils.ts +0 -30
- package/src/utils/warn.ts +0 -13
- /package/dist/{matcher/create-matcher.spec.d.ts → index.test.d.ts} +0 -0
- /package/dist/{utils/path.spec.d.ts → matcher.test.d.ts} +0 -0
|
@@ -1,293 +0,0 @@
|
|
|
1
|
-
import { compile, match, pathToRegexp } from 'path-to-regexp';
|
|
2
|
-
|
|
3
|
-
import type {
|
|
4
|
-
HistoryState,
|
|
5
|
-
RouteConfig,
|
|
6
|
-
RouteMatch,
|
|
7
|
-
RouteRecord,
|
|
8
|
-
RouterLocation,
|
|
9
|
-
RouterMatcher
|
|
10
|
-
} from '../types';
|
|
11
|
-
import {
|
|
12
|
-
decode,
|
|
13
|
-
encodePath,
|
|
14
|
-
normalizeLocation,
|
|
15
|
-
normalizePath,
|
|
16
|
-
parsePath,
|
|
17
|
-
stringifyPath
|
|
18
|
-
} from '../utils';
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* 路由匹配器
|
|
22
|
-
*/
|
|
23
|
-
class RouteMatcher {
|
|
24
|
-
/*
|
|
25
|
-
* 路由匹配规则
|
|
26
|
-
*/
|
|
27
|
-
protected routeMatches: RouteMatch[];
|
|
28
|
-
|
|
29
|
-
/*
|
|
30
|
-
* 原始路由配置
|
|
31
|
-
*/
|
|
32
|
-
// protected routes: RouteConfig[];
|
|
33
|
-
|
|
34
|
-
constructor(routes: RouteConfig[]) {
|
|
35
|
-
// this.routes = routes;
|
|
36
|
-
this.routeMatches = createRouteMatches(routes);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/*
|
|
40
|
-
* 根据配置匹配对应的路由
|
|
41
|
-
*/
|
|
42
|
-
public match(
|
|
43
|
-
rawLocation: RouterLocation,
|
|
44
|
-
{
|
|
45
|
-
base,
|
|
46
|
-
redirectedFrom
|
|
47
|
-
}: { base: string; redirectedFrom?: RouteRecord } = { base: '' }
|
|
48
|
-
): RouteRecord | null {
|
|
49
|
-
let path = '';
|
|
50
|
-
/* 按 Hanson 要求加入 undefined 类型 */
|
|
51
|
-
let query: Record<string, string | undefined> = {};
|
|
52
|
-
let queryArray: Record<string, string[]> = {};
|
|
53
|
-
let params: Record<string, string> = {};
|
|
54
|
-
let hash = '';
|
|
55
|
-
let state: HistoryState = {};
|
|
56
|
-
|
|
57
|
-
const parsedOption = parsePath(rawLocation.path);
|
|
58
|
-
path = parsedOption.pathname;
|
|
59
|
-
query = rawLocation.query || parsedOption.query || {};
|
|
60
|
-
queryArray = rawLocation.queryArray || parsedOption.queryArray || {};
|
|
61
|
-
hash = rawLocation.hash || parsedOption.hash || '';
|
|
62
|
-
state = rawLocation.state || {};
|
|
63
|
-
|
|
64
|
-
const routeMatch = this.routeMatches.find(({ match }) => {
|
|
65
|
-
return match(path);
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
if (routeMatch) {
|
|
69
|
-
const {
|
|
70
|
-
component,
|
|
71
|
-
asyncComponent,
|
|
72
|
-
compile,
|
|
73
|
-
meta,
|
|
74
|
-
redirect,
|
|
75
|
-
matched,
|
|
76
|
-
parse
|
|
77
|
-
} = routeMatch.internalRedirect || routeMatch; // 优先使用内部重定向
|
|
78
|
-
|
|
79
|
-
params = rawLocation.params || parse(path).params || {};
|
|
80
|
-
|
|
81
|
-
const realPath = normalizePath(
|
|
82
|
-
compile({
|
|
83
|
-
query,
|
|
84
|
-
queryArray,
|
|
85
|
-
params,
|
|
86
|
-
hash
|
|
87
|
-
})
|
|
88
|
-
);
|
|
89
|
-
|
|
90
|
-
const {
|
|
91
|
-
params: realParams,
|
|
92
|
-
query: realQuery,
|
|
93
|
-
queryArray: realQueryArray,
|
|
94
|
-
hash: realHash
|
|
95
|
-
} = parse(realPath);
|
|
96
|
-
|
|
97
|
-
const routeRecord = {
|
|
98
|
-
base,
|
|
99
|
-
path: normalizePath(
|
|
100
|
-
compile({
|
|
101
|
-
params: realParams
|
|
102
|
-
})
|
|
103
|
-
),
|
|
104
|
-
fullPath: realPath,
|
|
105
|
-
params: realParams,
|
|
106
|
-
query: realQuery,
|
|
107
|
-
queryArray: realQueryArray,
|
|
108
|
-
hash: realHash,
|
|
109
|
-
state,
|
|
110
|
-
component,
|
|
111
|
-
asyncComponent,
|
|
112
|
-
meta,
|
|
113
|
-
redirect,
|
|
114
|
-
redirectedFrom,
|
|
115
|
-
matched
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
if (redirect) {
|
|
119
|
-
const normalizedLocation = normalizeLocation(
|
|
120
|
-
typeof redirect === 'function'
|
|
121
|
-
? redirect(routeRecord)
|
|
122
|
-
: redirect,
|
|
123
|
-
base
|
|
124
|
-
);
|
|
125
|
-
return this.match(normalizedLocation, {
|
|
126
|
-
base,
|
|
127
|
-
redirectedFrom: routeRecord
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
return routeRecord;
|
|
131
|
-
}
|
|
132
|
-
return null;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/*
|
|
136
|
-
* 获取当前路由匹配规则
|
|
137
|
-
*/
|
|
138
|
-
public getRoutes(): RouteMatch[] {
|
|
139
|
-
return this.routeMatches;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* 新增单个路由匹配规则
|
|
144
|
-
*/
|
|
145
|
-
// public addRoute(route: RouteConfig) {
|
|
146
|
-
// this.routes.push(route);
|
|
147
|
-
// this.routeMatches = createRouteMatches(this.routes);
|
|
148
|
-
// }
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* 新增多个路由匹配规则
|
|
152
|
-
*/
|
|
153
|
-
// public addRoutes(routes: RouteConfig[]) {
|
|
154
|
-
// this.routes.push(...routes);
|
|
155
|
-
// this.routeMatches = createRouteMatches(this.routes);
|
|
156
|
-
// }
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* 创建路由匹配器
|
|
161
|
-
*/
|
|
162
|
-
export function createRouterMatcher(routes: RouteConfig[]): RouterMatcher {
|
|
163
|
-
return new RouteMatcher(routes);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* 生成打平的路由匹配规则
|
|
168
|
-
*/
|
|
169
|
-
function createRouteMatches(
|
|
170
|
-
routes: RouteConfig[],
|
|
171
|
-
parent?: RouteMatch
|
|
172
|
-
): RouteMatch[] {
|
|
173
|
-
const routeMatches: RouteMatch[] = [];
|
|
174
|
-
for (const route of routes) {
|
|
175
|
-
routeMatches.push(
|
|
176
|
-
...createRouteMatch(
|
|
177
|
-
{
|
|
178
|
-
...route,
|
|
179
|
-
path:
|
|
180
|
-
route.path instanceof Array ? route.path : [route.path]
|
|
181
|
-
},
|
|
182
|
-
parent
|
|
183
|
-
)
|
|
184
|
-
);
|
|
185
|
-
}
|
|
186
|
-
return routeMatches;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* 生成单个路由匹配规则
|
|
191
|
-
*/
|
|
192
|
-
function createRouteMatch(
|
|
193
|
-
route: RouteConfig & { path: string },
|
|
194
|
-
parent?: RouteMatch
|
|
195
|
-
): RouteMatch;
|
|
196
|
-
function createRouteMatch(
|
|
197
|
-
route: RouteConfig & { path: string[] },
|
|
198
|
-
parent?: RouteMatch
|
|
199
|
-
): RouteMatch[];
|
|
200
|
-
function createRouteMatch(
|
|
201
|
-
route: RouteConfig,
|
|
202
|
-
parent?: RouteMatch
|
|
203
|
-
): RouteMatch | RouteMatch[] {
|
|
204
|
-
const pathList = route.path instanceof Array ? route.path : [route.path];
|
|
205
|
-
const routeMatches: RouteMatch[] = pathList.reduce<RouteMatch[]>(
|
|
206
|
-
(acc, item, index) => {
|
|
207
|
-
const { children } = route;
|
|
208
|
-
const path = normalizePath(item, parent?.path);
|
|
209
|
-
let regex: RegExp;
|
|
210
|
-
try {
|
|
211
|
-
regex = pathToRegexp(path);
|
|
212
|
-
} catch (error) {
|
|
213
|
-
console.warn(
|
|
214
|
-
`@create route rule failed on path: ${path}`,
|
|
215
|
-
route
|
|
216
|
-
);
|
|
217
|
-
return acc;
|
|
218
|
-
}
|
|
219
|
-
const toPath = compile(path, { encode: encodePath });
|
|
220
|
-
const parseParams = match(path, { decode });
|
|
221
|
-
const current: RouteMatch = {
|
|
222
|
-
regex,
|
|
223
|
-
match: (path: string) => {
|
|
224
|
-
return regex.test(path);
|
|
225
|
-
},
|
|
226
|
-
parse: (
|
|
227
|
-
path: string
|
|
228
|
-
): {
|
|
229
|
-
params: Record<string, string>;
|
|
230
|
-
query: Record<string, string>;
|
|
231
|
-
queryArray: Record<string, string[]>;
|
|
232
|
-
hash: string;
|
|
233
|
-
} => {
|
|
234
|
-
const { pathname, query, queryArray, hash } =
|
|
235
|
-
parsePath(path);
|
|
236
|
-
const { params } = parseParams(pathname) || { params: {} };
|
|
237
|
-
return {
|
|
238
|
-
params: Object.assign({}, params), // parse的 params 是使用 Object.create(null) 创建的没有原型的对象,需要进行包装处理
|
|
239
|
-
query,
|
|
240
|
-
queryArray,
|
|
241
|
-
hash
|
|
242
|
-
};
|
|
243
|
-
},
|
|
244
|
-
compile: (
|
|
245
|
-
{ params = {}, query = {}, queryArray = {}, hash = '' } = {
|
|
246
|
-
params: {},
|
|
247
|
-
query: {},
|
|
248
|
-
queryArray: {},
|
|
249
|
-
hash: ''
|
|
250
|
-
}
|
|
251
|
-
) => {
|
|
252
|
-
const pathString = toPath(params);
|
|
253
|
-
return stringifyPath({
|
|
254
|
-
pathname: pathString,
|
|
255
|
-
query,
|
|
256
|
-
queryArray,
|
|
257
|
-
hash
|
|
258
|
-
});
|
|
259
|
-
},
|
|
260
|
-
path,
|
|
261
|
-
appType: route.appType || parent?.appType || '',
|
|
262
|
-
component: route.component,
|
|
263
|
-
asyncComponent: route.asyncComponent,
|
|
264
|
-
meta: route.meta || {},
|
|
265
|
-
redirect: route.redirect,
|
|
266
|
-
/**
|
|
267
|
-
* 第一个 path 作为基准,后续 path 会内部重定向到第一个 path
|
|
268
|
-
* 同时如果父路由存在内部跳转,子路由也需要处理内部跳转
|
|
269
|
-
*/
|
|
270
|
-
internalRedirect:
|
|
271
|
-
index > 0 || parent?.internalRedirect
|
|
272
|
-
? createRouteMatch(
|
|
273
|
-
{
|
|
274
|
-
...route,
|
|
275
|
-
path: pathList[0]
|
|
276
|
-
},
|
|
277
|
-
parent?.internalRedirect || parent
|
|
278
|
-
)
|
|
279
|
-
: undefined,
|
|
280
|
-
matched: [...(parent?.matched || []), route]
|
|
281
|
-
};
|
|
282
|
-
if (children && children.length > 0) {
|
|
283
|
-
acc.push(...createRouteMatches(children, current));
|
|
284
|
-
}
|
|
285
|
-
acc.push(current);
|
|
286
|
-
return acc;
|
|
287
|
-
},
|
|
288
|
-
[]
|
|
289
|
-
);
|
|
290
|
-
return route.path instanceof Array
|
|
291
|
-
? routeMatches
|
|
292
|
-
: routeMatches[routeMatches.length - 1];
|
|
293
|
-
}
|
package/src/matcher/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { createRouterMatcher } from './create-matcher';
|
package/src/task-pipe/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { Tasks, createTasks } from './task';
|
package/src/task-pipe/task.ts
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import type { Awaitable } from '../types';
|
|
2
|
-
import { warn } from '../utils';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* 创建可操作的任务队列
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
type func = (...args: any) => any;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* 任务状态
|
|
12
|
-
*/
|
|
13
|
-
export enum TaskStatus {
|
|
14
|
-
INITIAL = 'initial',
|
|
15
|
-
RUNNING = 'running',
|
|
16
|
-
FINISHED = 'finished',
|
|
17
|
-
ERROR = 'error',
|
|
18
|
-
ABORTED = 'aborted'
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export class Tasks<T extends func = func> {
|
|
22
|
-
protected handlers: T[] = [];
|
|
23
|
-
|
|
24
|
-
public add(handler: T | T[]) {
|
|
25
|
-
const params: T[] = handler instanceof Array ? handler : [handler];
|
|
26
|
-
this.handlers.push(...params);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
public reset() {
|
|
30
|
-
this.handlers = [];
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
get list() {
|
|
34
|
-
return this.handlers;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
get length() {
|
|
38
|
-
return this.handlers.length;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
status: TaskStatus = TaskStatus.INITIAL;
|
|
42
|
-
|
|
43
|
-
public async run({
|
|
44
|
-
cb,
|
|
45
|
-
final
|
|
46
|
-
}: {
|
|
47
|
-
cb?: (res: Awaited<ReturnType<T>>) => Awaitable<void>;
|
|
48
|
-
final?: () => Awaitable<void>;
|
|
49
|
-
} = {}) {
|
|
50
|
-
if (this.status !== 'initial') {
|
|
51
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
52
|
-
warn(`task start failed in status ${this.status}`);
|
|
53
|
-
}
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
this.status = TaskStatus.RUNNING;
|
|
58
|
-
|
|
59
|
-
for await (const handler of this.list) {
|
|
60
|
-
if ((this.status as TaskStatus) === TaskStatus.ABORTED) {
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
if (typeof handler === 'function') {
|
|
64
|
-
try {
|
|
65
|
-
const res = await handler();
|
|
66
|
-
cb && (await cb(res));
|
|
67
|
-
} catch (error) {
|
|
68
|
-
// if (process.env.NODE_ENV !== 'production') {
|
|
69
|
-
warn('task error:', error);
|
|
70
|
-
// }
|
|
71
|
-
this.status = TaskStatus.ERROR;
|
|
72
|
-
}
|
|
73
|
-
} else {
|
|
74
|
-
// if (process.env.NODE_ENV !== 'production') {
|
|
75
|
-
warn('task is not a function', handler);
|
|
76
|
-
// }
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
if ((this.status as TaskStatus) !== TaskStatus.RUNNING) return;
|
|
80
|
-
final && (await final());
|
|
81
|
-
this.status = TaskStatus.FINISHED;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
public abort() {
|
|
85
|
-
if (
|
|
86
|
-
process.env.NODE_ENV !== 'production' &&
|
|
87
|
-
this.status === TaskStatus.RUNNING
|
|
88
|
-
) {
|
|
89
|
-
warn('abort task when task is running');
|
|
90
|
-
}
|
|
91
|
-
this.status = TaskStatus.ABORTED;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export function createTasks<T extends func = func>() {
|
|
96
|
-
return new Tasks<T>();
|
|
97
|
-
}
|
package/src/utils/bom.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 在新窗口打开页面,如果被拦截,则会降级到当前窗口打开
|
|
3
|
-
* @param url 打开的地址
|
|
4
|
-
*/
|
|
5
|
-
export function openWindow(url: string, target?: string) {
|
|
6
|
-
try {
|
|
7
|
-
const newWindow = window.open(url, target);
|
|
8
|
-
if (!newWindow) {
|
|
9
|
-
location.href = url;
|
|
10
|
-
}
|
|
11
|
-
} catch (e) {
|
|
12
|
-
location.href = url;
|
|
13
|
-
}
|
|
14
|
-
}
|
package/src/utils/encoding.ts
DELETED
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
import { warn } from './warn';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Encoding Rules (␣ = Space)
|
|
5
|
-
* - Path: ␣ " < > # ? { }
|
|
6
|
-
* - Query: ␣ " < > # & =
|
|
7
|
-
* - Hash: ␣ " < > `
|
|
8
|
-
*
|
|
9
|
-
* On top of that, the RFC3986 (https://tools.ietf.org/html/rfc3986#section-2.2)
|
|
10
|
-
* defines some extra characters to be encoded. Most browsers do not encode them
|
|
11
|
-
* in encodeURI https://github.com/whatwg/url/issues/369, so it may be safer to
|
|
12
|
-
* also encode `!'()*`. Leaving un-encoded only ASCII alphanumeric(`a-zA-Z0-9`)
|
|
13
|
-
* plus `-._~`. This extra safety should be applied to query by patching the
|
|
14
|
-
* string returned by encodeURIComponent encodeURI also encodes `[\]^`. `\`
|
|
15
|
-
* should be encoded to avoid ambiguity. Browsers (IE, FF, C) transform a `\`
|
|
16
|
-
* into a `/` if directly typed in. The _backtick_ (`````) should also be
|
|
17
|
-
* encoded everywhere because some browsers like FF encode it when directly
|
|
18
|
-
* written while others don't. Safari and IE don't encode ``"<>{}``` in hash.
|
|
19
|
-
*/
|
|
20
|
-
// const EXTRA_RESERVED_RE = /[!'()*]/g
|
|
21
|
-
// const encodeReservedReplacer = (c: string) => '%' + c.charCodeAt(0).toString(16)
|
|
22
|
-
|
|
23
|
-
const HASH_RE = /#/g; // %23
|
|
24
|
-
const AMPERSAND_RE = /&/g; // %26
|
|
25
|
-
const SLASH_RE = /\//g; // %2F
|
|
26
|
-
const EQUAL_RE = /=/g; // %3D
|
|
27
|
-
const IM_RE = /\?/g; // %3F
|
|
28
|
-
export const PLUS_RE = /\+/g; // %2B
|
|
29
|
-
/**
|
|
30
|
-
* NOTE: It's not clear to me if we should encode the + symbol in queries, it
|
|
31
|
-
* seems to be less flexible than not doing so and I can't find out the legacy
|
|
32
|
-
* systems requiring this for regular requests like text/html. In the standard,
|
|
33
|
-
* the encoding of the plus character is only mentioned for
|
|
34
|
-
* application/x-www-form-urlencoded
|
|
35
|
-
* (https://url.spec.whatwg.org/#urlencoded-parsing) and most browsers seems lo
|
|
36
|
-
* leave the plus character as is in queries. To be more flexible, we allow the
|
|
37
|
-
* plus character on the query, but it can also be manually encoded by the user.
|
|
38
|
-
*
|
|
39
|
-
* Resources:
|
|
40
|
-
* - https://url.spec.whatwg.org/#urlencoded-parsing
|
|
41
|
-
* - https://stackoverflow.com/questions/1634271/url-encoding-the-space-character-or-20
|
|
42
|
-
*/
|
|
43
|
-
|
|
44
|
-
const ENC_BRACKET_OPEN_RE = /%5B/g; // [
|
|
45
|
-
const ENC_BRACKET_CLOSE_RE = /%5D/g; // ]
|
|
46
|
-
const ENC_CARET_RE = /%5E/g; // ^
|
|
47
|
-
const ENC_BACKTICK_RE = /%60/g; // `
|
|
48
|
-
const ENC_CURLY_OPEN_RE = /%7B/g; // {
|
|
49
|
-
const ENC_PIPE_RE = /%7C/g; // |
|
|
50
|
-
const ENC_CURLY_CLOSE_RE = /%7D/g; // }
|
|
51
|
-
const ENC_SPACE_RE = /%20/g; // }
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Encode characters that need to be encoded on the path, search and hash
|
|
55
|
-
* sections of the URL.
|
|
56
|
-
*
|
|
57
|
-
* @internal
|
|
58
|
-
* @param text - string to encode
|
|
59
|
-
* @returns encoded string
|
|
60
|
-
*/
|
|
61
|
-
function commonEncode(text: string | number): string {
|
|
62
|
-
return encodeURIComponent('' + text)
|
|
63
|
-
.replace(ENC_PIPE_RE, '|')
|
|
64
|
-
.replace(ENC_BRACKET_OPEN_RE, '[')
|
|
65
|
-
.replace(ENC_BRACKET_CLOSE_RE, ']');
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Encode characters that need to be encoded on the hash section of the URL.
|
|
70
|
-
*
|
|
71
|
-
* @param text - string to encode
|
|
72
|
-
* @returns encoded string
|
|
73
|
-
*/
|
|
74
|
-
export function encodeHash(text: string): string {
|
|
75
|
-
return commonEncode(text)
|
|
76
|
-
.replace(ENC_CURLY_OPEN_RE, '{')
|
|
77
|
-
.replace(ENC_CURLY_CLOSE_RE, '}')
|
|
78
|
-
.replace(ENC_CARET_RE, '^');
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Encode characters that need to be encoded query values on the query
|
|
83
|
-
* section of the URL.
|
|
84
|
-
*
|
|
85
|
-
* @param text - string to encode
|
|
86
|
-
* @returns encoded string
|
|
87
|
-
*/
|
|
88
|
-
export function encodeQueryValue(text: string | number): string {
|
|
89
|
-
return (
|
|
90
|
-
commonEncode(text)
|
|
91
|
-
// Encode the space as +, encode the + to differentiate it from the space
|
|
92
|
-
.replace(PLUS_RE, '%2B')
|
|
93
|
-
.replace(ENC_SPACE_RE, '+')
|
|
94
|
-
.replace(HASH_RE, '%23')
|
|
95
|
-
.replace(AMPERSAND_RE, '%26')
|
|
96
|
-
.replace(ENC_BACKTICK_RE, '`')
|
|
97
|
-
.replace(ENC_CURLY_OPEN_RE, '{')
|
|
98
|
-
.replace(ENC_CURLY_CLOSE_RE, '}')
|
|
99
|
-
.replace(ENC_CARET_RE, '^')
|
|
100
|
-
);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Like `encodeQueryValue` but also encodes the `=` character.
|
|
105
|
-
*
|
|
106
|
-
* @param text - string to encode
|
|
107
|
-
*/
|
|
108
|
-
export function encodeQueryKey(text: string | number): string {
|
|
109
|
-
return encodeQueryValue(text).replace(EQUAL_RE, '%3D');
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Encode characters that need to be encoded on the path section of the URL.
|
|
114
|
-
*
|
|
115
|
-
* @param text - string to encode
|
|
116
|
-
* @returns encoded string
|
|
117
|
-
*/
|
|
118
|
-
export function encodePath(text: string | number): string {
|
|
119
|
-
return commonEncode(text).replace(HASH_RE, '%23').replace(IM_RE, '%3F');
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Encode characters that need to be encoded on the path section of the URL as a
|
|
124
|
-
* param. This function encodes everything {@link encodePath} does plus the
|
|
125
|
-
* slash (`/`) character. If `text` is `null` or `undefined`, returns an empty
|
|
126
|
-
* string instead.
|
|
127
|
-
*
|
|
128
|
-
* @param text - string to encode
|
|
129
|
-
* @returns encoded string
|
|
130
|
-
*/
|
|
131
|
-
export function encodeParam(text: string | number | null | undefined): string {
|
|
132
|
-
return text == null ? '' : encodePath(text).replace(SLASH_RE, '%2F');
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Decode text using `decodeURIComponent`. Returns the original text if it
|
|
137
|
-
* fails.
|
|
138
|
-
*
|
|
139
|
-
* @param text - string to decode
|
|
140
|
-
* @returns decoded string
|
|
141
|
-
*/
|
|
142
|
-
export function decode(text: string | number): string {
|
|
143
|
-
try {
|
|
144
|
-
return decodeURIComponent('' + text);
|
|
145
|
-
} catch (err) {
|
|
146
|
-
warn(`Error decoding "${text}". Using original value`);
|
|
147
|
-
}
|
|
148
|
-
return '' + text;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
export function decodeQuery(text: string): string {
|
|
152
|
-
return decode(text.replace(PLUS_RE, ' '));
|
|
153
|
-
}
|
package/src/utils/guards.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import type { RouteRecord } from '../types';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 判断是否是同一个路由
|
|
5
|
-
*/
|
|
6
|
-
export function isSameRoute(from: RouteRecord, to: RouteRecord) {
|
|
7
|
-
return (
|
|
8
|
-
from.matched.length === to.matched.length &&
|
|
9
|
-
from.matched.every((record, i) => record === to.matched[i])
|
|
10
|
-
);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* 判断是否是全等的路由: 路径完全相同
|
|
15
|
-
*/
|
|
16
|
-
export function isEqualRoute(from: RouteRecord, to: RouteRecord) {
|
|
17
|
-
return (
|
|
18
|
-
// 这里不仅仅判断了前后的path是否一致
|
|
19
|
-
// 同时判断了匹配路由对象的个数
|
|
20
|
-
// 这是因为在首次初始化时 this.current 的值为 { path:'/',matched:[] }
|
|
21
|
-
// 假如我们打开页面同样为 / 路径时,此时如果单纯判断path那么就会造成无法渲染
|
|
22
|
-
from.fullPath === to.fullPath &&
|
|
23
|
-
from.matched.length === to.matched.length
|
|
24
|
-
);
|
|
25
|
-
}
|
package/src/utils/index.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
export {
|
|
2
|
-
regexDomain,
|
|
3
|
-
normalizePath,
|
|
4
|
-
parsePath,
|
|
5
|
-
stringifyPath,
|
|
6
|
-
normalizeLocation,
|
|
7
|
-
isPathWithProtocolOrDomain
|
|
8
|
-
} from './path';
|
|
9
|
-
export { isESModule, inBrowser } from './utils';
|
|
10
|
-
export { warn } from './warn';
|
|
11
|
-
export { isSameRoute, isEqualRoute } from './guards';
|
|
12
|
-
export {
|
|
13
|
-
computeScrollPosition,
|
|
14
|
-
scrollToPosition,
|
|
15
|
-
saveScrollPosition,
|
|
16
|
-
getSavedScrollPosition,
|
|
17
|
-
getKeepScrollPosition
|
|
18
|
-
} from './scroll';
|
|
19
|
-
export { openWindow } from './bom';
|
|
20
|
-
export {
|
|
21
|
-
encodeHash,
|
|
22
|
-
encodeParam,
|
|
23
|
-
encodePath,
|
|
24
|
-
encodeQueryKey,
|
|
25
|
-
encodeQueryValue,
|
|
26
|
-
decode
|
|
27
|
-
} from './encoding';
|
package/src/utils/path.spec.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { describe, it } from 'vitest';
|
|
2
|
-
|
|
3
|
-
import { normalizeLocation, normalizePath } from './path';
|
|
4
|
-
|
|
5
|
-
describe('testing normalizeLocation', () => {
|
|
6
|
-
it('testing normal domain', ({ expect }) => {
|
|
7
|
-
expect(
|
|
8
|
-
normalizeLocation(
|
|
9
|
-
'http://localhost:5173/en/en/en/en/en',
|
|
10
|
-
'http://localhost:5173/en/'
|
|
11
|
-
).path
|
|
12
|
-
).toBe('/en/en/en/en');
|
|
13
|
-
});
|
|
14
|
-
it('testing path', ({ expect }) => {
|
|
15
|
-
expect(
|
|
16
|
-
normalizeLocation('/test1/test2?t=https://www-six.betafollowme.com')
|
|
17
|
-
.path
|
|
18
|
-
).toBe('/test1/test2');
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
describe('testing normalizePath', () => {
|
|
23
|
-
it('testing normal domain', ({ expect }) => {
|
|
24
|
-
expect(normalizePath('test2', 'test1')).toBe('/test1/test2');
|
|
25
|
-
});
|
|
26
|
-
it('testing path', ({ expect }) => {
|
|
27
|
-
expect(
|
|
28
|
-
normalizeLocation('/test1/test2?t=https://www-six.betafollowme.com')
|
|
29
|
-
.path
|
|
30
|
-
).toBe('/test1/test2');
|
|
31
|
-
});
|
|
32
|
-
});
|