-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathrouter.ts
1116 lines (933 loc) · 34 KB
/
router.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import RouteRecognizer, { MatchCallback, Params, QueryParams } from 'route-recognizer';
import { Promise } from 'rsvp';
import { Dict, Maybe, Option } from './core';
import InternalRouteInfo, {
ModelFor,
Route,
RouteInfo,
RouteInfoWithAttributes,
toReadOnlyRouteInfo,
} from './route-info';
import InternalTransition, {
logAbort,
OpaqueTransition,
PublicTransition as Transition,
QUERY_PARAMS_SYMBOL,
STATE_SYMBOL,
} from './transition';
import { throwIfAborted, isTransitionAborted } from './transition-aborted-error';
import { TransitionIntent } from './transition-intent';
import NamedTransitionIntent from './transition-intent/named-transition-intent';
import URLTransitionIntent from './transition-intent/url-transition-intent';
import TransitionState, { TransitionError } from './transition-state';
import {
ChangeList,
extractQueryParams,
forEach,
getChangelist,
log,
merge,
ModelsAndQueryParams,
promiseLabel,
} from './utils';
export interface SerializerFunc<T> {
(model: T, params: string[]): Dict<unknown>;
}
export interface ParsedHandler {
handler: string;
names: string[];
}
export default abstract class Router<R extends Route> {
private _lastQueryParams = {};
log?: (message: string) => void;
state?: TransitionState<R> = undefined;
oldState: Maybe<TransitionState<R>> = undefined;
activeTransition?: InternalTransition<R> = undefined;
currentRouteInfos?: InternalRouteInfo<R>[] = undefined;
_changedQueryParams?: Dict<unknown> = undefined;
currentSequence = 0;
recognizer: RouteRecognizer;
constructor(logger?: (message: string) => void) {
this.log = logger;
this.recognizer = new RouteRecognizer();
this.reset();
}
abstract getRoute(name: string): R | Promise<R>;
abstract getSerializer(name: string): SerializerFunc<ModelFor<R>> | undefined;
abstract updateURL(url: string): void;
abstract replaceURL(url: string): void;
abstract willTransition(
oldRouteInfos: InternalRouteInfo<R>[],
newRouteInfos: InternalRouteInfo<R>[],
transition: Transition
): void;
abstract didTransition(routeInfos: InternalRouteInfo<R>[]): void;
abstract triggerEvent(
routeInfos: InternalRouteInfo<R>[],
ignoreFailure: boolean,
name: string,
args: unknown[]
): void;
abstract routeWillChange(transition: Transition): void;
abstract routeDidChange(transition: Transition): void;
abstract transitionDidError(error: TransitionError, transition: Transition): Transition | Error;
/**
The main entry point into the router. The API is essentially
the same as the `map` method in `route-recognizer`.
This method extracts the String handler at the last `.to()`
call and uses it as the name of the whole route.
@param {Function} callback
*/
map(callback: MatchCallback) {
this.recognizer.map(callback, function (recognizer, routes) {
for (let i = routes.length - 1, proceed = true; i >= 0 && proceed; --i) {
let route = routes[i];
let handler = route.handler as string;
recognizer.add(routes, { as: handler });
proceed = route.path === '/' || route.path === '' || handler.slice(-6) === '.index';
}
});
}
hasRoute(route: string) {
return this.recognizer.hasRoute(route);
}
queryParamsTransition(
changelist: ChangeList,
wasTransitioning: boolean,
oldState: TransitionState<R>,
newState: TransitionState<R>
): OpaqueTransition {
this.fireQueryParamDidChange(newState, changelist);
if (!wasTransitioning && this.activeTransition) {
// One of the routes in queryParamsDidChange
// caused a transition. Just return that transition.
return this.activeTransition;
} else {
// Running queryParamsDidChange didn't change anything.
// Just update query params and be on our way.
// We have to return a noop transition that will
// perform a URL update at the end. This gives
// the user the ability to set the url update
// method (default is replaceState).
let newTransition = new InternalTransition(this, undefined, undefined);
newTransition.queryParamsOnly = true;
oldState.queryParams = this.finalizeQueryParamChange(
newState.routeInfos,
newState.queryParams,
newTransition
);
newTransition[QUERY_PARAMS_SYMBOL] = newState.queryParams;
this.toReadOnlyInfos(newTransition, newState);
this.routeWillChange(newTransition);
newTransition.promise = newTransition.promise!.then(
(result: TransitionState<R> | Route | Error | undefined) => {
if (!newTransition.isAborted) {
this._updateURL(newTransition, oldState);
this.didTransition(this.currentRouteInfos!);
this.toInfos(newTransition, newState.routeInfos, true);
this.routeDidChange(newTransition);
}
return result;
},
null,
promiseLabel('Transition complete')
);
return newTransition;
}
}
transitionByIntent(intent: TransitionIntent<R>, isIntermediate: boolean): InternalTransition<R> {
try {
return this.getTransitionByIntent(intent, isIntermediate);
} catch (e) {
return new InternalTransition(this, intent, undefined, e, undefined);
}
}
recognize(url: string): Option<RouteInfo> {
let intent = new URLTransitionIntent<R>(this, url);
let newState = this.generateNewState(intent);
if (newState === null) {
return newState;
}
let readonlyInfos = toReadOnlyRouteInfo(newState.routeInfos, newState.queryParams);
return readonlyInfos[readonlyInfos.length - 1] as RouteInfo;
}
recognizeAndLoad(url: string): Promise<RouteInfoWithAttributes> {
let intent = new URLTransitionIntent<R>(this, url);
let newState = this.generateNewState(intent);
if (newState === null) {
return Promise.reject(`URL ${url} was not recognized`);
}
let newTransition: OpaqueTransition = new InternalTransition(this, intent, newState, undefined);
return newTransition.then(() => {
let routeInfosWithAttributes = toReadOnlyRouteInfo(
newState!.routeInfos,
newTransition[QUERY_PARAMS_SYMBOL],
true
) as RouteInfoWithAttributes[];
return routeInfosWithAttributes[routeInfosWithAttributes.length - 1];
});
}
private generateNewState(intent: TransitionIntent<R>): Option<TransitionState<R>> {
try {
return intent.applyToState(this.state!, false);
} catch (e) {
return null;
}
}
private getTransitionByIntent(
intent: TransitionIntent<R>,
isIntermediate: boolean
): InternalTransition<R> {
let wasTransitioning = !!this.activeTransition;
let oldState = wasTransitioning ? this.activeTransition![STATE_SYMBOL] : this.state;
let newTransition: InternalTransition<R>;
let newState = intent.applyToState(oldState!, isIntermediate);
let queryParamChangelist = getChangelist(oldState!.queryParams, newState.queryParams);
if (routeInfosEqual(newState.routeInfos, oldState!.routeInfos)) {
// This is a no-op transition. See if query params changed.
if (queryParamChangelist) {
let newTransition = this.queryParamsTransition(
queryParamChangelist,
wasTransitioning,
oldState!,
newState
);
newTransition.queryParamsOnly = true;
// SAFETY: The returned OpaqueTransition should actually be this.
return newTransition as InternalTransition<R>;
}
// No-op. No need to create a new transition.
return this.activeTransition || new InternalTransition(this, undefined, undefined);
}
if (isIntermediate) {
let transition = new InternalTransition(this, undefined, newState);
transition.isIntermediate = true;
this.toReadOnlyInfos(transition, newState);
this.setupContexts(newState, transition);
this.routeWillChange(transition);
return this.activeTransition!;
}
// Create a new transition to the destination route.
newTransition = new InternalTransition(
this,
intent,
newState,
undefined,
this.activeTransition
);
// transition is to same route with same params, only query params differ.
// not caught above probably because refresh() has been used
if (routeInfosSameExceptQueryParams(newState.routeInfos, oldState!.routeInfos)) {
newTransition.queryParamsOnly = true;
}
this.toReadOnlyInfos(newTransition, newState);
// Abort and usurp any previously active transition.
if (this.activeTransition) {
this.activeTransition.redirect(newTransition);
}
this.activeTransition = newTransition;
// Transition promises by default resolve with resolved state.
// For our purposes, swap out the promise to resolve
// after the transition has been finalized.
newTransition.promise = newTransition.promise!.then(
(result: TransitionState<R>) => {
return this.finalizeTransition(newTransition, result);
},
null,
promiseLabel('Settle transition promise when transition is finalized')
);
if (!wasTransitioning) {
this.notifyExistingHandlers(newState, newTransition);
}
this.fireQueryParamDidChange(newState, queryParamChangelist!);
return newTransition;
}
/**
@private
Begins and returns a Transition based on the provided
arguments. Accepts arguments in the form of both URL
transitions and named transitions.
@param {Router} router
@param {Array[Object]} args arguments passed to transitionTo,
replaceWith, or handleURL
*/
private doTransition(
name?: string,
modelsArray: [...ModelFor<R>[]] | [...ModelFor<R>[], { queryParams: QueryParams }] = [],
isIntermediate = false
): InternalTransition<R> {
let lastArg = modelsArray[modelsArray.length - 1];
let queryParams: Dict<unknown> = {};
if (lastArg && Object.prototype.hasOwnProperty.call(lastArg, 'queryParams')) {
// We just checked this.
// TODO: Use an assertion?
queryParams = (modelsArray.pop() as { queryParams: QueryParams }).queryParams as Dict<
unknown
>;
}
let intent;
if (name === undefined) {
log(this, 'Updating query params');
// A query param update is really just a transition
// into the route you're already on.
let { routeInfos } = this.state!;
intent = new NamedTransitionIntent<R>(
this,
routeInfos[routeInfos.length - 1].name,
undefined,
[],
queryParams
);
} else if (name.charAt(0) === '/') {
log(this, 'Attempting URL transition to ' + name);
intent = new URLTransitionIntent<R>(this, name);
} else {
log(this, 'Attempting transition to ' + name);
intent = new NamedTransitionIntent<R>(
this,
name,
undefined,
// SAFETY: We know this to be the case since we removed the last item if it was QPs
modelsArray as ModelFor<R>[],
queryParams
);
}
return this.transitionByIntent(intent, isIntermediate);
}
/**
@private
Updates the URL (if necessary) and calls `setupContexts`
to update the router's array of `currentRouteInfos`.
*/
private finalizeTransition(
transition: InternalTransition<R>,
newState: TransitionState<R>
): R | Promise<any> {
try {
log(
transition.router,
transition.sequence,
'Resolved all models on destination route; finalizing transition.'
);
let routeInfos = newState.routeInfos;
// Run all the necessary enter/setup/exit hooks
this.setupContexts(newState, transition);
// Check if a redirect occurred in enter/setup
if (transition.isAborted) {
// TODO: cleaner way? distinguish b/w targetRouteInfos?
this.state!.routeInfos = this.currentRouteInfos!;
return Promise.reject(logAbort(transition));
}
this._updateURL(transition, newState);
transition.isActive = false;
this.activeTransition = undefined;
this.triggerEvent(this.currentRouteInfos!, true, 'didTransition', []);
this.didTransition(this.currentRouteInfos!);
this.toInfos(transition, newState.routeInfos, true);
this.routeDidChange(transition);
log(this, transition.sequence, 'TRANSITION COMPLETE.');
// Resolve with the final route.
return routeInfos[routeInfos.length - 1].route!;
} catch (e) {
if (!isTransitionAborted(e)) {
let infos = transition[STATE_SYMBOL]!.routeInfos;
transition.trigger(true, 'error', e, transition, infos[infos.length - 1].route);
transition.abort();
}
throw e;
}
}
/**
@private
Takes an Array of `RouteInfo`s, figures out which ones are
exiting, entering, or changing contexts, and calls the
proper route hooks.
For example, consider the following tree of routes. Each route is
followed by the URL segment it handles.
```
|~index ("/")
| |~posts ("/posts")
| | |-showPost ("/:id")
| | |-newPost ("/new")
| | |-editPost ("/edit")
| |~about ("/about/:id")
```
Consider the following transitions:
1. A URL transition to `/posts/1`.
1. Triggers the `*model` callbacks on the
`index`, `posts`, and `showPost` routes
2. Triggers the `enter` callback on the same
3. Triggers the `setup` callback on the same
2. A direct transition to `newPost`
1. Triggers the `exit` callback on `showPost`
2. Triggers the `enter` callback on `newPost`
3. Triggers the `setup` callback on `newPost`
3. A direct transition to `about` with a specified
context object
1. Triggers the `exit` callback on `newPost`
and `posts`
2. Triggers the `serialize` callback on `about`
3. Triggers the `enter` callback on `about`
4. Triggers the `setup` callback on `about`
@param {Router} transition
@param {TransitionState} newState
*/
private setupContexts(newState: TransitionState<R>, transition?: InternalTransition<R>) {
let partition = this.partitionRoutes(this.state!, newState);
let i, l, route;
for (i = 0, l = partition.exited.length; i < l; i++) {
route = partition.exited[i].route;
delete route!.context;
if (route !== undefined) {
if (route._internalReset !== undefined) {
route._internalReset(true, transition);
}
if (route.exit !== undefined) {
route.exit(transition);
}
}
}
let oldState = (this.oldState = this.state);
this.state = newState;
let currentRouteInfos = (this.currentRouteInfos = partition.unchanged.slice());
try {
for (i = 0, l = partition.reset.length; i < l; i++) {
route = partition.reset[i].route;
if (route !== undefined) {
if (route._internalReset !== undefined) {
route._internalReset(false, transition);
}
}
}
for (i = 0, l = partition.updatedContext.length; i < l; i++) {
this.routeEnteredOrUpdated(
currentRouteInfos,
partition.updatedContext[i],
false,
transition!
);
}
for (i = 0, l = partition.entered.length; i < l; i++) {
this.routeEnteredOrUpdated(currentRouteInfos, partition.entered[i], true, transition!);
}
} catch (e) {
this.state = oldState;
this.currentRouteInfos = oldState!.routeInfos;
throw e;
}
this.state.queryParams = this.finalizeQueryParamChange(
currentRouteInfos,
newState.queryParams,
transition!
);
}
/**
@private
Fires queryParamsDidChange event
*/
private fireQueryParamDidChange(newState: TransitionState<R>, queryParamChangelist: ChangeList) {
// If queryParams changed trigger event
if (queryParamChangelist) {
// This is a little hacky but we need some way of storing
// changed query params given that no activeTransition
// is guaranteed to have occurred.
this._changedQueryParams = queryParamChangelist.all;
this.triggerEvent(newState.routeInfos, true, 'queryParamsDidChange', [
queryParamChangelist.changed,
queryParamChangelist.all,
queryParamChangelist.removed,
]);
this._changedQueryParams = undefined;
}
}
/**
@private
Helper method used by setupContexts. Handles errors or redirects
that may happen in enter/setup.
*/
private routeEnteredOrUpdated(
currentRouteInfos: InternalRouteInfo<R>[],
routeInfo: InternalRouteInfo<R>,
enter: boolean,
transition?: InternalTransition<R>
) {
let route = routeInfo.route,
context = routeInfo.context;
function _routeEnteredOrUpdated(route: R) {
if (enter) {
if (route.enter !== undefined) {
route.enter(transition!);
}
}
throwIfAborted(transition);
route.context = context as Awaited<typeof context>;
if (route.contextDidChange !== undefined) {
route.contextDidChange();
}
if (route.setup !== undefined) {
route.setup(context!, transition!);
}
throwIfAborted(transition);
currentRouteInfos.push(routeInfo);
return route;
}
// If the route doesn't exist, it means we haven't resolved the route promise yet
if (route === undefined) {
routeInfo.routePromise = routeInfo.routePromise.then(_routeEnteredOrUpdated);
} else {
_routeEnteredOrUpdated(route);
}
return true;
}
/**
@private
This function is called when transitioning from one URL to
another to determine which routes are no longer active,
which routes are newly active, and which routes remain
active but have their context changed.
Take a list of old routes and new routes and partition
them into four buckets:
* unchanged: the route was active in both the old and
new URL, and its context remains the same
* updated context: the route was active in both the
old and new URL, but its context changed. The route's
`setup` method, if any, will be called with the new
context.
* exited: the route was active in the old URL, but is
no longer active.
* entered: the route was not active in the old URL, but
is now active.
The PartitionedRoutes structure has four fields:
* `updatedContext`: a list of `RouteInfo` objects that
represent routes that remain active but have a changed
context
* `entered`: a list of `RouteInfo` objects that represent
routes that are newly active
* `exited`: a list of `RouteInfo` objects that are no
longer active.
* `unchanged`: a list of `RouteInfo` objects that remain active.
@param {Array[InternalRouteInfo]} oldRoutes a list of the route
information for the previous URL (or `[]` if this is the
first handled transition)
@param {Array[InternalRouteInfo]} newRoutes a list of the route
information for the new URL
@return {Partition}
*/
private partitionRoutes(oldState: TransitionState<R>, newState: TransitionState<R>) {
let oldRouteInfos = oldState.routeInfos;
let newRouteInfos = newState.routeInfos;
let routes: RoutePartition<R> = {
updatedContext: [],
exited: [],
entered: [],
unchanged: [],
reset: [],
};
let routeChanged,
contextChanged = false,
i,
l;
for (i = 0, l = newRouteInfos.length; i < l; i++) {
let oldRouteInfo = oldRouteInfos[i],
newRouteInfo = newRouteInfos[i];
if (!oldRouteInfo || oldRouteInfo.route !== newRouteInfo.route) {
routeChanged = true;
}
if (routeChanged) {
routes.entered.push(newRouteInfo);
if (oldRouteInfo) {
routes.exited.unshift(oldRouteInfo);
}
} else if (contextChanged || oldRouteInfo.context !== newRouteInfo.context) {
contextChanged = true;
routes.updatedContext.push(newRouteInfo);
} else {
routes.unchanged.push(oldRouteInfo);
}
}
for (i = newRouteInfos.length, l = oldRouteInfos.length; i < l; i++) {
routes.exited.unshift(oldRouteInfos[i]);
}
routes.reset = routes.updatedContext.slice();
routes.reset.reverse();
return routes;
}
private _updateURL(transition: OpaqueTransition, state: TransitionState<R>) {
let urlMethod: string | null = transition.urlMethod;
if (!urlMethod) {
return;
}
let { routeInfos } = state;
let { name: routeName } = routeInfos[routeInfos.length - 1];
let params: Dict<unknown> = {};
for (let i = routeInfos.length - 1; i >= 0; --i) {
let routeInfo = routeInfos[i];
merge(params, routeInfo.params);
if (routeInfo.route!.inaccessibleByURL) {
urlMethod = null;
}
}
if (urlMethod) {
params.queryParams = transition._visibleQueryParams || state.queryParams;
let url = this.recognizer.generate(routeName, params as Params);
// transitions during the initial transition must always use replaceURL.
// When the app boots, you are at a url, e.g. /foo. If some route
// redirects to bar as part of the initial transition, you don't want to
// add a history entry for /foo. If you do, pressing back will immediately
// hit the redirect again and take you back to /bar, thus killing the back
// button
let initial = transition.isCausedByInitialTransition;
// say you are at / and you click a link to route /foo. In /foo's
// route, the transition is aborted using replaceWith('/bar').
// Because the current url is still /, the history entry for / is
// removed from the history. Clicking back will take you to the page
// you were on before /, which is often not even the app, thus killing
// the back button. That's why updateURL is always correct for an
// aborting transition that's not the initial transition
let replaceAndNotAborting =
urlMethod === 'replace' && !transition.isCausedByAbortingTransition;
// because calling refresh causes an aborted transition, this needs to be
// special cased - if the initial transition is a replace transition, the
// urlMethod should be honored here.
let isQueryParamsRefreshTransition = transition.queryParamsOnly && urlMethod === 'replace';
// say you are at / and you a `replaceWith(/foo)` is called. Then, that
// transition is aborted with `replaceWith(/bar)`. At the end, we should
// end up with /bar replacing /. We are replacing the replace. We only
// will replace the initial route if all subsequent aborts are also
// replaces. However, there is some ambiguity around the correct behavior
// here.
let replacingReplace =
urlMethod === 'replace' && transition.isCausedByAbortingReplaceTransition;
if (initial || replaceAndNotAborting || isQueryParamsRefreshTransition || replacingReplace) {
this.replaceURL!(url);
} else {
this.updateURL(url);
}
}
}
private finalizeQueryParamChange(
resolvedHandlers: InternalRouteInfo<R>[],
newQueryParams: Dict<unknown>,
transition: OpaqueTransition
) {
// We fire a finalizeQueryParamChange event which
// gives the new route hierarchy a chance to tell
// us which query params it's consuming and what
// their final values are. If a query param is
// no longer consumed in the final route hierarchy,
// its serialized segment will be removed
// from the URL.
for (let k in newQueryParams) {
if (newQueryParams.hasOwnProperty(k) && newQueryParams[k] === null) {
delete newQueryParams[k];
}
}
let finalQueryParamsArray: {
key: string;
value: string;
visible: boolean;
}[] = [];
this.triggerEvent(resolvedHandlers, true, 'finalizeQueryParamChange', [
newQueryParams,
finalQueryParamsArray,
transition,
]);
if (transition) {
transition._visibleQueryParams = {};
}
let finalQueryParams: Dict<unknown> = {};
for (let i = 0, len = finalQueryParamsArray.length; i < len; ++i) {
let qp = finalQueryParamsArray[i];
finalQueryParams[qp.key] = qp.value;
if (transition && qp.visible !== false) {
transition._visibleQueryParams[qp.key] = qp.value;
}
}
return finalQueryParams;
}
private toReadOnlyInfos(newTransition: OpaqueTransition, newState: TransitionState<R>) {
let oldRouteInfos = this.state!.routeInfos;
this.fromInfos(newTransition, oldRouteInfos);
this.toInfos(newTransition, newState.routeInfos);
this._lastQueryParams = newState.queryParams;
}
private fromInfos(newTransition: OpaqueTransition, oldRouteInfos: InternalRouteInfo<R>[]) {
if (newTransition !== undefined && oldRouteInfos.length > 0) {
let fromInfos = toReadOnlyRouteInfo(
oldRouteInfos,
Object.assign({}, this._lastQueryParams),
true
) as RouteInfoWithAttributes[];
newTransition!.from = fromInfos[fromInfos.length - 1] || null;
}
}
public toInfos(
newTransition: OpaqueTransition,
newRouteInfos: InternalRouteInfo<R>[],
includeAttributes = false
) {
if (newTransition !== undefined && newRouteInfos.length > 0) {
let toInfos = toReadOnlyRouteInfo(
newRouteInfos,
Object.assign({}, newTransition[QUERY_PARAMS_SYMBOL]),
includeAttributes
);
newTransition!.to = toInfos[toInfos.length - 1] || null;
}
}
private notifyExistingHandlers(
newState: TransitionState<R>,
newTransition: InternalTransition<R>
) {
let oldRouteInfos = this.state!.routeInfos,
changing = [],
i,
oldRouteInfoLen,
oldHandler,
newRouteInfo;
oldRouteInfoLen = oldRouteInfos.length;
for (i = 0; i < oldRouteInfoLen; i++) {
oldHandler = oldRouteInfos[i];
newRouteInfo = newState.routeInfos[i];
if (!newRouteInfo || oldHandler.name !== newRouteInfo.name) {
break;
}
if (!newRouteInfo.isResolved) {
changing.push(oldHandler);
}
}
this.triggerEvent(oldRouteInfos, true, 'willTransition', [newTransition]);
this.routeWillChange(newTransition);
this.willTransition(oldRouteInfos, newState.routeInfos, newTransition);
}
/**
Clears the current and target route routes and triggers exit
on each of them starting at the leaf and traversing up through
its ancestors.
*/
reset() {
if (this.state) {
forEach<InternalRouteInfo<R>>(this.state.routeInfos.slice().reverse(), function (routeInfo) {
let route = routeInfo.route;
if (route !== undefined) {
if (route.exit !== undefined) {
route.exit();
}
}
return true;
});
}
this.oldState = undefined;
this.state = new TransitionState();
this.currentRouteInfos = undefined;
}
/**
let handler = routeInfo.handler;
The entry point for handling a change to the URL (usually
via the back and forward button).
Returns an Array of handlers and the parameters associated
with those parameters.
@param {String} url a URL to process
@return {Array} an Array of `[handler, parameter]` tuples
*/
handleURL(url: string) {
// Perform a URL-based transition, but don't change
// the URL afterward, since it already happened.
if (url.charAt(0) !== '/') {
url = '/' + url;
}
return this.doTransition(url)!.method(null);
}
/**
Transition into the specified named route.
If necessary, trigger the exit callback on any routes
that are no longer represented by the target route.
@param {String} name the name of the route
*/
transitionTo(name: string | { queryParams: Dict<unknown> }, ...contexts: any[]) {
if (typeof name === 'object') {
contexts.push(name);
return this.doTransition(undefined, contexts, false);
}
return this.doTransition(name, contexts);
}
intermediateTransitionTo(name: string, ...args: any[]) {
return this.doTransition(name, args, true);
}
refresh(pivotRoute?: R) {
let previousTransition = this.activeTransition;
let state = previousTransition ? previousTransition[STATE_SYMBOL] : this.state;
let routeInfos = state!.routeInfos;
if (pivotRoute === undefined) {
pivotRoute = routeInfos[0].route;
}
log(this, 'Starting a refresh transition');
let name = routeInfos[routeInfos.length - 1].name;
let intent = new NamedTransitionIntent(
this,
name,
pivotRoute,
[],
this._changedQueryParams || state!.queryParams
);
let newTransition = this.transitionByIntent(intent, false);
// if the previous transition is a replace transition, that needs to be preserved
if (previousTransition && previousTransition.urlMethod === 'replace') {
newTransition.method(previousTransition.urlMethod);
}
return newTransition;
}
/**
Identical to `transitionTo` except that the current URL will be replaced
if possible.
This method is intended primarily for use with `replaceState`.
@param {String} name the name of the route
*/
replaceWith(name: string) {
return this.doTransition(name).method('replace');
}
/**
Take a named route and context objects and generate a
URL.
@param {String} name the name of the route to generate
a URL for
@param {...Object} objects a list of objects to serialize
@return {String} a URL
*/
generate(routeName: string, ...args: ModelsAndQueryParams<ModelFor<R>>) {
let partitionedArgs = extractQueryParams(args),
suppliedParams = partitionedArgs[0],
queryParams = partitionedArgs[1];
// Construct a TransitionIntent with the provided params
// and apply it to the present state of the router.
let intent = new NamedTransitionIntent(this, routeName, undefined, suppliedParams);
let state = intent.applyToState(this.state!, false);
let params: Params = {};
for (let i = 0, len = state.routeInfos.length; i < len; ++i) {
let routeInfo = state.routeInfos[i];
let routeParams = routeInfo.serialize();
merge(params, routeParams);
}
params.queryParams = queryParams;
return this.recognizer.generate(routeName, params);
}
applyIntent(routeName: string, contexts: ModelFor<R>[]): TransitionState<R> {
let intent = new NamedTransitionIntent(this, routeName, undefined, contexts);
let state = (this.activeTransition && this.activeTransition[STATE_SYMBOL]) || this.state!;
return intent.applyToState(state, false);
}
isActiveIntent(
routeName: string,
contexts: ModelFor<R>[],
queryParams?: Dict<unknown> | null,
_state?: TransitionState<R>
) {
let state = _state || this.state!,
targetRouteInfos = state.routeInfos,
routeInfo,
len;
if (!targetRouteInfos.length) {
return false;
}
let targetHandler = targetRouteInfos[targetRouteInfos.length - 1].name;
let recognizerHandlers: ParsedHandler[] = this.recognizer.handlersFor(targetHandler);
let index = 0;
for (len = recognizerHandlers.length; index < len; ++index) {
routeInfo = targetRouteInfos[index];
if (routeInfo.name === routeName) {
break;
}
}