@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.
- 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 -30
- 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/types/index.d.ts +0 -694
- package/dist/types/index.mjs +0 -6
- 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 -282
- 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 -292
- 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/types/index.ts +0 -858
- 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 -418
- 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
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { PAGE_ID } from "./increment-id.mjs";
|
|
2
|
+
import { RouterMode } from "./types.mjs";
|
|
3
|
+
const PAGE_ID_KEY = "__pageId__";
|
|
4
|
+
export class Navigation {
|
|
5
|
+
options;
|
|
6
|
+
_history;
|
|
7
|
+
_unSubscribePopState;
|
|
8
|
+
_promiseResolve = null;
|
|
9
|
+
constructor(options, onUpdated) {
|
|
10
|
+
const history2 = options.mode === RouterMode.history ? window.history : new MemoryHistory();
|
|
11
|
+
const onPopStateChange = (url, state) => {
|
|
12
|
+
const dispatchEvent = this._promiseResolve || onUpdated;
|
|
13
|
+
dispatchEvent == null ? void 0 : dispatchEvent(url, state);
|
|
14
|
+
};
|
|
15
|
+
const subscribePopState = history2 instanceof MemoryHistory ? history2.onPopState(onPopStateChange) : subscribeHtmlHistory(onPopStateChange);
|
|
16
|
+
this.options = options;
|
|
17
|
+
this._history = history2;
|
|
18
|
+
this._unSubscribePopState = subscribePopState;
|
|
19
|
+
}
|
|
20
|
+
get length() {
|
|
21
|
+
return this._history.length;
|
|
22
|
+
}
|
|
23
|
+
_push(history2, data, url) {
|
|
24
|
+
const state = Object.freeze({
|
|
25
|
+
...data || {},
|
|
26
|
+
[PAGE_ID_KEY]: PAGE_ID.next()
|
|
27
|
+
});
|
|
28
|
+
history2.pushState(state, "", url);
|
|
29
|
+
return state;
|
|
30
|
+
}
|
|
31
|
+
_replace(history2, data, url) {
|
|
32
|
+
var _a;
|
|
33
|
+
const oldId = (_a = history2.state) == null ? void 0 : _a[PAGE_ID_KEY];
|
|
34
|
+
const state = Object.freeze({
|
|
35
|
+
...data || {},
|
|
36
|
+
[PAGE_ID_KEY]: typeof oldId === "number" ? oldId : PAGE_ID.next()
|
|
37
|
+
});
|
|
38
|
+
history2.replaceState(state, "", url);
|
|
39
|
+
return state;
|
|
40
|
+
}
|
|
41
|
+
push(data, url) {
|
|
42
|
+
return this._push(this._history, data, url);
|
|
43
|
+
}
|
|
44
|
+
replace(data, url) {
|
|
45
|
+
return this._replace(this._history, data, url);
|
|
46
|
+
}
|
|
47
|
+
pushHistoryState(data, url) {
|
|
48
|
+
this._push(history, data, url);
|
|
49
|
+
}
|
|
50
|
+
replaceHistoryState(data, url) {
|
|
51
|
+
this._replace(history, data, url);
|
|
52
|
+
}
|
|
53
|
+
go(index) {
|
|
54
|
+
if (this._promiseResolve) {
|
|
55
|
+
return Promise.resolve(null);
|
|
56
|
+
}
|
|
57
|
+
return new Promise((resolve) => {
|
|
58
|
+
this._promiseResolve = (url, state) => {
|
|
59
|
+
this._promiseResolve = null;
|
|
60
|
+
if (typeof url !== "string") return resolve(null);
|
|
61
|
+
resolve({ type: "success", url, state: state || {} });
|
|
62
|
+
};
|
|
63
|
+
setTimeout(this._promiseResolve, 80);
|
|
64
|
+
this._history.go(index);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
forward() {
|
|
68
|
+
return this.go(1);
|
|
69
|
+
}
|
|
70
|
+
back() {
|
|
71
|
+
return this.go(-1);
|
|
72
|
+
}
|
|
73
|
+
destroy() {
|
|
74
|
+
var _a;
|
|
75
|
+
(_a = this._promiseResolve) == null ? void 0 : _a.call(this);
|
|
76
|
+
this._unSubscribePopState();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
export class MemoryHistory {
|
|
80
|
+
_entries = [];
|
|
81
|
+
_index = -1;
|
|
82
|
+
get _curEntry() {
|
|
83
|
+
const idx = this._index;
|
|
84
|
+
if (idx < 0 || idx >= this.length) return null;
|
|
85
|
+
return this._entries[idx];
|
|
86
|
+
}
|
|
87
|
+
_popStateCbs = /* @__PURE__ */ new Set();
|
|
88
|
+
scrollRestoration = "auto";
|
|
89
|
+
// Return null when no current entry to align with browser history.state behavior
|
|
90
|
+
// Browser history.state can be null when no state was provided
|
|
91
|
+
get state() {
|
|
92
|
+
var _a;
|
|
93
|
+
return ((_a = this._curEntry) == null ? void 0 : _a.state) ?? null;
|
|
94
|
+
}
|
|
95
|
+
get url() {
|
|
96
|
+
var _a;
|
|
97
|
+
return ((_a = this._curEntry) == null ? void 0 : _a.url) ?? "";
|
|
98
|
+
}
|
|
99
|
+
constructor() {
|
|
100
|
+
this.pushState(null, "", "/");
|
|
101
|
+
}
|
|
102
|
+
get length() {
|
|
103
|
+
return this._entries.length;
|
|
104
|
+
}
|
|
105
|
+
pushState(data, unused, url) {
|
|
106
|
+
this._entries.splice(this._index + 1);
|
|
107
|
+
this._entries.push({ state: data, url: (url == null ? void 0 : url.toString()) ?? this.url });
|
|
108
|
+
this._index = this._entries.length - 1;
|
|
109
|
+
}
|
|
110
|
+
replaceState(data, unused, url) {
|
|
111
|
+
const curEntry = this._curEntry;
|
|
112
|
+
if (!curEntry) return;
|
|
113
|
+
curEntry.state = { ...data };
|
|
114
|
+
if (url) curEntry.url = url.toString();
|
|
115
|
+
}
|
|
116
|
+
back() {
|
|
117
|
+
this.go(-1);
|
|
118
|
+
}
|
|
119
|
+
forward() {
|
|
120
|
+
this.go(1);
|
|
121
|
+
}
|
|
122
|
+
go(delta) {
|
|
123
|
+
if (!delta) return;
|
|
124
|
+
const newIdx = this._index + delta;
|
|
125
|
+
if (newIdx < 0 || newIdx >= this.length) return;
|
|
126
|
+
this._index = newIdx;
|
|
127
|
+
const entry = this._curEntry;
|
|
128
|
+
setTimeout(() => {
|
|
129
|
+
this._popStateCbs.forEach((cb) => cb(entry.url, entry.state));
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
onPopState(cb) {
|
|
133
|
+
if (typeof cb !== "function") return () => {
|
|
134
|
+
};
|
|
135
|
+
this._popStateCbs.add(cb);
|
|
136
|
+
return () => this._popStateCbs.delete(cb);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
function subscribeHtmlHistory(cb) {
|
|
140
|
+
const wrapper = () => cb(location.href, history.state || {});
|
|
141
|
+
window.addEventListener("popstate", wrapper);
|
|
142
|
+
return () => window.removeEventListener("popstate", wrapper);
|
|
143
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|