@esmx/router 3.0.0-rc.18 → 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.
Files changed (158) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +70 -0
  3. package/README.zh-CN.md +70 -0
  4. package/dist/error.d.ts +23 -0
  5. package/dist/error.mjs +61 -0
  6. package/dist/increment-id.d.ts +7 -0
  7. package/dist/increment-id.mjs +11 -0
  8. package/dist/index.d.ts +5 -3
  9. package/dist/index.mjs +14 -3
  10. package/dist/index.test.mjs +8 -0
  11. package/dist/location.d.ts +15 -0
  12. package/dist/location.mjs +53 -0
  13. package/dist/location.test.d.ts +8 -0
  14. package/dist/location.test.mjs +370 -0
  15. package/dist/matcher.d.ts +3 -0
  16. package/dist/matcher.mjs +44 -0
  17. package/dist/matcher.test.mjs +1492 -0
  18. package/dist/micro-app.d.ts +18 -0
  19. package/dist/micro-app.dom.test.d.ts +1 -0
  20. package/dist/micro-app.dom.test.mjs +532 -0
  21. package/dist/micro-app.mjs +80 -0
  22. package/dist/navigation.d.ts +43 -0
  23. package/dist/navigation.mjs +143 -0
  24. package/dist/navigation.test.d.ts +1 -0
  25. package/dist/navigation.test.mjs +681 -0
  26. package/dist/options.d.ts +4 -0
  27. package/dist/options.mjs +88 -0
  28. package/dist/route-task.d.ts +40 -0
  29. package/dist/route-task.mjs +75 -0
  30. package/dist/route-task.test.d.ts +1 -0
  31. package/dist/route-task.test.mjs +673 -0
  32. package/dist/route-transition.d.ts +53 -0
  33. package/dist/route-transition.mjs +307 -0
  34. package/dist/route-transition.test.d.ts +1 -0
  35. package/dist/route-transition.test.mjs +146 -0
  36. package/dist/route.d.ts +72 -0
  37. package/dist/route.mjs +194 -0
  38. package/dist/route.test.d.ts +1 -0
  39. package/dist/route.test.mjs +1664 -0
  40. package/dist/router-back.test.d.ts +1 -0
  41. package/dist/router-back.test.mjs +361 -0
  42. package/dist/router-forward.test.d.ts +1 -0
  43. package/dist/router-forward.test.mjs +376 -0
  44. package/dist/router-go.test.d.ts +1 -0
  45. package/dist/router-go.test.mjs +73 -0
  46. package/dist/router-guards-cleanup.test.d.ts +1 -0
  47. package/dist/router-guards-cleanup.test.mjs +437 -0
  48. package/dist/router-link.d.ts +10 -0
  49. package/dist/router-link.mjs +126 -0
  50. package/dist/router-push.test.d.ts +1 -0
  51. package/dist/router-push.test.mjs +115 -0
  52. package/dist/router-replace.test.d.ts +1 -0
  53. package/dist/router-replace.test.mjs +114 -0
  54. package/dist/router-resolve.test.d.ts +1 -0
  55. package/dist/router-resolve.test.mjs +393 -0
  56. package/dist/router-restart-app.dom.test.d.ts +1 -0
  57. package/dist/router-restart-app.dom.test.mjs +616 -0
  58. package/dist/router-window-navigation.test.d.ts +1 -0
  59. package/dist/router-window-navigation.test.mjs +359 -0
  60. package/dist/router.d.ts +109 -102
  61. package/dist/router.mjs +260 -361
  62. package/dist/types.d.ts +246 -0
  63. package/dist/types.mjs +18 -0
  64. package/dist/util.d.ts +26 -0
  65. package/dist/util.mjs +53 -0
  66. package/dist/util.test.d.ts +1 -0
  67. package/dist/util.test.mjs +1020 -0
  68. package/package.json +10 -13
  69. package/src/error.ts +84 -0
  70. package/src/increment-id.ts +12 -0
  71. package/src/index.test.ts +9 -0
  72. package/src/index.ts +54 -3
  73. package/src/location.test.ts +406 -0
  74. package/src/location.ts +96 -0
  75. package/src/matcher.test.ts +1685 -0
  76. package/src/matcher.ts +59 -0
  77. package/src/micro-app.dom.test.ts +708 -0
  78. package/src/micro-app.ts +101 -0
  79. package/src/navigation.test.ts +858 -0
  80. package/src/navigation.ts +195 -0
  81. package/src/options.ts +131 -0
  82. package/src/route-task.test.ts +901 -0
  83. package/src/route-task.ts +105 -0
  84. package/src/route-transition.test.ts +178 -0
  85. package/src/route-transition.ts +425 -0
  86. package/src/route.test.ts +2014 -0
  87. package/src/route.ts +308 -0
  88. package/src/router-back.test.ts +487 -0
  89. package/src/router-forward.test.ts +506 -0
  90. package/src/router-go.test.ts +91 -0
  91. package/src/router-guards-cleanup.test.ts +595 -0
  92. package/src/router-link.ts +235 -0
  93. package/src/router-push.test.ts +140 -0
  94. package/src/router-replace.test.ts +139 -0
  95. package/src/router-resolve.test.ts +475 -0
  96. package/src/router-restart-app.dom.test.ts +783 -0
  97. package/src/router-window-navigation.test.ts +457 -0
  98. package/src/router.ts +289 -470
  99. package/src/types.ts +341 -0
  100. package/src/util.test.ts +1262 -0
  101. package/src/util.ts +116 -0
  102. package/dist/history/abstract.d.ts +0 -29
  103. package/dist/history/abstract.mjs +0 -107
  104. package/dist/history/base.d.ts +0 -79
  105. package/dist/history/base.mjs +0 -275
  106. package/dist/history/html.d.ts +0 -30
  107. package/dist/history/html.mjs +0 -183
  108. package/dist/history/index.d.ts +0 -7
  109. package/dist/history/index.mjs +0 -16
  110. package/dist/matcher/create-matcher.d.ts +0 -5
  111. package/dist/matcher/create-matcher.mjs +0 -218
  112. package/dist/matcher/create-matcher.spec.mjs +0 -0
  113. package/dist/matcher/index.d.ts +0 -1
  114. package/dist/matcher/index.mjs +0 -1
  115. package/dist/task-pipe/index.d.ts +0 -1
  116. package/dist/task-pipe/index.mjs +0 -1
  117. package/dist/task-pipe/task.d.ts +0 -30
  118. package/dist/task-pipe/task.mjs +0 -66
  119. package/dist/types/index.d.ts +0 -694
  120. package/dist/types/index.mjs +0 -6
  121. package/dist/utils/bom.d.ts +0 -5
  122. package/dist/utils/bom.mjs +0 -10
  123. package/dist/utils/encoding.d.ts +0 -48
  124. package/dist/utils/encoding.mjs +0 -44
  125. package/dist/utils/guards.d.ts +0 -9
  126. package/dist/utils/guards.mjs +0 -12
  127. package/dist/utils/index.d.ts +0 -7
  128. package/dist/utils/index.mjs +0 -27
  129. package/dist/utils/path.d.ts +0 -60
  130. package/dist/utils/path.mjs +0 -282
  131. package/dist/utils/path.spec.mjs +0 -27
  132. package/dist/utils/scroll.d.ts +0 -25
  133. package/dist/utils/scroll.mjs +0 -59
  134. package/dist/utils/utils.d.ts +0 -16
  135. package/dist/utils/utils.mjs +0 -11
  136. package/dist/utils/warn.d.ts +0 -2
  137. package/dist/utils/warn.mjs +0 -12
  138. package/src/history/abstract.ts +0 -149
  139. package/src/history/base.ts +0 -408
  140. package/src/history/html.ts +0 -228
  141. package/src/history/index.ts +0 -20
  142. package/src/matcher/create-matcher.spec.ts +0 -3
  143. package/src/matcher/create-matcher.ts +0 -292
  144. package/src/matcher/index.ts +0 -1
  145. package/src/task-pipe/index.ts +0 -1
  146. package/src/task-pipe/task.ts +0 -97
  147. package/src/types/index.ts +0 -858
  148. package/src/utils/bom.ts +0 -14
  149. package/src/utils/encoding.ts +0 -153
  150. package/src/utils/guards.ts +0 -25
  151. package/src/utils/index.ts +0 -27
  152. package/src/utils/path.spec.ts +0 -32
  153. package/src/utils/path.ts +0 -418
  154. package/src/utils/scroll.ts +0 -120
  155. package/src/utils/utils.ts +0 -30
  156. package/src/utils/warn.ts +0 -13
  157. /package/dist/{matcher/create-matcher.spec.d.ts → index.test.d.ts} +0 -0
  158. /package/dist/{utils/path.spec.d.ts → matcher.test.d.ts} +0 -0
