@native-router/core 1.2.0 → 1.3.0
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/README.md +95 -55
- package/dist/index.cjs +367 -64
- package/dist/index.mjs +365 -65
- package/dist/types/errors.d.ts +3 -0
- package/dist/types/router.d.ts +74 -6
- package/dist/types/types.d.ts +50 -0
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -12,9 +12,44 @@ class NotFoundError extends NativeRouterError {
|
|
|
12
12
|
super(`Can't find the path: ${pathname}`);
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
|
+
class RedirectLoopError extends NativeRouterError {
|
|
16
|
+
constructor(target) {
|
|
17
|
+
super(`Redirect loop detected${target ? ` after following redirects to: ${target}` : ''}`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
15
20
|
|
|
16
21
|
const DEFAULT_MAX_STACK_DEPTH = 100;
|
|
17
22
|
|
|
23
|
+
/** Max redirects followed by {@link resolveEntry} before giving up. */
|
|
24
|
+
const MAX_REDIRECTS = 10;
|
|
25
|
+
|
|
26
|
+
/** Default cache lifetime of {@link preload} results, in milliseconds. */
|
|
27
|
+
const DEFAULT_PRELOAD_TTL = 30_000;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* A location resolved through the route guards, together with the view task
|
|
31
|
+
* of its final target. When guards redirected, `location` is the terminal
|
|
32
|
+
* location and `task` resolves the view of the target route.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Cache record of {@link preload}: the resolution promise of the
|
|
37
|
+
* prefetched target(every hit within the TTL awaits the very same
|
|
38
|
+
* promise, which also deduplicates concurrent callers) plus its
|
|
39
|
+
* expiry timestamp.
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Bookkeeping every {@link create}d router carries on top of
|
|
44
|
+
* {@link RouterInstance}. Declared in this module(instead of types.ts)
|
|
45
|
+
* to keep the public instance type surface stable:
|
|
46
|
+
* - `baseIndex`: absolute history index of `locationStack[0]`. The
|
|
47
|
+
* physical(window-relative) stack slot of a history entry is
|
|
48
|
+
* `history index - baseIndex`; entries whose slot falls outside the
|
|
49
|
+
* memory window re-resolve lazily when landed on.
|
|
50
|
+
* - `preloadCache`: router-level cache of {@link preload} results.
|
|
51
|
+
*/
|
|
52
|
+
|
|
18
53
|
/**
|
|
19
54
|
* Create a router instance.
|
|
20
55
|
* @group Methods
|
|
@@ -27,25 +62,30 @@ const DEFAULT_MAX_STACK_DEPTH = 100;
|
|
|
27
62
|
*/
|
|
28
63
|
function create(routes, history, resolveView, options) {
|
|
29
64
|
const [currentGuard, cancelAll] = util.createCurrentGuard();
|
|
65
|
+
const instanceHistory = history;
|
|
66
|
+
const state = instanceHistory.location.state || {};
|
|
30
67
|
const {
|
|
31
68
|
index
|
|
32
69
|
} = getHistoryState({
|
|
33
|
-
history:
|
|
70
|
+
history: instanceHistory
|
|
34
71
|
});
|
|
35
|
-
// Restore the session
|
|
36
|
-
// current entry state
|
|
37
|
-
//
|
|
38
|
-
const locationStack = restoreLocationStack(
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
return {
|
|
72
|
+
// Restore the session window from the bounded location window in the
|
|
73
|
+
// current entry state. Window-less legacy(1.x index-only) states degrade
|
|
74
|
+
// to a single-entry window aligned with the landed position.
|
|
75
|
+
const locationStack = restoreLocationStack(instanceHistory);
|
|
76
|
+
const baseIndex = state.locationStack?.length ? state.base || 0 :
|
|
77
|
+
// Degraded window: its only entry IS the landed position.
|
|
78
|
+
index;
|
|
79
|
+
const router = {
|
|
44
80
|
routes: Array.isArray(routes) ? routes : [routes],
|
|
45
81
|
resolveView,
|
|
46
|
-
history:
|
|
82
|
+
history: instanceHistory,
|
|
47
83
|
locationStack,
|
|
48
|
-
|
|
84
|
+
// The view stack is window-relative, so it is exactly as long as the
|
|
85
|
+
// location window and stays bounded by maxStackDepth with it.
|
|
86
|
+
viewStack: new Array(locationStack.length).fill(null),
|
|
87
|
+
baseIndex,
|
|
88
|
+
preloadCache: new Map(),
|
|
49
89
|
currentGuard,
|
|
50
90
|
cancelAll,
|
|
51
91
|
errorHandler: util.reject,
|
|
@@ -53,6 +93,16 @@ function create(routes, history, resolveView, options) {
|
|
|
53
93
|
baseUrl: options?.baseUrl || '',
|
|
54
94
|
maxStackDepth: options?.maxStackDepth || DEFAULT_MAX_STACK_DEPTH
|
|
55
95
|
};
|
|
96
|
+
if (options?.currentView) {
|
|
97
|
+
const physical = index - baseIndex;
|
|
98
|
+
// Hand-crafted or corrupted state may land the index outside the
|
|
99
|
+
// restored window; skip the write instead of creating a string-keyed
|
|
100
|
+
// property on the array(a negative index would).
|
|
101
|
+
if (physical >= 0 && physical < router.viewStack.length) {
|
|
102
|
+
router.viewStack[physical] = options.currentView;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return router;
|
|
56
106
|
}
|
|
57
107
|
function setOptions(router, options) {
|
|
58
108
|
return Object.assign(router, options);
|
|
@@ -68,34 +118,34 @@ function getLocation({
|
|
|
68
118
|
}
|
|
69
119
|
|
|
70
120
|
/**
|
|
71
|
-
* Restore the
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
121
|
+
* Restore the bounded location window serialized in the current history
|
|
122
|
+
* entry state, as-is: entries before the window start are outside the
|
|
123
|
+
* memory window(see {@link RouterCore.baseIndex}) and re-resolve lazily
|
|
124
|
+
* when landed on. Window-less legacy(1.x index-only) state degrades to a
|
|
125
|
+
* single-entry window.
|
|
75
126
|
*/
|
|
76
127
|
function restoreLocationStack(history) {
|
|
77
128
|
const state = history.location.state || {};
|
|
78
|
-
|
|
79
|
-
locationStack,
|
|
80
|
-
base
|
|
81
|
-
} = state;
|
|
82
|
-
return locationStack?.length ? [...new Array(base || 0), ...locationStack] : [getLocation({
|
|
129
|
+
return state.locationStack?.length ? [...state.locationStack] : [getLocation({
|
|
83
130
|
history
|
|
84
131
|
})];
|
|
85
132
|
}
|
|
86
133
|
|
|
87
134
|
/**
|
|
88
|
-
* Serialize the
|
|
89
|
-
*
|
|
135
|
+
* Serialize the memory window together with the absolute index of its
|
|
136
|
+
* first entry, so a refresh can restore it. The memory window is trimmed
|
|
137
|
+
* on every push, so it is already bounded by `maxStackDepth`; the cap
|
|
138
|
+
* only matters when `maxStackDepth` was lowered via
|
|
139
|
+
* {@link setOptions} after the fact.
|
|
90
140
|
*/
|
|
91
141
|
function serializeStack(router) {
|
|
92
142
|
const {
|
|
93
143
|
locationStack,
|
|
94
144
|
maxStackDepth
|
|
95
145
|
} = router;
|
|
96
|
-
const windowed = locationStack.slice(-maxStackDepth);
|
|
146
|
+
const windowed = locationStack.length > maxStackDepth ? locationStack.slice(-maxStackDepth) : locationStack;
|
|
97
147
|
return {
|
|
98
|
-
base: locationStack.length - windowed.length,
|
|
148
|
+
base: router.baseIndex + (locationStack.length - windowed.length),
|
|
99
149
|
locationStack: windowed
|
|
100
150
|
};
|
|
101
151
|
}
|
|
@@ -105,8 +155,17 @@ function getHistoryState(router) {
|
|
|
105
155
|
index: state.index || 0
|
|
106
156
|
};
|
|
107
157
|
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Physical(window-relative) view slot of an absolute history index.
|
|
161
|
+
* Slots before the window start(negative) or past its end read as
|
|
162
|
+
* `undefined`, driving the lazy refresh fallback of {@link listen}.
|
|
163
|
+
*/
|
|
164
|
+
function viewAt(router, index) {
|
|
165
|
+
return router.viewStack[index - router.baseIndex];
|
|
166
|
+
}
|
|
108
167
|
function getCurrentView(router) {
|
|
109
|
-
return router
|
|
168
|
+
return viewAt(router, getHistoryState(router).index);
|
|
110
169
|
}
|
|
111
170
|
|
|
112
171
|
/**
|
|
@@ -206,6 +265,158 @@ function resolveTo(router, to, state) {
|
|
|
206
265
|
return resolve(router, location);
|
|
207
266
|
}
|
|
208
267
|
|
|
268
|
+
/**
|
|
269
|
+
* Resolve a location through the route guards(`redirect`/`beforeLoad`).
|
|
270
|
+
*
|
|
271
|
+
* Guards run per matched level from the shallowest to the deepest. A guard
|
|
272
|
+
* returning a path string(redirect) restarts the resolution at the new
|
|
273
|
+
* location — from the shallowest level again, so guards of shallower
|
|
274
|
+
* levels re-run on every hop(keep side-effectful guards idempotent) —
|
|
275
|
+
* carrying the original user state; at most
|
|
276
|
+
* {@link MAX_REDIRECTS 10} redirects are followed before a
|
|
277
|
+
* {@link RedirectLoopError} is thrown. An unmatched pathname keeps the
|
|
278
|
+
* {@link resolve resolve} behavior: the task rejects with a
|
|
279
|
+
* {@link NotFoundError} and is routed through `router.errorHandler`.
|
|
280
|
+
*
|
|
281
|
+
* @group Methods
|
|
282
|
+
* @category Router
|
|
283
|
+
* @param router router instance
|
|
284
|
+
* @param location the location to resolve; the object itself is never
|
|
285
|
+
* mutated — a redirect rebinds the resolution to a new location
|
|
286
|
+
* @returns the terminal location and its resolve task
|
|
287
|
+
*/
|
|
288
|
+
async function resolveEntry(router, location) {
|
|
289
|
+
const {
|
|
290
|
+
resolveView,
|
|
291
|
+
errorHandler
|
|
292
|
+
} = router;
|
|
293
|
+
for (let redirects = 0;; redirects++) {
|
|
294
|
+
if (redirects > MAX_REDIRECTS) {
|
|
295
|
+
throw new RedirectLoopError(location.pathname);
|
|
296
|
+
}
|
|
297
|
+
const matched = match(router, location.pathname);
|
|
298
|
+
if (!matched) {
|
|
299
|
+
return {
|
|
300
|
+
location,
|
|
301
|
+
task: Promise.reject(new NotFoundError(location.pathname)).catch(errorHandler)
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
let redirected = false;
|
|
305
|
+
for (let i = 0; i < matched.length; i++) {
|
|
306
|
+
const {
|
|
307
|
+
route
|
|
308
|
+
} = matched[i];
|
|
309
|
+
// `redirect` wins over `beforeLoad`; a non-empty string target
|
|
310
|
+
// restarts the resolution at the redirected location.
|
|
311
|
+
const target = route.redirect ?? (
|
|
312
|
+
// eslint-disable-next-line no-await-in-loop -- guards must run in declaration order, sequentially
|
|
313
|
+
await route.beforeLoad?.({
|
|
314
|
+
router,
|
|
315
|
+
location,
|
|
316
|
+
params: mergeMatchedParams(matched, i)
|
|
317
|
+
}));
|
|
318
|
+
if (target) {
|
|
319
|
+
location = toLocation(router, target, location.state);
|
|
320
|
+
redirected = true;
|
|
321
|
+
break;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
// eslint-disable-next-line no-continue -- the redirect loop restarts the outer resolution pass
|
|
325
|
+
if (redirected) continue;
|
|
326
|
+
return {
|
|
327
|
+
location,
|
|
328
|
+
task: resolveView(matched, {
|
|
329
|
+
router,
|
|
330
|
+
location
|
|
331
|
+
}).catch(errorHandler)
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Resolve a target through the route guards(`redirect`/`beforeLoad`) and
|
|
338
|
+
* cache the result at the router level, keyed by `pathname + search`.
|
|
339
|
+
*
|
|
340
|
+
* Within its TTL(`opts.ttl`, default 30s) repeated and concurrent calls
|
|
341
|
+
* return the very same entry promise, so concurrent callers share one
|
|
342
|
+
* resolution(in-flight dedup) and repeated prefetches reuse the resolved
|
|
343
|
+
* view task instead of re-running guards and `resolveView`. A rejected
|
|
344
|
+
* resolution(guard error, redirect loop) is evicted from the cache, so
|
|
345
|
+
* the next call retries it. Committing a navigation({@link commit} or
|
|
346
|
+
* {@link commitReplace}) consumes the entry and evicts its cache slot —
|
|
347
|
+
* a later preload re-resolves fresh state, while callers still holding
|
|
348
|
+
* the old entry keep their references.
|
|
349
|
+
*
|
|
350
|
+
* @group Methods
|
|
351
|
+
* @category Router
|
|
352
|
+
* @param router router instance
|
|
353
|
+
* @param to path string
|
|
354
|
+
* @param opts options; `ttl` is the cache lifetime in milliseconds
|
|
355
|
+
* @returns the terminal location and its resolve task
|
|
356
|
+
*/
|
|
357
|
+
function preload(router, to, opts) {
|
|
358
|
+
const cache = preloadCacheOf(router);
|
|
359
|
+
const location = toLocation(router, to);
|
|
360
|
+
const key = preloadLocationKey(location);
|
|
361
|
+
const cached = cache.get(key);
|
|
362
|
+
if (cached && Date.now() < cached.expires) {
|
|
363
|
+
return cached.entry;
|
|
364
|
+
}
|
|
365
|
+
prunePreloadCache(cache);
|
|
366
|
+
const entry = resolveEntry(router, location);
|
|
367
|
+
const record = {
|
|
368
|
+
entry,
|
|
369
|
+
expires: Date.now() + (opts?.ttl ?? DEFAULT_PRELOAD_TTL)
|
|
370
|
+
};
|
|
371
|
+
cache.set(key, record);
|
|
372
|
+
entry.then(resolved => {
|
|
373
|
+
record.terminal = resolved.location;
|
|
374
|
+
}, () => {
|
|
375
|
+
// Never cache a failure: evict the slot(this record only, a newer
|
|
376
|
+
// preload may already have replaced it) so the next call retries.
|
|
377
|
+
if (cache.get(key)?.entry === entry) cache.delete(key);
|
|
378
|
+
});
|
|
379
|
+
return entry;
|
|
380
|
+
}
|
|
381
|
+
function preloadLocationKey(location) {
|
|
382
|
+
return location.pathname + location.search;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Drop expired records. Runs on every cache write, so distinct prefetched
|
|
387
|
+
* targets never accumulate beyond their TTL in a long session.
|
|
388
|
+
*/
|
|
389
|
+
function prunePreloadCache(cache) {
|
|
390
|
+
const now = Date.now();
|
|
391
|
+
cache.forEach((record, key) => {
|
|
392
|
+
if (record.expires <= now) cache.delete(key);
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Evict the cache slots consumed by a committed navigation. A redirecting
|
|
398
|
+
* entry is cached under its pre-redirect key while its terminal location
|
|
399
|
+
* differs, so records whose terminal resolves to the committed location
|
|
400
|
+
* are dropped too; in-flight records(terminal not yet known) are left to
|
|
401
|
+
* the TTL.
|
|
402
|
+
*/
|
|
403
|
+
function evictPreloadCache(router, location) {
|
|
404
|
+
const cache = preloadCacheOf(router);
|
|
405
|
+
const key = preloadLocationKey(location);
|
|
406
|
+
cache.delete(key);
|
|
407
|
+
cache.forEach((record, k) => {
|
|
408
|
+
if (record.terminal && preloadLocationKey(record.terminal) === key) {
|
|
409
|
+
cache.delete(k);
|
|
410
|
+
}
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
function preloadCacheOf(router) {
|
|
414
|
+
const core = router;
|
|
415
|
+
// create() always seeds the cache; the lazy path keeps hand-built
|
|
416
|
+
// router-shaped objects working.
|
|
417
|
+
return core.preloadCache ??= new Map();
|
|
418
|
+
}
|
|
419
|
+
|
|
209
420
|
/**
|
|
210
421
|
* Commit the resolve task and push history.
|
|
211
422
|
* @group Methods
|
|
@@ -215,13 +426,43 @@ function resolveTo(router, to, state) {
|
|
|
215
426
|
* @param location the location to resolved
|
|
216
427
|
*/
|
|
217
428
|
function commit(router, resolvePromise, location) {
|
|
429
|
+
// Wrap the raw task so external callers share the guarded entry
|
|
430
|
+
// pipeline; the entry location is the given one, as-is.
|
|
431
|
+
return pushEntry(router, Promise.resolve({
|
|
432
|
+
location,
|
|
433
|
+
task: resolvePromise
|
|
434
|
+
}), location);
|
|
435
|
+
}
|
|
436
|
+
function pushEntry(router, entryPromise, fromLocation) {
|
|
218
437
|
const {
|
|
219
438
|
history
|
|
220
439
|
} = router;
|
|
221
440
|
const nextIndex = getHistoryState(router).index + 1;
|
|
222
|
-
return commitBase(router,
|
|
223
|
-
|
|
224
|
-
|
|
441
|
+
return commitBase(router, entryPromise, fromLocation, (resolvedView, entry) => {
|
|
442
|
+
const {
|
|
443
|
+
location
|
|
444
|
+
} = entry;
|
|
445
|
+
let next = nextIndex - router.baseIndex;
|
|
446
|
+
if (next < 0 || next > router.locationStack.length) {
|
|
447
|
+
// The current entry sits outside the memory window(a push while an
|
|
448
|
+
// out-of-window lazy refresh is still pending): restart the window
|
|
449
|
+
// at the pushed position. Out-of-window neighbours re-resolve
|
|
450
|
+
// lazily when landed on.
|
|
451
|
+
router.baseIndex = nextIndex;
|
|
452
|
+
router.locationStack = [];
|
|
453
|
+
router.viewStack = [];
|
|
454
|
+
next = 0;
|
|
455
|
+
}
|
|
456
|
+
router.locationStack = [...router.locationStack.slice(0, next), location];
|
|
457
|
+
router.viewStack = [...router.viewStack.slice(0, next), resolvedView];
|
|
458
|
+
// Bound the memory window: evict the oldest entries once the stack
|
|
459
|
+
// outgrows maxStackDepth, shifting the window base along.
|
|
460
|
+
if (router.locationStack.length > router.maxStackDepth) {
|
|
461
|
+
const evicted = router.locationStack.length - router.maxStackDepth;
|
|
462
|
+
router.locationStack = router.locationStack.slice(evicted);
|
|
463
|
+
router.viewStack = router.viewStack.slice(evicted);
|
|
464
|
+
router.baseIndex += evicted;
|
|
465
|
+
}
|
|
225
466
|
history.push(location, {
|
|
226
467
|
index: nextIndex,
|
|
227
468
|
state: location.state,
|
|
@@ -239,15 +480,37 @@ function commit(router, resolvePromise, location) {
|
|
|
239
480
|
* @param location the location to resolved
|
|
240
481
|
*/
|
|
241
482
|
function commitReplace(router, resolvePromise, location) {
|
|
483
|
+
return replaceEntry(router, Promise.resolve({
|
|
484
|
+
location,
|
|
485
|
+
task: resolvePromise
|
|
486
|
+
}), location);
|
|
487
|
+
}
|
|
488
|
+
function replaceEntry(router, entryPromise, fromLocation) {
|
|
242
489
|
const {
|
|
243
490
|
history
|
|
244
491
|
} = router;
|
|
245
492
|
const {
|
|
246
493
|
index
|
|
247
494
|
} = getHistoryState(router);
|
|
248
|
-
return commitBase(router,
|
|
249
|
-
|
|
250
|
-
|
|
495
|
+
return commitBase(router, entryPromise, fromLocation, (resolvedView, entry) => {
|
|
496
|
+
const {
|
|
497
|
+
location
|
|
498
|
+
} = entry;
|
|
499
|
+
const physical = index - router.baseIndex;
|
|
500
|
+
if (physical < 0 || physical >= router.locationStack.length) {
|
|
501
|
+
// The landed entry is outside the memory window(the browser evicted
|
|
502
|
+
// older history past the window, or a window-less legacy state).
|
|
503
|
+
// Restart the window at the landed position — placeholders for the
|
|
504
|
+
// unknown gap slots are gone, so neighbouring out-of-window entries
|
|
505
|
+
// re-resolve lazily on every POP, consistent with the rare
|
|
506
|
+
// browser-evicted paths.
|
|
507
|
+
router.baseIndex = index;
|
|
508
|
+
router.locationStack = [location];
|
|
509
|
+
router.viewStack = [resolvedView];
|
|
510
|
+
} else {
|
|
511
|
+
router.locationStack[physical] = location;
|
|
512
|
+
router.viewStack[physical] = resolvedView;
|
|
513
|
+
}
|
|
251
514
|
history.replace(location, {
|
|
252
515
|
index,
|
|
253
516
|
state: location.state,
|
|
@@ -255,7 +518,7 @@ function commitReplace(router, resolvePromise, location) {
|
|
|
255
518
|
});
|
|
256
519
|
});
|
|
257
520
|
}
|
|
258
|
-
function commitBase(router,
|
|
521
|
+
function commitBase(router, entryPromise, location, onResolved) {
|
|
259
522
|
const {
|
|
260
523
|
currentGuard,
|
|
261
524
|
onLoadingChange = util.noop
|
|
@@ -266,16 +529,42 @@ function commitBase(router, resolvePromise, location, onResolved) {
|
|
|
266
529
|
}
|
|
267
530
|
router.resolving = location;
|
|
268
531
|
onLoadingChange('pending');
|
|
269
|
-
return
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
532
|
+
return (
|
|
533
|
+
// The whole chain — route guards AND the view task — is guarded from
|
|
534
|
+
// the very start: a superseding navigation or a cancel() while slow
|
|
535
|
+
// guards are still running parks this chain forever, exactly like
|
|
536
|
+
// during the view phase.
|
|
537
|
+
currentGuard(entryPromise.then(entry => entry.task.then(resolvedView => ({
|
|
538
|
+
entry,
|
|
539
|
+
resolvedView
|
|
540
|
+
})))).then(({
|
|
541
|
+
entry,
|
|
542
|
+
resolvedView
|
|
543
|
+
}) => {
|
|
544
|
+
onResolved(resolvedView, entry);
|
|
545
|
+
// The navigation consumed this resolution: drop its preload
|
|
546
|
+
// cache slots so a later preload re-resolves fresh state.
|
|
547
|
+
evictPreloadCache(router, entry.location);
|
|
548
|
+
}).then(() => {
|
|
549
|
+
// This chain settled, so it is no longer in flight. Superseded
|
|
550
|
+
// chains park forever, so only the latest chain can reach here:
|
|
551
|
+
// the mark is always ours to clear.
|
|
552
|
+
router.resolving = undefined;
|
|
553
|
+
onLoadingChange('resolved');
|
|
554
|
+
}).catch(e => {
|
|
555
|
+
router.resolving = undefined;
|
|
556
|
+
onLoadingChange('rejected');
|
|
557
|
+
throw e;
|
|
558
|
+
})
|
|
559
|
+
);
|
|
275
560
|
}
|
|
276
561
|
|
|
277
562
|
/**
|
|
278
|
-
* Navigate to a new path.
|
|
563
|
+
* Navigate to a new path. Route guards(`redirect`/`beforeLoad`) run before
|
|
564
|
+
* the view resolves; the history entry is committed on the terminal
|
|
565
|
+
* location when guards redirected. The guard phase is part of the
|
|
566
|
+
* cancelable navigation: a superseding navigate or a `cancel()` while
|
|
567
|
+
* guards are still running discards this navigation.
|
|
279
568
|
* @group Methods
|
|
280
569
|
* @category Router
|
|
281
570
|
* @param router router instance
|
|
@@ -284,20 +573,19 @@ function commitBase(router, resolvePromise, location, onResolved) {
|
|
|
284
573
|
*/
|
|
285
574
|
function navigate(router, to, state) {
|
|
286
575
|
const location = toLocation(router, to, state);
|
|
287
|
-
|
|
288
|
-
return commit(router, viewPromise, location);
|
|
576
|
+
return pushEntry(router, resolveEntry(router, location), location);
|
|
289
577
|
}
|
|
290
578
|
|
|
291
579
|
/**
|
|
292
|
-
* Refresh the page.
|
|
580
|
+
* Refresh the page. Route guards run before the view resolves; a redirect
|
|
581
|
+
* replaces the current entry with the terminal location.
|
|
293
582
|
* @group Methods
|
|
294
583
|
* @category Router
|
|
295
584
|
* @param router router instance
|
|
296
585
|
*/
|
|
297
586
|
function refresh(router) {
|
|
298
587
|
const location = getLocation(router);
|
|
299
|
-
|
|
300
|
-
return commitReplace(router, viewPromise, location);
|
|
588
|
+
return replaceEntry(router, resolveEntry(router, location), location);
|
|
301
589
|
}
|
|
302
590
|
|
|
303
591
|
/**
|
|
@@ -352,12 +640,13 @@ function createHref({
|
|
|
352
640
|
* @category Router
|
|
353
641
|
* @param router router instance
|
|
354
642
|
*/
|
|
355
|
-
function cancel({
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
643
|
+
function cancel(router) {
|
|
644
|
+
// The cancelled chain parks forever, so nothing else will clear the
|
|
645
|
+
// in-flight mark: drop it here, or a later navigation would fire a
|
|
646
|
+
// spurious cancel signal(`onLoadingChange()`) for a dead resolve.
|
|
647
|
+
router.resolving = undefined;
|
|
648
|
+
router.cancelAll();
|
|
649
|
+
router.onLoadingChange?.();
|
|
361
650
|
}
|
|
362
651
|
|
|
363
652
|
/**
|
|
@@ -372,7 +661,7 @@ function cancel({
|
|
|
372
661
|
* @param router router instance
|
|
373
662
|
*/
|
|
374
663
|
function initHistoryStack(router) {
|
|
375
|
-
return Promise.all(router.locationStack.map(location =>
|
|
664
|
+
return Promise.all(router.locationStack.map(location => resolve(router, location))).then(views => {
|
|
376
665
|
router.viewStack = views;
|
|
377
666
|
});
|
|
378
667
|
}
|
|
@@ -396,13 +685,15 @@ function listen(router, onViewChange) {
|
|
|
396
685
|
cancel(router);
|
|
397
686
|
const state = location.state;
|
|
398
687
|
const index = state?.index || 0;
|
|
399
|
-
const view = router
|
|
688
|
+
const view = viewAt(router, index);
|
|
400
689
|
onViewChange(view);
|
|
401
690
|
if (!view) {
|
|
402
|
-
// Lazy fallback for
|
|
403
|
-
// re-resolving the landed entry also re-serializes the
|
|
404
|
-
// into it via the replace commit.
|
|
405
|
-
|
|
691
|
+
// Lazy fallback for out-of-window slots and window-less legacy
|
|
692
|
+
// state: re-resolving the landed entry also re-serializes the
|
|
693
|
+
// window into it via the replace commit. A guard failure here
|
|
694
|
+
// must not surface as an unhandled rejection — the landed entry
|
|
695
|
+
// simply keeps its(unknown) view.
|
|
696
|
+
refresh(router).catch(util.noop);
|
|
406
697
|
} else if (action === 'POP') {
|
|
407
698
|
// Sync the current window into the landed entry so a later
|
|
408
699
|
// refresh("refresh → back → refresh again") still restores it.
|
|
@@ -421,15 +712,20 @@ function listen(router, onViewChange) {
|
|
|
421
712
|
}
|
|
422
713
|
|
|
423
714
|
/**
|
|
424
|
-
* Merge params of
|
|
715
|
+
* Merge params of the matched levels. Params of deeper levels override
|
|
425
716
|
* the same keys of shallower ones.
|
|
717
|
+
*
|
|
718
|
+
* When `end` is given, only the levels up to and including `end` are
|
|
719
|
+
* merged — the accumulated params a level at index `end` sees(shallow →
|
|
720
|
+
* current level). Omitting `end` merges every level.
|
|
426
721
|
* @group Methods
|
|
427
722
|
* @category Router
|
|
428
723
|
* @param matched matched route levels, see {@link match}
|
|
724
|
+
* @param end the index of the last level to merge, defaults to the deepest
|
|
429
725
|
* @returns the merged params object
|
|
430
726
|
*/
|
|
431
|
-
function mergeMatchedParams(matched) {
|
|
432
|
-
return matched.reduce((params, {
|
|
727
|
+
function mergeMatchedParams(matched, end) {
|
|
728
|
+
return matched.slice(0, end === undefined ? matched.length : end + 1).reduce((params, {
|
|
433
729
|
params: levelParams
|
|
434
730
|
}) => ({
|
|
435
731
|
...params,
|
|
@@ -438,7 +734,11 @@ function mergeMatchedParams(matched) {
|
|
|
438
734
|
}
|
|
439
735
|
|
|
440
736
|
/**
|
|
441
|
-
* Get current route params from router.
|
|
737
|
+
* Get current route params from router. The params are re-derived by
|
|
738
|
+
* matching the current entry of {@link RouterInstance.locationStack} so
|
|
739
|
+
* they stay correct even when the view stack holds resolved views
|
|
740
|
+
* (e.g. React elements) instead of match results. Merges params of all
|
|
741
|
+
* matched levels; deeper levels override shallower ones.
|
|
442
742
|
* @group Methods
|
|
443
743
|
* @category Router
|
|
444
744
|
* @param router router instance
|
|
@@ -448,13 +748,14 @@ function getParams(router) {
|
|
|
448
748
|
const {
|
|
449
749
|
index
|
|
450
750
|
} = getHistoryState(router);
|
|
451
|
-
const
|
|
452
|
-
if (!
|
|
453
|
-
return mergeMatchedParams(
|
|
751
|
+
const location = router.locationStack[index - router.baseIndex];
|
|
752
|
+
if (!location) return {};
|
|
753
|
+
return mergeMatchedParams(match(router, location.pathname) ?? []);
|
|
454
754
|
}
|
|
455
755
|
|
|
456
756
|
exports.NativeRouterError = NativeRouterError;
|
|
457
757
|
exports.NotFoundError = NotFoundError;
|
|
758
|
+
exports.RedirectLoopError = RedirectLoopError;
|
|
458
759
|
exports.back = back;
|
|
459
760
|
exports.cancel = cancel;
|
|
460
761
|
exports.commit = commit;
|
|
@@ -471,8 +772,10 @@ exports.listen = listen;
|
|
|
471
772
|
exports.match = match;
|
|
472
773
|
exports.mergeMatchedParams = mergeMatchedParams;
|
|
473
774
|
exports.navigate = navigate;
|
|
775
|
+
exports.preload = preload;
|
|
474
776
|
exports.refresh = refresh;
|
|
475
777
|
exports.resolve = resolve;
|
|
778
|
+
exports.resolveEntry = resolveEntry;
|
|
476
779
|
exports.resolveTo = resolveTo;
|
|
477
780
|
exports.setOptions = setOptions;
|
|
478
781
|
exports.toLocation = toLocation;
|