Skip to content

Commit 8a30bf2

Browse files
committed
Merge branch 'feat-router' of github.com:esmnext/esmx into feat-router
2 parents 328aed2 + 695b7d1 commit 8a30bf2

File tree

3 files changed

+66
-11
lines changed

3 files changed

+66
-11
lines changed

packages/router/src/next/router.ts

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { Navigation } from './navigation';
22
import { parsedOptions } from './options';
33
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
1315
} from './types';
1416

1517
export class Router {
@@ -259,5 +261,36 @@ export class Router {
259261
}
260262
public destroy() {
261263
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+
}
262295
}
263296
}

packages/router/src/next/task.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
const isDev = process.env.NODE_ENV !== 'production';
21
const warn = (...message: any[]) => {
3-
if (!isDev) return;
2+
if (process.env.NODE_ENV === 'production') return;
43
console.warn('[Task Warning]', ...message);
54
};
65

@@ -19,6 +18,11 @@ export class Tasks<T extends Function> {
1918
protected handlers: T[] = []
2019
) {}
2120

21+
public add(...handlers: T[]) {
22+
this.handlers.push(...handlers);
23+
return this;
24+
}
25+
2226
public status: TaskStatus = 'initial';
2327

2428
public abort() {

packages/router/src/next/types.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import type { MatchFunction } from 'path-to-regexp';
2+
import type { Router } from './router';
3+
24
export enum NavigationType {
35
// Action 类型
46
push = 'push',
@@ -212,4 +214,20 @@ export interface RouteMatchResult {
212214

213215
export type RouteMatcher = (targetURL: URL, baseURL: URL) => RouteMatchResult;
214216

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+
215233
// 旧字段 + 新字段

0 commit comments

Comments
 (0)