@brggroup/share-lib 0.0.85 → 0.0.86

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,5 +1,5 @@
1
1
  import * as i0 from '@angular/core';
2
- import { inject, Injectable, NgZone, Component, HostListener, ViewContainerRef, Input, ViewChild, EventEmitter, Output, Directive, Optional, Self, HostBinding, forwardRef, Pipe } from '@angular/core';
2
+ import { inject, Injectable, Component, HostListener, ViewContainerRef, Input, ViewChild, EventEmitter, Output, Directive, Optional, Self, HostBinding, forwardRef, Pipe } from '@angular/core';
3
3
  import * as i1$4 from '@angular/router';
4
4
  import { Router, ActivatedRoute, NavigationEnd, RouterLink, RouterOutlet } from '@angular/router';
5
5
  import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
@@ -544,8 +544,11 @@ class TokenStorage {
544
544
  }
545
545
 
546
546
  class SessionManagerService {
547
- zone = inject(NgZone);
548
- isRunning = false;
547
+ zone;
548
+ constructor(zone) {
549
+ this.zone = zone;
550
+ }
551
+ handlers;
549
552
  idleTimer;
550
553
  refreshTimer;
551
554
  heartbeatTimer;
@@ -554,19 +557,25 @@ class SessionManagerService {
554
557
  get IDLE_TIME() {
555
558
  return (SessionManagerService.IDLE_MIN || 0) * 60 * 1000;
556
559
  }
557
- LEADER_KEY = 'APP_LEADER';
558
- HEARTBEAT_KEY = 'APP_LEADER_HEARTBEAT';
559
- channel = new BroadcastChannel('APP_SESSION');
560
+ LEADER_KEY = 'APP_SESSION_LEADER';
561
+ HEARTBEAT_KEY = 'APP_SESSION_HEARTBEAT';
562
+ ACTIVITY_KEY = 'APP_LAST_ACTIVITY';
560
563
  tabId = crypto.randomUUID();
561
564
  isLeader = false;
562
- handlers;
563
- /* ---------------- START ---------------- */
565
+ isRunning = false;
566
+ refreshLock = false;
567
+ channel = null;
568
+ /* -------------------------------- START -------------------------------- */
564
569
  start(handlers) {
565
570
  if (this.IDLE_TIME <= 0 || this.isRunning)
566
571
  return;
572
+ this.isRunning = true;
567
573
  this.handlers = handlers;
574
+ if ('BroadcastChannel' in window) {
575
+ this.channel = new BroadcastChannel('APP_SESSION');
576
+ this.listenChannel();
577
+ }
568
578
  this.listenActivity();
569
- this.listenChannel();
570
579
  this.updateActivity();
571
580
  this.resetIdle();
572
581
  this.electLeader();
@@ -576,11 +585,10 @@ class SessionManagerService {
576
585
  clearTimeout(this.idleTimer);
577
586
  clearTimeout(this.refreshTimer);
578
587
  clearInterval(this.heartbeatTimer);
579
- if (this.channel) {
580
- this.channel.close();
581
- }
588
+ this.channel?.close();
589
+ this.channel = null;
582
590
  }
583
- /* ---------------- ACTIVITY ---------------- */
591
+ /* -------------------------------- ACTIVITY -------------------------------- */
584
592
  listenActivity() {
585
593
  const events = ['mousemove', 'keydown', 'click', 'scroll'];
586
594
  events.forEach((e) => {
@@ -588,22 +596,21 @@ class SessionManagerService {
588
596
  });
589
597
  }
590
598
  onActivity = this.debounce(() => {
599
+ if (!this.isRunning)
600
+ return;
591
601
  this.updateActivity();
592
602
  this.resetIdle();
593
603
  this.broadcast('activity');
594
604
  }, 1000);
595
605
  updateActivity() {
596
- localStorage.setItem('APP_LAST_ACTIVITY', Date.now().toString());
606
+ localStorage.setItem(this.ACTIVITY_KEY, Date.now().toString());
597
607
  }
598
- broadcast(type) {
608
+ /* -------------------------------- IDLE -------------------------------- */
609
+ resetIdle() {
599
610
  if (!this.isRunning)
600
611
  return;
601
- this.channel?.postMessage({ type });
602
- }
603
- /* ---------------- IDLE ---------------- */
604
- resetIdle() {
605
612
  clearTimeout(this.idleTimer);
606
- const last = Number(localStorage.getItem('APP_LAST_ACTIVITY') || Date.now());
613
+ const last = Number(localStorage.getItem(this.ACTIVITY_KEY) || Date.now());
607
614
  const remain = this.IDLE_TIME - (Date.now() - last);
608
615
  if (remain <= 0) {
609
616
  this.logout();
@@ -615,7 +622,7 @@ class SessionManagerService {
615
622
  }, remain);
616
623
  });
617
624
  }
618
- /* ---------------- LEADER ---------------- */
625
+ /* -------------------------------- LEADER -------------------------------- */
619
626
  electLeader() {
620
627
  const leader = localStorage.getItem(this.LEADER_KEY);
621
628
  if (!leader) {
@@ -625,7 +632,6 @@ class SessionManagerService {
625
632
  this.checkLeaderHeartbeat();
626
633
  }
627
634
  becomeLeader() {
628
- console.log('Become leader', this.tabId);
629
635
  this.isLeader = true;
630
636
  localStorage.setItem(this.LEADER_KEY, this.tabId);
631
637
  this.startHeartbeat();
@@ -644,12 +650,12 @@ class SessionManagerService {
644
650
  }
645
651
  }, 3000);
646
652
  }
647
- /* ---------------- REFRESH ---------------- */
653
+ /* -------------------------------- REFRESH TOKEN -------------------------------- */
648
654
  scheduleRefresh() {
649
- if (!this.isLeader)
655
+ if (!this.isLeader || !this.isRunning)
650
656
  return;
651
657
  clearTimeout(this.refreshTimer);
652
- const token = TokenStorage.getToken();
658
+ const token = this.handlers.getToken();
653
659
  if (!token)
654
660
  return;
655
661
  const exp = this.parseJwt(token).exp * 1000;
@@ -663,8 +669,9 @@ class SessionManagerService {
663
669
  }, delay);
664
670
  }
665
671
  async refreshToken() {
666
- if (!this.isLeader)
672
+ if (!this.isLeader || this.refreshLock || !this.isRunning)
667
673
  return;
674
+ this.refreshLock = true;
668
675
  try {
669
676
  const res = await this.handlers.refreshToken();
670
677
  if (res?.IsSuccess) {
@@ -678,10 +685,17 @@ class SessionManagerService {
678
685
  catch {
679
686
  this.logout();
680
687
  }
688
+ finally {
689
+ this.refreshLock = false;
690
+ }
681
691
  }
682
- /* ---------------- CHANNEL ---------------- */
692
+ /* -------------------------------- CHANNEL -------------------------------- */
683
693
  listenChannel() {
694
+ if (!this.channel)
695
+ return;
684
696
  this.channel.onmessage = (msg) => {
697
+ if (!this.isRunning)
698
+ return;
685
699
  const type = msg.data?.type;
686
700
  if (type === 'activity') {
687
701
  this.resetIdle();
@@ -694,12 +708,24 @@ class SessionManagerService {
694
708
  }
695
709
  };
696
710
  }
697
- /* ---------------- LOGOUT ---------------- */
711
+ broadcast(type) {
712
+ if (!this.isRunning)
713
+ return;
714
+ if (this.channel) {
715
+ this.channel.postMessage({ type });
716
+ }
717
+ else {
718
+ localStorage.setItem('APP_SESSION_EVENT', JSON.stringify({ type, t: Date.now() }));
719
+ }
720
+ }
721
+ /* -------------------------------- LOGOUT -------------------------------- */
698
722
  logout() {
723
+ if (!this.isRunning)
724
+ return;
699
725
  this.broadcast('logout');
700
726
  this.handlers.logout();
701
727
  }
702
- /* ---------------- UTILS ---------------- */
728
+ /* -------------------------------- UTILS -------------------------------- */
703
729
  debounce(fn, delay) {
704
730
  let timer;
705
731
  return () => {
@@ -711,7 +737,7 @@ class SessionManagerService {
711
737
  const payload = token.split('.')[1];
712
738
  return JSON.parse(atob(payload));
713
739
  }
714
- static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.1.7", ngImport: i0, type: SessionManagerService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
740
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.1.7", ngImport: i0, type: SessionManagerService, deps: [{ token: i0.NgZone }], target: i0.ɵɵFactoryTarget.Injectable });
715
741
  static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.1.7", ngImport: i0, type: SessionManagerService, providedIn: 'root' });
716
742
  }
717
743
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.1.7", ngImport: i0, type: SessionManagerService, decorators: [{
@@ -719,7 +745,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.1.7", ngImpor
719
745
  args: [{
720
746
  providedIn: 'root',
721
747
  }]
722
- }] });
748
+ }], ctorParameters: () => [{ type: i0.NgZone }] });
723
749
 
724
750
  const URLs$4 = {
725
751
  login: '/api/Auth/Login',
@@ -784,6 +810,7 @@ class AuthService extends HTTPService {
784
810
  this.sessionManager.start({
785
811
  refreshToken: () => this.refreshToken(),
786
812
  logout: () => this.signOut(),
813
+ getToken: () => this.getToken(),
787
814
  });
788
815
  if (returnUrl) {
789
816
  this.router.navigateByUrl(returnUrl);
@@ -806,7 +833,11 @@ class AuthService extends HTTPService {
806
833
  initSession() {
807
834
  const token = TokenStorage.getToken();
808
835
  if (token) {
809
- this.sessionManager.start({ refreshToken: () => this.refreshToken(), logout: () => this.signOut() });
836
+ this.sessionManager.start({
837
+ refreshToken: () => this.refreshToken(),
838
+ logout: () => this.signOut(),
839
+ getToken: () => this.getToken(),
840
+ });
810
841
  }
811
842
  }
812
843
  /* ----------------------------------------