@ngxs/router-plugin 3.7.5 → 3.7.6-dev.master-a585f14

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.
@@ -1,4 +1,4 @@
1
- import { NgZone, Injectable, NgModule } from '@angular/core';
1
+ import { InjectionToken, NgZone, Injector, Injectable, NgModule } from '@angular/core';
2
2
  import { Store, Action, Selector, State, NgxsModule } from '@ngxs/store';
3
3
  import { __assign, __decorate, __metadata } from 'tslib';
4
4
  import { NavigationStart, RoutesRecognized, ResolveEnd, NavigationCancel, NavigationError, NavigationEnd, Router } from '@angular/router';
@@ -47,7 +47,7 @@ if (false) {
47
47
  *
48
48
  */
49
49
  /**
50
- * An action dispatched when the router navigates.
50
+ * An action dispatched when the router starts the navigation.
51
51
  * @template T
52
52
  */
53
53
  var /**
@@ -56,6 +56,42 @@ var /**
56
56
  *
57
57
  */
58
58
  /**
59
+ * An action dispatched when the router starts the navigation.
60
+ * @template T
61
+ */
62
+ RouterRequest = /** @class */ (function () {
63
+ function RouterRequest(routerState, event, trigger) {
64
+ if (trigger === void 0) { trigger = 'none'; }
65
+ this.routerState = routerState;
66
+ this.event = event;
67
+ this.trigger = trigger;
68
+ }
69
+ Object.defineProperty(RouterRequest, "type", {
70
+ get: /**
71
+ * @return {?}
72
+ */
73
+ function () {
74
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
75
+ return '[Router] RouterRequest';
76
+ },
77
+ enumerable: true,
78
+ configurable: true
79
+ });
80
+ return RouterRequest;
81
+ }());
82
+ if (false) {
83
+ /** @type {?} */
84
+ RouterRequest.prototype.routerState;
85
+ /** @type {?} */
86
+ RouterRequest.prototype.event;
87
+ /** @type {?} */
88
+ RouterRequest.prototype.trigger;
89
+ }
90
+ /**
91
+ * An action dispatched when the router navigates.
92
+ * @template T
93
+ */
94
+ var /**
59
95
  * An action dispatched when the router navigates.
60
96
  * @template T
61
97
  */
@@ -201,6 +237,42 @@ if (false) {
201
237
  /** @type {?} */
202
238
  RouterDataResolved.prototype.trigger;
203
239
  }
240
+ /**
241
+ * An action dispatched when the router navigation has been finished successfully.
242
+ * @template T
243
+ */
244
+ var /**
245
+ * An action dispatched when the router navigation has been finished successfully.
246
+ * @template T
247
+ */
248
+ RouterNavigated = /** @class */ (function () {
249
+ function RouterNavigated(routerState, event, trigger) {
250
+ if (trigger === void 0) { trigger = 'none'; }
251
+ this.routerState = routerState;
252
+ this.event = event;
253
+ this.trigger = trigger;
254
+ }
255
+ Object.defineProperty(RouterNavigated, "type", {
256
+ get: /**
257
+ * @return {?}
258
+ */
259
+ function () {
260
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
261
+ return '[Router] RouterNavigated';
262
+ },
263
+ enumerable: true,
264
+ configurable: true
265
+ });
266
+ return RouterNavigated;
267
+ }());
268
+ if (false) {
269
+ /** @type {?} */
270
+ RouterNavigated.prototype.routerState;
271
+ /** @type {?} */
272
+ RouterNavigated.prototype.event;
273
+ /** @type {?} */
274
+ RouterNavigated.prototype.trigger;
275
+ }
204
276
 
205
277
  /**
206
278
  * @fileoverview added by tsickle
@@ -294,6 +366,43 @@ var DefaultRouterStateSerializer = /** @class */ (function () {
294
366
  return DefaultRouterStateSerializer;
295
367
  }());
296
368
 
369
+ /**
370
+ * @fileoverview added by tsickle
371
+ * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
372
+ */
373
+ /** @enum {number} */
374
+ var NavigationActionTiming = {
375
+ PreActivation: 1,
376
+ PostActivation: 2,
377
+ };
378
+ /**
379
+ * @record
380
+ */
381
+ function NgxsRouterPluginOptions() { }
382
+ if (false) {
383
+ /** @type {?|undefined} */
384
+ NgxsRouterPluginOptions.prototype.navigationActionTiming;
385
+ }
386
+ /** @type {?} */
387
+ var USER_OPTIONS = new InjectionToken('USER_OPTIONS', { providedIn: 'root', factory: (/**
388
+ * @return {?}
389
+ */
390
+ function () { return undefined; }) });
391
+ /** @type {?} */
392
+ var NGXS_ROUTER_PLUGIN_OPTIONS = new InjectionToken('NGXS_ROUTER_PLUGIN_OPTIONS', { providedIn: 'root', factory: (/**
393
+ * @return {?}
394
+ */
395
+ function () { return ({}); }) });
396
+ /**
397
+ * @param {?} options
398
+ * @return {?}
399
+ */
400
+ function createRouterPluginOptions(options) {
401
+ return {
402
+ navigationActionTiming: (options && options.navigationActionTiming) || 1 /* PreActivation */
403
+ };
404
+ }
405
+
297
406
  /**
298
407
  * @fileoverview added by tsickle
299
408
  * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
@@ -312,7 +421,7 @@ if (false) {
312
421
  RouterStateModel.prototype.trigger;
313
422
  }
314
423
  var RouterState = /** @class */ (function () {
315
- function RouterState(_store, _router, _serializer, _ngZone) {
424
+ function RouterState(_store, _router, _serializer, _ngZone, injector) {
316
425
  this._store = _store;
317
426
  this._router = _router;
318
427
  this._serializer = _serializer;
@@ -330,10 +439,14 @@ var RouterState = /** @class */ (function () {
330
439
  * That's the value of the `RouterState` state
331
440
  */
332
441
  this._storeState = null;
333
- this._lastRoutesRecognized = (/** @type {?} */ (null));
442
+ this._lastEvent = null;
334
443
  this._subscription = new Subscription();
335
- this.setUpStoreListener();
336
- this.setUpRouterEventsListener();
444
+ this._options = null;
445
+ // Note: do not use `@Inject` since it fails on lower versions of Angular with Jest
446
+ // integration, it cannot resolve the token provider.
447
+ this._options = injector.get(NGXS_ROUTER_PLUGIN_OPTIONS, null);
448
+ this._setUpStoreListener();
449
+ this._setUpRouterEventsListener();
337
450
  }
338
451
  RouterState_1 = RouterState;
339
452
  /**
@@ -399,13 +512,17 @@ var RouterState = /** @class */ (function () {
399
512
  * @return {?}
400
513
  */
401
514
  function (ctx, action) {
402
- ctx.setState(__assign({}, ctx.getState(), { trigger: action.trigger, state: action.routerState, navigationId: action.event.id }));
515
+ ctx.setState({
516
+ trigger: action.trigger,
517
+ state: action.routerState,
518
+ navigationId: action.event.id
519
+ });
403
520
  };
404
521
  /**
405
522
  * @private
406
523
  * @return {?}
407
524
  */
408
- RouterState.prototype.setUpStoreListener = /**
525
+ RouterState.prototype._setUpStoreListener = /**
409
526
  * @private
410
527
  * @return {?}
411
528
  */
@@ -419,160 +536,201 @@ var RouterState = /** @class */ (function () {
419
536
  * @return {?}
420
537
  */
421
538
  function (state) {
422
- _this.navigateIfNeeded(state);
539
+ _this._navigateIfNeeded(state);
423
540
  }));
424
541
  this._subscription.add(subscription);
425
542
  };
543
+ /**
544
+ * @private
545
+ * @param {?} routerState
546
+ * @return {?}
547
+ */
548
+ RouterState.prototype._navigateIfNeeded = /**
549
+ * @private
550
+ * @param {?} routerState
551
+ * @return {?}
552
+ */
553
+ function (routerState) {
554
+ var _this = this;
555
+ if (routerState && routerState.trigger === 'devtools') {
556
+ this._storeState = this._store.selectSnapshot(RouterState_1);
557
+ }
558
+ /** @type {?} */
559
+ var canSkipNavigation = !this._storeState ||
560
+ !this._storeState.state ||
561
+ !routerState ||
562
+ routerState.trigger === 'router' ||
563
+ this._router.url === this._storeState.state.url ||
564
+ this._lastEvent instanceof NavigationStart;
565
+ if (canSkipNavigation) {
566
+ return;
567
+ }
568
+ this._storeState = this._store.selectSnapshot(RouterState_1);
569
+ this._trigger = 'store';
570
+ this._ngZone.run((/**
571
+ * @return {?}
572
+ */
573
+ function () { return _this._router.navigateByUrl((/** @type {?} */ ((/** @type {?} */ (_this._storeState)).state)).url); }));
574
+ };
426
575
  /**
427
576
  * @private
428
577
  * @return {?}
429
578
  */
430
- RouterState.prototype.setUpRouterEventsListener = /**
579
+ RouterState.prototype._setUpRouterEventsListener = /**
431
580
  * @private
432
581
  * @return {?}
433
582
  */
434
583
  function () {
435
584
  var _this = this;
436
585
  /** @type {?} */
586
+ var dispatchRouterNavigationLate = this._options != null &&
587
+ this._options.navigationActionTiming === 2 /* PostActivation */;
588
+ /** @type {?} */
589
+ var lastRoutesRecognized;
590
+ /** @type {?} */
437
591
  var subscription = this._router.events.subscribe((/**
438
592
  * @param {?} event
439
593
  * @return {?}
440
594
  */
441
595
  function (event) {
596
+ _this._lastEvent = event;
442
597
  if (event instanceof NavigationStart) {
443
- _this.navigationStart();
598
+ _this._navigationStart(event);
444
599
  }
445
600
  else if (event instanceof RoutesRecognized) {
446
- _this._lastRoutesRecognized = event;
601
+ lastRoutesRecognized = event;
602
+ if (!dispatchRouterNavigationLate && _this._trigger !== 'store') {
603
+ _this._dispatchRouterNavigation(lastRoutesRecognized);
604
+ }
447
605
  }
448
606
  else if (event instanceof ResolveEnd) {
449
- _this.dispatchRouterDataResolved(event);
607
+ _this._dispatchRouterDataResolved(event);
450
608
  }
451
609
  else if (event instanceof NavigationCancel) {
452
- _this.dispatchRouterCancel(event);
453
- _this.reset();
610
+ _this._dispatchRouterCancel(event);
611
+ _this._reset();
454
612
  }
455
613
  else if (event instanceof NavigationError) {
456
- _this.dispatchRouterError(event);
457
- _this.reset();
614
+ _this._dispatchRouterError(event);
615
+ _this._reset();
458
616
  }
459
617
  else if (event instanceof NavigationEnd) {
460
- _this.navigationEnd();
461
- _this.reset();
618
+ if (_this._trigger !== 'store') {
619
+ if (dispatchRouterNavigationLate) {
620
+ _this._dispatchRouterNavigation(lastRoutesRecognized);
621
+ }
622
+ _this._dispatchRouterNavigated(event);
623
+ }
624
+ _this._reset();
462
625
  }
463
626
  }));
464
627
  this._subscription.add(subscription);
465
628
  };
629
+ /** Reacts to `NavigationStart`. */
466
630
  /**
631
+ * Reacts to `NavigationStart`.
467
632
  * @private
633
+ * @param {?} event
468
634
  * @return {?}
469
635
  */
470
- RouterState.prototype.navigationStart = /**
636
+ RouterState.prototype._navigationStart = /**
637
+ * Reacts to `NavigationStart`.
471
638
  * @private
639
+ * @param {?} event
472
640
  * @return {?}
473
641
  */
474
- function () {
642
+ function (event) {
475
643
  this._routerState = this._serializer.serialize(this._router.routerState.snapshot);
476
644
  if (this._trigger !== 'none') {
477
645
  this._storeState = this._store.selectSnapshot(RouterState_1);
646
+ this._dispatchRouterAction(new RouterRequest(this._routerState, event, this._trigger));
478
647
  }
479
648
  };
649
+ /** Reacts to `ResolveEnd`. */
480
650
  /**
651
+ * Reacts to `ResolveEnd`.
481
652
  * @private
653
+ * @param {?} event
482
654
  * @return {?}
483
655
  */
484
- RouterState.prototype.navigationEnd = /**
485
- * @private
486
- * @return {?}
487
- */
488
- function () {
489
- if (this.shouldDispatchRouterNavigation()) {
490
- this.dispatchRouterNavigation();
491
- }
492
- };
493
- /**
494
- * @private
495
- * @return {?}
496
- */
497
- RouterState.prototype.shouldDispatchRouterNavigation = /**
656
+ RouterState.prototype._dispatchRouterDataResolved = /**
657
+ * Reacts to `ResolveEnd`.
498
658
  * @private
659
+ * @param {?} event
499
660
  * @return {?}
500
661
  */
501
- function () {
502
- if (!this._storeState)
503
- return true;
504
- return this._trigger !== 'store';
662
+ function (event) {
663
+ /** @type {?} */
664
+ var routerState = this._serializer.serialize(event.state);
665
+ this._dispatchRouterAction(new RouterDataResolved(routerState, event, this._trigger));
505
666
  };
667
+ /** Reacts to `RoutesRecognized` or `NavigationEnd`, depends on the `navigationActionTiming`. */
506
668
  /**
669
+ * Reacts to `RoutesRecognized` or `NavigationEnd`, depends on the `navigationActionTiming`.
507
670
  * @private
508
- * @param {?} state
671
+ * @param {?} lastRoutesRecognized
509
672
  * @return {?}
510
673
  */
511
- RouterState.prototype.navigateIfNeeded = /**
674
+ RouterState.prototype._dispatchRouterNavigation = /**
675
+ * Reacts to `RoutesRecognized` or `NavigationEnd`, depends on the `navigationActionTiming`.
512
676
  * @private
513
- * @param {?} state
677
+ * @param {?} lastRoutesRecognized
514
678
  * @return {?}
515
679
  */
516
- function (state) {
517
- var _this = this;
518
- if (state && state.trigger === 'devtools') {
519
- this._storeState = this._store.selectSnapshot(RouterState_1);
520
- }
680
+ function (lastRoutesRecognized) {
521
681
  /** @type {?} */
522
- var canSkipNavigation = !this._storeState ||
523
- !this._storeState.state ||
524
- !state ||
525
- state.trigger === 'router' ||
526
- this._router.url === this._storeState.state.url;
527
- if (canSkipNavigation) {
528
- return;
529
- }
530
- this._trigger = 'store';
531
- this._ngZone.run((/**
532
- * @return {?}
533
- */
534
- function () {
535
- _this._router.navigateByUrl((/** @type {?} */ ((/** @type {?} */ (_this._storeState)).state)).url);
536
- }));
682
+ var nextRouterState = this._serializer.serialize(lastRoutesRecognized.state);
683
+ this._dispatchRouterAction(new RouterNavigation(nextRouterState, new RoutesRecognized(lastRoutesRecognized.id, lastRoutesRecognized.url, lastRoutesRecognized.urlAfterRedirects, nextRouterState), this._trigger));
537
684
  };
685
+ /** Reacts to `NavigationCancel`. */
538
686
  /**
687
+ * Reacts to `NavigationCancel`.
539
688
  * @private
689
+ * @param {?} event
540
690
  * @return {?}
541
691
  */
542
- RouterState.prototype.dispatchRouterNavigation = /**
692
+ RouterState.prototype._dispatchRouterCancel = /**
693
+ * Reacts to `NavigationCancel`.
543
694
  * @private
695
+ * @param {?} event
544
696
  * @return {?}
545
697
  */
546
- function () {
547
- /** @type {?} */
548
- var nextRouterState = this._serializer.serialize(this._lastRoutesRecognized.state);
549
- this.dispatchRouterAction(new RouterNavigation(nextRouterState, new RoutesRecognized(this._lastRoutesRecognized.id, this._lastRoutesRecognized.url, this._lastRoutesRecognized.urlAfterRedirects, nextRouterState), this._trigger));
698
+ function (event) {
699
+ this._dispatchRouterAction(new RouterCancel((/** @type {?} */ (this._routerState)), this._storeState, event, this._trigger));
550
700
  };
701
+ /** Reacts to `NavigationEnd`. */
551
702
  /**
703
+ * Reacts to `NavigationEnd`.
552
704
  * @private
553
705
  * @param {?} event
554
706
  * @return {?}
555
707
  */
556
- RouterState.prototype.dispatchRouterCancel = /**
708
+ RouterState.prototype._dispatchRouterError = /**
709
+ * Reacts to `NavigationEnd`.
557
710
  * @private
558
711
  * @param {?} event
559
712
  * @return {?}
560
713
  */
561
714
  function (event) {
562
- this.dispatchRouterAction(new RouterCancel((/** @type {?} */ (this._routerState)), this._storeState, event, this._trigger));
715
+ this._dispatchRouterAction(new RouterError((/** @type {?} */ (this._routerState)), this._storeState, new NavigationError(event.id, event.url, "" + event), this._trigger));
563
716
  };
717
+ /** Reacts to `NavigationEnd`. */
564
718
  /**
719
+ * Reacts to `NavigationEnd`.
565
720
  * @private
566
721
  * @param {?} event
567
722
  * @return {?}
568
723
  */
569
- RouterState.prototype.dispatchRouterError = /**
724
+ RouterState.prototype._dispatchRouterNavigated = /**
725
+ * Reacts to `NavigationEnd`.
570
726
  * @private
571
727
  * @param {?} event
572
728
  * @return {?}
573
729
  */
574
730
  function (event) {
575
- this.dispatchRouterAction(new RouterError((/** @type {?} */ (this._routerState)), this._storeState, new NavigationError(event.id, event.url, "" + event), this._trigger));
731
+ /** @type {?} */
732
+ var routerState = this._serializer.serialize(this._router.routerState.snapshot);
733
+ this._dispatchRouterAction(new RouterNavigated(routerState, event, this._trigger));
576
734
  };
577
735
  /**
578
736
  * @private
@@ -580,7 +738,7 @@ var RouterState = /** @class */ (function () {
580
738
  * @param {?} action
581
739
  * @return {?}
582
740
  */
583
- RouterState.prototype.dispatchRouterAction = /**
741
+ RouterState.prototype._dispatchRouterAction = /**
584
742
  * @private
585
743
  * @template T
586
744
  * @param {?} action
@@ -595,26 +753,11 @@ var RouterState = /** @class */ (function () {
595
753
  this._trigger = 'none';
596
754
  }
597
755
  };
598
- /**
599
- * @private
600
- * @param {?} event
601
- * @return {?}
602
- */
603
- RouterState.prototype.dispatchRouterDataResolved = /**
604
- * @private
605
- * @param {?} event
606
- * @return {?}
607
- */
608
- function (event) {
609
- /** @type {?} */
610
- var routerState = this._serializer.serialize(event.state);
611
- this.dispatchRouterAction(new RouterDataResolved(routerState, event, this._trigger));
612
- };
613
756
  /**
614
757
  * @private
615
758
  * @return {?}
616
759
  */
617
- RouterState.prototype.reset = /**
760
+ RouterState.prototype._reset = /**
618
761
  * @private
619
762
  * @return {?}
620
763
  */
@@ -628,7 +771,8 @@ var RouterState = /** @class */ (function () {
628
771
  { type: Store },
629
772
  { type: Router },
630
773
  { type: RouterStateSerializer },
631
- { type: NgZone }
774
+ { type: NgZone },
775
+ { type: Injector }
632
776
  ]; };
633
777
  RouterState.decorators = [
634
778
  { type: Injectable }
@@ -638,7 +782,8 @@ var RouterState = /** @class */ (function () {
638
782
  { type: Store },
639
783
  { type: Router },
640
784
  { type: RouterStateSerializer },
641
- { type: NgZone }
785
+ { type: NgZone },
786
+ { type: Injector }
642
787
  ]; };
643
788
  __decorate([
644
789
  Action(Navigate),
@@ -647,7 +792,14 @@ var RouterState = /** @class */ (function () {
647
792
  __metadata("design:returntype", void 0)
648
793
  ], RouterState.prototype, "navigate", null);
649
794
  __decorate([
650
- Action([RouterNavigation, RouterError, RouterCancel, RouterDataResolved]),
795
+ Action([
796
+ RouterRequest,
797
+ RouterNavigation,
798
+ RouterError,
799
+ RouterCancel,
800
+ RouterDataResolved,
801
+ RouterNavigated
802
+ ]),
651
803
  __metadata("design:type", Function),
652
804
  __metadata("design:paramtypes", [Object, Object]),
653
805
  __metadata("design:returntype", void 0)
@@ -676,7 +828,8 @@ var RouterState = /** @class */ (function () {
676
828
  __metadata("design:paramtypes", [Store,
677
829
  Router,
678
830
  RouterStateSerializer,
679
- NgZone])
831
+ NgZone,
832
+ Injector])
680
833
  ], RouterState);
681
834
  return RouterState;
682
835
  }());
@@ -704,12 +857,17 @@ if (false) {
704
857
  * @type {?}
705
858
  * @private
706
859
  */
707
- RouterState.prototype._lastRoutesRecognized;
860
+ RouterState.prototype._lastEvent;
708
861
  /**
709
862
  * @type {?}
710
863
  * @private
711
864
  */
712
865
  RouterState.prototype._subscription;
866
+ /**
867
+ * @type {?}
868
+ * @private
869
+ */
870
+ RouterState.prototype._options;
713
871
  /**
714
872
  * @type {?}
715
873
  * @private
@@ -740,15 +898,25 @@ var NgxsRouterPluginModule = /** @class */ (function () {
740
898
  function NgxsRouterPluginModule() {
741
899
  }
742
900
  /**
901
+ * @param {?=} options
743
902
  * @return {?}
744
903
  */
745
904
  NgxsRouterPluginModule.forRoot = /**
905
+ * @param {?=} options
746
906
  * @return {?}
747
907
  */
748
- function () {
908
+ function (options) {
749
909
  return {
750
910
  ngModule: NgxsRouterPluginModule,
751
- providers: [{ provide: RouterStateSerializer, useClass: DefaultRouterStateSerializer }]
911
+ providers: [
912
+ { provide: USER_OPTIONS, useValue: options },
913
+ {
914
+ provide: NGXS_ROUTER_PLUGIN_OPTIONS,
915
+ useFactory: createRouterPluginOptions,
916
+ deps: [USER_OPTIONS]
917
+ },
918
+ { provide: RouterStateSerializer, useClass: DefaultRouterStateSerializer }
919
+ ]
752
920
  };
753
921
  };
754
922
  NgxsRouterPluginModule.decorators = [
@@ -774,5 +942,5 @@ var NgxsRouterPluginModule = /** @class */ (function () {
774
942
  * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
775
943
  */
776
944
 
777
- export { DefaultRouterStateSerializer, Navigate, NgxsRouterPluginModule, RouterCancel, RouterDataResolved, RouterError, RouterNavigation, RouterState, RouterStateSerializer };
945
+ export { DefaultRouterStateSerializer, Navigate, NgxsRouterPluginModule, RouterCancel, RouterDataResolved, RouterError, RouterNavigated, RouterNavigation, RouterRequest, RouterState, RouterStateSerializer, USER_OPTIONS as ɵb, NGXS_ROUTER_PLUGIN_OPTIONS as ɵc, createRouterPluginOptions as ɵd };
778
946
  //# sourceMappingURL=ngxs-router-plugin.js.map