@@ -1,183 +0,0 @@
1
- import {
2
- computeScrollPosition,
3
- getKeepScrollPosition,
4
- getSavedScrollPosition,
5
- isPathWithProtocolOrDomain,
6
- normalizeLocation,
7
- openWindow,
8
- saveScrollPosition,
9
- scrollToPosition
10
- } from "../utils/index.mjs";
11
- import { BaseRouterHistory } from "./base.mjs";
12
- export class HtmlHistory extends BaseRouterHistory {
13
- constructor(router) {
14
- super(router);
15
- if ("scrollRestoration" in window.history) {
16
- window.history.scrollRestoration = "manual";
17
- }
18
- }
19
- // 获取当前地址,包括 path query hash
20
- getCurrentLocation() {
21
- const { href } = window.location;
22
- const { state } = window.history;
23
- const { path, base, ...rest } = normalizeLocation(
24
- href,
25
- this.router.base
26
- );
27
- return {
28
- path: path.replace(new RegExp(`^(${base})`), ""),
29
- base,
30
- ...rest,
31
- state
32
- };
33
- }
34
- onPopState = (e) => {
35
- if (this.isFrozen) return;
36
- if (this.router.checkLayerState(e.state)) return;
37
- const current = Object.assign({}, this.current);
38
- this.transitionTo(this.getCurrentLocation(), async (route) => {
39
- const { state } = window.history;
40
- saveScrollPosition(current.fullPath, computeScrollPosition());
41
- setTimeout(async () => {
42
- const keepScrollPosition = state.keepScrollPosition;
43
- if (keepScrollPosition) {
44
- return;
45
- }
46
- const savedPosition = getSavedScrollPosition(route.fullPath);
47
- const position = await this.router.scrollBehavior(
48
- current,
49
- route,
50
- savedPosition
51
- );
52
- const { nextTick } = this.router.options;
53
- if (position) {
54
- nextTick && await nextTick();
55
- scrollToPosition(position);
56
- }
57
- });
58
- });
59
- };
60
- async init({ replace } = { replace: true }) {
61
- const { initUrl } = this.router.options;
62
- let route = this.getCurrentLocation();
63
- if (initUrl !== void 0) {
64
- route = this.resolve(initUrl);
65
- } else {
66
- const state = history.state || {};
67
- route.state = {
68
- ...state,
69
- _ancientRoute: state._ancientRoute ?? true
70
- // 最古历史的标记, 在调用返回事件时如果有这个标记则直接调用没有历史记录的钩子
71
- };
72
- }
73
- if (replace) {
74
- await this.replace(route);
75
- } else {
76
- await this.push(route);
77
- }
78
- this.setupListeners();
79
- }
80
- // 设置监听函数
81
- setupListeners() {
82
- window.addEventListener("popstate", this.onPopState);
83
- }
84
- destroy() {
85
- window.removeEventListener("popstate", this.onPopState);
86
- }
87
- pushWindow(location) {
88
- if (this.isFrozen) return;
89
- this.handleOutside(location, false, true);
90
- }
91
- replaceWindow(location) {
92
- if (this.isFrozen) return;
93
- this.handleOutside(location, true, true);
94
- }
95
- // 处理外站跳转逻辑
96
- handleOutside(location, replace = false, isTriggerWithWindow = false) {
97
- const base = this.router.base;
98
- const { flag, route } = isPathWithProtocolOrDomain(location, base);
99
- const router = this.router;
100
- const { handleOutside, validateOutside } = router.options;
101
- const isSameHost = !flag || window.location.hostname === route.hostname;
102
- if (!isTriggerWithWindow) {
103
- if (isSameHost && !(validateOutside == null ? void 0 : validateOutside({ router, location, route }))) {
104
- return false;
105
- }
106
- }
107
- const res = handleOutside == null ? void 0 : handleOutside({
108
- router,
109
- route,
110
- replace,
111
- isTriggerWithWindow,
112
- isSameHost
113
- });
114
- if (res === false) {
115
- return true;
116
- }
117
- if (replace) {
118
- window.location.replace(route.href);
119
- } else {
120
- const { hostname, href } = route;
121
- openWindow(href, hostname);
122
- }
123
- return true;
124
- }
125
- // 新增路由记录跳转
126
- async push(location) {
127
- await this.jump(location, false);
128
- }
129
- // 替换当前路由记录跳转
130
- async replace(location) {
131
- await this.jump(location, true);
132
- }
133
- // 跳转方法
134
- async jump(location, replace = false) {
135
- if (this.isFrozen) return;
136
- if (this.handleOutside(location, replace)) {
137
- return;
138
- }
139
- const current = Object.assign({}, this.current);
140
- await this.transitionTo(location, (route) => {
141
- const keepScrollPosition = getKeepScrollPosition(location);
142
- if (!keepScrollPosition) {
143
- saveScrollPosition(current.fullPath, computeScrollPosition());
144
- scrollToPosition({ left: 0, top: 0 });
145
- }
146
- const state = Object.assign(
147
- replace ? { ...history.state, ...route.state } : { ...route.state, _ancientRoute: false },
148
- { keepScrollPosition }
149
- );
150
- window.history[replace ? "replaceState" : "pushState"](
151
- state,
152
- "",
153
- route.fullPath
154
- );
155
- this.router.updateLayerState(route);
156
- });
157
- }
158
- go(delta) {
159
- if (this.isFrozen) return;
160
- window.history.go(delta);
161
- }
162
- forward() {
163
- if (this.isFrozen) return;
164
- window.history.forward();
165
- }
166
- timer = null;
167
- back() {
168
- if (this.isFrozen) return;
169
- const oldState = history.state;
170
- const noBackNavigation = this.router.options.noBackNavigation;
171
- if (oldState._ancientRoute === true) {
172
- noBackNavigation && noBackNavigation(this.router);
173
- return;
174
- }
175
- window.history.back();
176
- this.timer = setTimeout(() => {
177
- if (history.state === oldState) {
178
- noBackNavigation && noBackNavigation(this.router);
179
- }
180
- this.timer = null;
181
- }, 80);
182
- }
183
- }
@@ -1,7 +0,0 @@
1
- import { type RouterInstance, RouterMode } from '../types';
2
- import { AbstractHistory } from './abstract';
3
- import { HtmlHistory } from './html';
4
- export declare function createHistory({ router, mode }: {
5
- router: RouterInstance;
6
- mode: RouterMode;
7
- }): AbstractHistory | HtmlHistory;
@@ -1,16 +0,0 @@
1
- import { RouterMode } from "../types/index.mjs";
2
- import { AbstractHistory } from "./abstract.mjs";
3
- import { HtmlHistory } from "./html.mjs";
4
- export function createHistory({
5
- router,
6
- mode
7
- }) {
8
- switch (mode) {
9
- case RouterMode.HISTORY:
10
- return new HtmlHistory(router);
11
- case RouterMode.ABSTRACT:
12
- return new AbstractHistory(router);
13
- default:
14
- throw new Error("not support mode");
15
- }
16
- }
@@ -1,5 +0,0 @@
1
- import type { RouteConfig, RouterMatcher } from '../types';
2
- /**
3
- * 创建路由匹配器
4
- */
5
- export declare function createRouterMatcher(routes: RouteConfig[]): RouterMatcher;
@@ -1,218 +0,0 @@
1
- import { compile, match, pathToRegexp } from "path-to-regexp";
2
- import {
3
- decode,
4
- encodePath,
5
- normalizeLocation,
6
- normalizePath,
7
- parsePath,
8
- stringifyPath
9
- } from "../utils/index.mjs";
10
- class RouteMatcher {
11
- /*
12
- * 路由匹配规则
13
- */
14
- routeMatches;
15
- /*
16
- * 原始路由配置
17
- */
18
- // protected routes: RouteConfig[];
19
- constructor(routes) {
20
- this.routeMatches = createRouteMatches(routes);
21
- }
22
- /*
23
- * 根据配置匹配对应的路由
24
- */
25
- match(rawLocation, {
26
- base,
27
- redirectedFrom
28
- } = { base: "" }) {
29
- let path = "";
30
- let query = {};
31
- let queryArray = {};
32
- let params = {};
33
- let hash = "";
34
- let state = {};
35
- const parsedOption = parsePath(rawLocation.path);
36
- path = parsedOption.pathname;
37
- query = rawLocation.query || parsedOption.query || {};
38
- queryArray = rawLocation.queryArray || parsedOption.queryArray || {};
39
- hash = rawLocation.hash || parsedOption.hash || "";
40
- state = rawLocation.state || {};
41
- const routeMatch = this.routeMatches.find(({ match: match2 }) => {
42
- return match2(path);
43
- });
44
- if (routeMatch) {
45
- const {
46
- component,
47
- asyncComponent,
48
- compile: compile2,
49
- meta,
50
- redirect,
51
- matched,
52
- parse
53
- } = routeMatch.internalRedirect || routeMatch;
54
- params = rawLocation.params || parse(path).params || {};
55
- const realPath = normalizePath(
56
- compile2({
57
- query,
58
- queryArray,
59
- params,
60
- hash
61
- })
62
- );
63
- const {
64
- params: realParams,
65
- query: realQuery,
66
- queryArray: realQueryArray,
67
- hash: realHash
68
- } = parse(realPath);
69
- const routeRecord = {
70
- base,
71
- path: normalizePath(
72
- compile2({
73
- params: realParams
74
- })
75
- ),
76
- fullPath: realPath,
77
- params: realParams,
78
- query: realQuery,
79
- queryArray: realQueryArray,
80
- hash: realHash,
81
- state,
82
- component,
83
- asyncComponent,
84
- meta,
85
- redirect,
86
- redirectedFrom,
87
- matched
88
- };
89
- if (redirect) {
90
- const normalizedLocation = normalizeLocation(
91
- typeof redirect === "function" ? redirect(routeRecord) : redirect,
92
- base
93
- );
94
- return this.match(normalizedLocation, {
95
- base,
96
- redirectedFrom: routeRecord
97
- });
98
- }
99
- return routeRecord;
100
- }
101
- return null;
102
- }
103
- /*
104
- * 获取当前路由匹配规则
105
- */
106
- getRoutes() {
107
- return this.routeMatches;
108
- }
109
- /**
110
- * 新增单个路由匹配规则
111
- */
112
- // public addRoute(route: RouteConfig) {
113
- // this.routes.push(route);
114
- // this.routeMatches = createRouteMatches(this.routes);
115
- // }
116
- /**
117
- * 新增多个路由匹配规则
118
- */
119
- // public addRoutes(routes: RouteConfig[]) {
120
- // this.routes.push(...routes);
121
- // this.routeMatches = createRouteMatches(this.routes);
122
- // }
123
- }
124
- export function createRouterMatcher(routes) {
125
- return new RouteMatcher(routes);
126
- }
127
- function createRouteMatches(routes, parent) {
128
- const routeMatches = [];
129
- for (const route of routes) {
130
- routeMatches.push(
131
- ...createRouteMatch(
132
- {
133
- ...route,
134
- path: Array.isArray(route.path) ? route.path : [route.path]
135
- },
136
- parent
137
- )
138
- );
139
- }
140
- return routeMatches;
141
- }
142
- function createRouteMatch(route, parent) {
143
- const pathList = Array.isArray(route.path) ? route.path : [route.path];
144
- const routeMatches = pathList.reduce(
145
- (acc, item, index) => {
146
- const { children } = route;
147
- const path = normalizePath(item, parent == null ? void 0 : parent.path);
148
- let regex;
149
- try {
150
- regex = pathToRegexp(path);
151
- } catch (error) {
152
- console.warn(
153
- `@create route rule failed on path: ${path}`,
154
- route
155
- );
156
- return acc;
157
- }
158
- const toPath = compile(path, { encode: encodePath });
159
- const parseParams = match(path, { decode });
160
- const current = {
161
- regex,
162
- match: (path2) => {
163
- return regex.test(path2);
164
- },
165
- parse: (path2) => {
166
- const { pathname, query, queryArray, hash } = parsePath(path2);
167
- const { params } = parseParams(pathname) || { params: {} };
168
- return {
169
- params: Object.assign({}, params),
170
- // parse的 params 是使用 Object.create(null) 创建的没有原型的对象,需要进行包装处理
171
- query,
172
- queryArray,
173
- hash
174
- };
175
- },
176
- compile: ({ params = {}, query = {}, queryArray = {}, hash = "" } = {
177
- params: {},
178
- query: {},
179
- queryArray: {},
180
- hash: ""
181
- }) => {
182
- const pathString = toPath(params);
183
- return stringifyPath({
184
- pathname: pathString,
185
- query,
186
- queryArray,
187
- hash
188
- });
189
- },
190
- path,
191
- appType: route.appType || (parent == null ? void 0 : parent.appType) || "",
192
- component: route.component,
193
- asyncComponent: route.asyncComponent,
194
- meta: route.meta || {},
195
- redirect: route.redirect,
196
- /**
197
- * 第一个 path 作为基准,后续 path 会内部重定向到第一个 path
198
- * 同时如果父路由存在内部跳转,子路由也需要处理内部跳转
199
- */
200
- internalRedirect: index > 0 || (parent == null ? void 0 : parent.internalRedirect) ? createRouteMatch(
201
- {
202
- ...route,
203
- path: pathList[0]
204
- },
205
- (parent == null ? void 0 : parent.internalRedirect) || parent
206
- ) : void 0,
207
- matched: [...(parent == null ? void 0 : parent.matched) || [], route]
208
- };
209
- if (children && children.length > 0) {
210
- acc.push(...createRouteMatches(children, current));
211
- }
212
- acc.push(current);
213
- return acc;
214
- },
215
- []
216
- );
217
- return Array.isArray(route.path) ? routeMatches : routeMatches[routeMatches.length - 1];
218
- }
File without changes
@@ -1 +0,0 @@
1
- export { createRouterMatcher } from './create-matcher';
@@ -1 +0,0 @@
1
- export { createRouterMatcher } from "./create-matcher.mjs";
@@ -1 +0,0 @@
1
- export { Tasks, createTasks } from './task';
@@ -1 +0,0 @@
1
- export { Tasks, createTasks } from "./task.mjs";
@@ -1,30 +0,0 @@
1
- import type { Awaitable } from '../types';
2
- /**
3
- * 创建可操作的任务队列
4
- */
5
- type func = (...args: any) => any;
6
- /**
7
- * 任务状态
8
- */
9
- export declare enum TaskStatus {
10
- INITIAL = "initial",
11
- RUNNING = "running",
12
- FINISHED = "finished",
13
- ERROR = "error",
14
- ABORTED = "aborted"
15
- }
16
- export declare class Tasks<T extends func = func> {
17
- protected handlers: T[];
18
- add(handler: T | T[]): void;
19
- reset(): void;
20
- get list(): T[];
21
- get length(): number;
22
- status: TaskStatus;
23
- run({ cb, final }?: {
24
- cb?: (res: Awaited<ReturnType<T>>) => Awaitable<void>;
25
- final?: () => Awaitable<void>;
26
- }): Promise<void>;
27
- abort(): void;
28
- }
29
- export declare function createTasks<T extends func = func>(): Tasks<T>;
30
- export {};
@@ -1,66 +0,0 @@
1
- import { warn } from "../utils/index.mjs";
2
- export var TaskStatus = /* @__PURE__ */ ((TaskStatus2) => {
3
- TaskStatus2["INITIAL"] = "initial";
4
- TaskStatus2["RUNNING"] = "running";
5
- TaskStatus2["FINISHED"] = "finished";
6
- TaskStatus2["ERROR"] = "error";
7
- TaskStatus2["ABORTED"] = "aborted";
8
- return TaskStatus2;
9
- })(TaskStatus || {});
10
- export class Tasks {
11
- handlers = [];
12
- add(handler) {
13
- const params = Array.isArray(handler) ? handler : [handler];
14
- this.handlers.push(...params);
15
- }
16
- reset() {
17
- this.handlers = [];
18
- }
19
- get list() {
20
- return this.handlers;
21
- }
22
- get length() {
23
- return this.handlers.length;
24
- }
25
- status = "initial" /* INITIAL */;
26
- async run({
27
- cb,
28
- final
29
- } = {}) {
30
- if (this.status !== "initial") {
31
- if (process.env.NODE_ENV !== "production") {
32
- warn(`task start failed in status ${this.status}`);
33
- }
34
- return;
35
- }
36
- this.status = "running" /* RUNNING */;
37
- for await (const handler of this.list) {
38
- if (this.status === "aborted" /* ABORTED */) {
39
- return;
40
- }
41
- if (typeof handler === "function") {
42
- try {
43
- const res = await handler();
44
- cb && await cb(res);
45
- } catch (error) {
46
- warn("task error:", error);
47
- this.status = "error" /* ERROR */;
48
- }
49
- } else {
50
- warn("task is not a function", handler);
51
- }
52
- }
53
- if (this.status !== "running" /* RUNNING */) return;
54
- final && await final();
55
- this.status = "finished" /* FINISHED */;
56
- }
57
- abort() {
58
- if (process.env.NODE_ENV !== "production" && this.status === "running" /* RUNNING */) {
59
- warn("abort task when task is running");
60
- }
61
- this.status = "aborted" /* ABORTED */;
62
- }
63
- }
64
- export function createTasks() {
65
- return new Tasks();
66
- }