@coolon/angular-ui-powerups 14.45.0 → 14.47.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.
@@ -60,7 +60,6 @@ import * as i1$6 from '@angular/material/dialog';
60
60
  import { MatDialogModule, MAT_DIALOG_DEFAULT_OPTIONS } from '@angular/material/dialog';
61
61
  import * as i1$4 from '@angular/router';
62
62
  import { ActivatedRouteSnapshot, UrlSegment, NavigationStart, NavigationEnd, NavigationError, NavigationCancel, RouterModule } from '@angular/router';
63
- import { ArrayMultimap } from '@teppeis/multimaps';
64
63
  import 'reflect-metadata';
65
64
  import { Memoize as Memoize$1 } from 'typescript-memoize';
66
65
  import * as i1$5 from '@angular/cdk/overlay';
@@ -6763,76 +6762,114 @@ __decorate([
6763
6762
  // .withAncestorParameters(ParentB, {id: "c"}) // barf if not a ancestor or missing parameters are required or parameter isn't valid
6764
6763
  //
6765
6764
 
6766
- class PlaceAuthorisationChecker extends ReducingValueModel {
6765
+ function toPromise(disposedWith, condition$) {
6766
+ // TODO: move this functionality to pectin with a waitUnit(model$).isTruthy() DSL.
6767
+ const { when } = Dsl.disposeWith(disposedWith);
6768
+ return new Promise((resolve) => {
6769
+ when(condition$).becomesTruthy(() => resolve(), { once: true, initialValue: "evaluate" });
6770
+ });
6771
+ }
6772
+ function withPostponeUntil(disposable, options, callback) {
6773
+ // TODO: move this functionality to powerups
6774
+ const { chain } = Dsl.disposeWith(disposable);
6775
+ if (options.postponeUntil) {
6776
+ return callback(options.postponeUntil);
6777
+ }
6778
+ else if (options.postponeWhile) {
6779
+ return callback(chain(options.postponeWhile).isFalsey());
6780
+ }
6781
+ return undefined;
6782
+ }
6783
+ class PlaceAuthGuard extends GarbageCollector {
6784
+ constructor(placeDefinition, enabled$, options) {
6785
+ super();
6786
+ this.placeDefinition = placeDefinition;
6787
+ this.enabled$ = enabled$;
6788
+ const { chain } = Dsl.disposeWith(this);
6789
+ this.readyPromise = withPostponeUntil(this, options, condition$ => {
6790
+ return toPromise(this, condition$);
6791
+ }) ?? Promise.resolve();
6792
+ }
6793
+ async waitUntilReady() {
6794
+ await this.readyPromise;
6795
+ }
6796
+ }
6797
+ class EnabledPlaceChecker extends ReducingValueModel {
6767
6798
  constructor() {
6768
6799
  super([]);
6769
- this.predicatesByPlace = new ArrayMultimap;
6770
- this.defaultAuthCheck = () => true;
6771
- }
6772
- setDefaultAuthCheck(options) {
6773
- this.defaultAuthCheck = (placeDef) => {
6774
- return options.isAuthorised(placeDef);
6775
- };
6776
- if (options.dependsOn) {
6777
- this.trackSource(options.dependsOn);
6778
- }
6800
+ this.placeGuards$ = new MutableListModel();
6801
+ const { chain } = Dsl.disposeWith(this);
6802
+ this.placeGuardsByPlaceDef$ = chain(this.placeGuards$).indexManyBy("placeDefinition");
6779
6803
  }
6780
- authoriseWhen(placeDefinition, condition$) {
6804
+ enableWhen(placeDefinition, condition$, options) {
6781
6805
  const { chain } = Dsl.disposeWith(this);
6782
- this.predicatesByPlace.put(placeDefinition, condition$);
6806
+ this.placeGuards$.push(new PlaceAuthGuard(placeDefinition, condition$, options));
6783
6807
  this.trackSource(condition$);
6784
6808
  }
6785
6809
  isAuthorised(placeDef) {
6786
6810
  return this.value(placeDef);
6787
6811
  }
6788
6812
  compute(values) {
6789
- return (def) => {
6790
- return this.defaultAuthCheck(def) && this.checkPredicatesByPlace(def);
6791
- };
6813
+ return (def) => this.checkPredicatesByPlace(def);
6792
6814
  }
6793
6815
  checkPredicatesByPlace(def) {
6794
- const placePredicates = this.predicatesByPlace.get(def);
6816
+ const placePredicates = this.placeGuardsByPlaceDef$.get(def);
6795
6817
  if (!placePredicates || placePredicates.length < 1) {
6796
6818
  return true;
6797
6819
  }
6798
6820
  else {
6799
6821
  return placePredicates.reduce((sum, model) => {
6800
- return sum && model.value;
6822
+ return sum && model.enabled$.value;
6801
6823
  }, true);
6802
6824
  }
6803
6825
  }
6826
+ async waitUntilReady(placeDef) {
6827
+ const placeAuthGuards = this.placeGuardsByPlaceDef$.get(placeDef);
6828
+ if (placeAuthGuards?.length > 0) {
6829
+ await Promise.all(placeAuthGuards.map(it => it.waitUntilReady()));
6830
+ }
6831
+ }
6804
6832
  }
6805
6833
  class PlaceAuthService extends GarbageCollector {
6806
6834
  constructor() {
6807
6835
  super();
6808
- this.placeGuards$ = new MutableListModel();
6809
- this._ready$ = new AndValueModel().setDefault(true);
6836
+ this._authNReady$ = new AndValueModel().setDefault(true);
6837
+ this._authZReady$ = new AndValueModel().setDefault(true);
6810
6838
  this.authenticated$ = this.registerDisposable(new DelegatingValueModel).withDefault(true);
6811
- this.authorisationChecker$ = this.registerDisposable(new PlaceAuthorisationChecker());
6839
+ this.authChecker$ = this.registerDisposable(new DelegatingValueModel).withDefault(() => true);
6840
+ this.enabledPlaceChecker = this.registerDisposable(new EnabledPlaceChecker());
6812
6841
  const { chain } = Dsl.disposeWith(this);
6813
- this.accessGuard$ = chain.reduce(this.authenticated$, this.authorisationChecker$, (authenticated, authCheck) => {
6814
- return new PlaceAuthService.AccessGuard(authenticated, authCheck);
6842
+ this.accessGuard$ = chain.reduce(this.authenticated$, this.authChecker$, this.enabledPlaceChecker, (authenticated, authCheck, enabledPlaceCheck) => {
6843
+ return new PlaceAuthService.AccessGuard(authenticated, def => authCheck(def) && enabledPlaceCheck(def));
6815
6844
  });
6816
6845
  }
6817
6846
  configureAuthentication(options) {
6818
6847
  this.authNOptions = options;
6819
6848
  this.authenticated$.setDelegate(options.isAuthenticated$);
6820
- this.capturePostoneDependency(options);
6849
+ withPostponeUntil(this, options, condition$ => this._authNReady$.addSource(condition$));
6821
6850
  }
6822
6851
  configureAuthorisation(options) {
6823
6852
  const { chain } = Dsl.disposeWith(this);
6824
6853
  this.authZOptions = options;
6825
- this.authorisationChecker$.setDefaultAuthCheck(options);
6826
- this.capturePostoneDependency(options);
6854
+ let authChecker$ = ValueModel.constant(def => options.isAuthorised(def));
6855
+ if (options.dependsOn$) {
6856
+ // have to return a new funtion or the value model won't fire a change event
6857
+ authChecker$ = chain(authChecker$).map(it => def => it(def), { dependsOn: options.dependsOn$ });
6858
+ }
6859
+ this.authChecker$.setDelegate(authChecker$);
6860
+ withPostponeUntil(this, options, condition$ => this._authZReady$.addSource(condition$));
6827
6861
  }
6828
6862
  enablePlace(placeDefinition, options) {
6829
6863
  const { chain } = Dsl.disposeWith(this);
6830
6864
  const when$ = options.when$ ? options.when$ : chain(options.unless$).isFalsey();
6831
- this.authorisationChecker$.authoriseWhen(PlaceComponent.getDefinition(placeDefinition), when$);
6832
- this.capturePostoneDependency(options);
6865
+ this.enabledPlaceChecker.enableWhen(PlaceComponent.getDefinition(placeDefinition), when$, options);
6866
+ }
6867
+ async waitUntilAuthenticationReady() {
6868
+ await toPromise(this, this._authNReady$);
6833
6869
  }
6834
- waitUntilReady() {
6835
- return this.toPromise(this._ready$);
6870
+ async waitUntilAuthorisationReadyFor(placeDef) {
6871
+ await toPromise(this, this._authZReady$);
6872
+ await this.enabledPlaceChecker.waitUntilReady(placeDef);
6836
6873
  }
6837
6874
  getAuthenticationFailureRedirect(activatedRoute, state) {
6838
6875
  const redirect = this.authNOptions.redirectTo(activatedRoute, state);
@@ -6841,22 +6878,6 @@ class PlaceAuthService extends GarbageCollector {
6841
6878
  getAuthorisationFailureRedirectOptions(component) {
6842
6879
  return this.authZOptions.redirectTo(component).map(it => it instanceof Visit ? it : Visit.goto(it));
6843
6880
  }
6844
- toPromise(condition$) {
6845
- // TODO: move this functionality to pectin with a waitUnit(model$).isTruthy() DSL.
6846
- const { when } = Dsl.disposeWith(this);
6847
- return new Promise((resolve) => {
6848
- when(condition$).becomesTruthy(() => resolve(), { once: true, initialValue: "evaluate" });
6849
- });
6850
- }
6851
- capturePostoneDependency(options) {
6852
- const { chain } = Dsl.disposeWith(this);
6853
- if (options.postponeUntil) {
6854
- this._ready$.addSource(options.postponeUntil);
6855
- }
6856
- else if (options.postponeWhile) {
6857
- this._ready$.addSource(chain(options.postponeWhile).isFalsey());
6858
- }
6859
- }
6860
6881
  isAuthenticated() {
6861
6882
  return this.accessGuard$.value.isAuthenticated();
6862
6883
  }
@@ -6866,15 +6887,15 @@ class PlaceAuthService extends GarbageCollector {
6866
6887
  }
6867
6888
  (function (PlaceAuthService) {
6868
6889
  class AccessGuard {
6869
- constructor(authenticated, authCheck) {
6890
+ constructor(authenticated, checkAuth) {
6870
6891
  this.authenticated = authenticated;
6871
- this.authCheck = authCheck;
6892
+ this.checkAuth = checkAuth;
6872
6893
  }
6873
6894
  isAuthenticated() {
6874
6895
  return this.authenticated;
6875
6896
  }
6876
6897
  isAuthorised(placeDefinition) {
6877
- return this.authCheck(placeDefinition);
6898
+ return this.checkAuth(placeDefinition);
6878
6899
  }
6879
6900
  }
6880
6901
  PlaceAuthService.AccessGuard = AccessGuard;
@@ -10870,7 +10891,7 @@ class PlaceGuard {
10870
10891
  if (PlaceComponent.isPlaceComponentConstructor(activatedRoute.component)) {
10871
10892
  const placeDefinition = PlaceComponent.getDefinition(activatedRoute.component);
10872
10893
  if (placeDefinition.authenticate) {
10873
- await this.placeRouter.authService.waitUntilReady();
10894
+ await this.placeRouter.authService.waitUntilAuthenticationReady();
10874
10895
  const authenticated = this.authService.isAuthenticated();
10875
10896
  if (!authenticated) {
10876
10897
  const redirect = this.authService.getAuthenticationFailureRedirect(activatedRoute, state);
@@ -10880,6 +10901,7 @@ class PlaceGuard {
10880
10901
  }
10881
10902
  return this.createUrlTree(redirect);
10882
10903
  }
10904
+ await this.authService.waitUntilAuthorisationReadyFor(placeDefinition);
10883
10905
  if (!this.authService.isAuthorised(placeDefinition)) {
10884
10906
  const nextRedirect = this.findBestAuthenticatedAlternative(placeDefinition, activatedRoute);
10885
10907
  return this.createUrlTree(nextRedirect);