daily pastebin goal
53%
SHARE
TWEET

Untitled

a guest Jul 16th, 2018 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {Injectable} from '@angular/core';
  2. import {Observable, race, timer} from 'rxjs';
  3. import {tap, switchMap, map, retry} from 'rxjs/operators';
  4. import {MatDialog, MatDialogRef, ConfirmationComponent} from '../../shared';
  5.  
  6. @Injectable()
  7. export class SessionExpirationService {
  8.   private readonly EXPIRATION_TIMEOUT: number = 5000;
  9.  
  10.   private readonly REDIRECT_TIMEOUT: number = 7000;
  11.  
  12.   private dialogRef: MatDialogRef<ConfirmationComponent>;
  13.  
  14.   constructor(private dialog: MatDialog) {}
  15.  
  16.   public start(): Observable<boolean> {
  17.     // start timer to show the confirmation
  18.     return timer(this.EXPIRATION_TIMEOUT).pipe(
  19.       tap(() => {
  20.         this.dialogRef = this.dialogRef || this.createConfirmation();
  21.       }),
  22.       switchMap(() => {
  23.         return race(
  24.           // user is done if the confirmation is closed
  25.           this.dialogRef.afterClosed().pipe(
  26.             tap(() => this.dialogRef = null),
  27.             map((needMoretime) => {
  28.               // treat 'Yes' answer as an error
  29.               if (needMoretime) {
  30.                 throw needMoretime;
  31.               }
  32.  
  33.               return needMoretime;
  34.             })
  35.           ),
  36.           //  user is done after being idle for REDIRECT_TIMEOUT
  37.           timer(this.REDIRECT_TIMEOUT).pipe(
  38.             tap(() => {
  39.               this.dialogRef.close();
  40.               this.dialogRef = null;
  41.             })
  42.           )
  43.         );
  44.       }),
  45.       // retry confirmation if user needs more time
  46.       retry()
  47.     );
  48.   }
  49.  
  50.   private createConfirmation(): MatDialogRef<ConfirmationComponent> {
  51.     return this.dialog.open(ConfirmationComponent, {
  52.       width: '300px',
  53.       disableClose: true,
  54.       data: {title: 'Do you need more time?', yesText: 'Yes', noText: 'No, I\'m Done'}
  55.     });
  56.   }
  57. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top