11import { Injectable } from '@angular/core' ;
2- import { BehaviorSubject } from 'rxjs' ;
2+ import { BehaviorSubject , map , Observable , switchMap , take , timer } from 'rxjs' ;
33import { environment } from '../../../environments/environment' ;
44import { UUIDService } from '../../core/shared/uuid.service' ;
5+ import { AccessibilitySettingsService , AccessibilitySetting } from '../../accessibility/accessibility-settings.service' ;
6+
7+ export const MIN_MESSAGE_DURATION = 200 ;
58
69/**
710 * The LiveRegionService is responsible for handling the messages that are shown by the {@link LiveRegionComponent}.
@@ -14,6 +17,7 @@ export class LiveRegionService {
1417
1518 constructor (
1619 protected uuidService : UUIDService ,
20+ protected accessibilitySettingsService : AccessibilitySettingsService ,
1721 ) {
1822 }
1923
@@ -64,7 +68,12 @@ export class LiveRegionService {
6468 addMessage ( message : string ) : string {
6569 const uuid = this . uuidService . generate ( ) ;
6670 this . messages . push ( { message, uuid } ) ;
67- setTimeout ( ( ) => this . clearMessageByUUID ( uuid ) , this . messageTimeOutDurationMs ) ;
71+
72+ this . getConfiguredMessageTimeOutMs ( ) . pipe (
73+ take ( 1 ) ,
74+ switchMap ( timeOut => timer ( timeOut ) ) ,
75+ ) . subscribe ( ( ) => this . clearMessageByUUID ( uuid ) ) ;
76+
6877 this . emitCurrentMessages ( ) ;
6978 return uuid ;
7079 }
@@ -115,6 +124,17 @@ export class LiveRegionService {
115124 this . liveRegionIsVisible = isVisible ;
116125 }
117126
127+ /**
128+ * Gets the user-configured timeOut, or the stored timeOut if the user has not configured a timeOut duration.
129+ * Emits {@link MIN_MESSAGE_DURATION} if the configured value is smaller.
130+ */
131+ getConfiguredMessageTimeOutMs ( ) : Observable < number > {
132+ return this . accessibilitySettingsService . getAsNumber (
133+ AccessibilitySetting . LiveRegionTimeOut ,
134+ this . getMessageTimeOutMs ( ) ,
135+ ) . pipe ( map ( timeOut => Math . max ( timeOut , MIN_MESSAGE_DURATION ) ) ) ;
136+ }
137+
118138 /**
119139 * Gets the current message timeOut duration in milliseconds
120140 */
0 commit comments