@@ -2,23 +2,34 @@ import { Injectable } from '@angular/core';
22import { BehaviorSubject } from 'rxjs' ;
33
44import { environment } from '../../../environments/environment' ;
5+ import { UUIDService } from '../../core/shared/uuid.service' ;
56
7+ /**
8+ * The LiveRegionService is responsible for handling the messages that are shown by the {@link LiveRegionComponent}.
9+ * Use this service to add or remove messages to the Live Region.
10+ */
611@Injectable ( {
712 providedIn : 'root' ,
813} )
914export class LiveRegionService {
1015
16+ constructor (
17+ protected uuidService : UUIDService ,
18+ ) {
19+ }
20+
1121 /**
1222 * The duration after which the messages disappear in milliseconds
1323 * @protected
1424 */
1525 protected messageTimeOutDurationMs : number = environment . liveRegion . messageTimeOutDurationMs ;
1626
1727 /**
18- * Array containing the messages that should be shown in the live region
28+ * Array containing the messages that should be shown in the live region,
29+ * together with a uuid, so they can be uniquely identified
1930 * @protected
2031 */
21- protected messages : string [ ] = [ ] ;
32+ protected messages : { message : string , uuid : string } [ ] = [ ] ;
2233
2334 /**
2435 * BehaviorSubject emitting the array with messages every time the array updates
@@ -35,27 +46,28 @@ export class LiveRegionService {
3546 /**
3647 * Returns a copy of the array with the current live region messages
3748 */
38- getMessages ( ) {
39- return [ ... this . messages ] ;
49+ getMessages ( ) : string [ ] {
50+ return this . messages . map ( messageObj => messageObj . message ) ;
4051 }
4152
4253 /**
4354 * Returns the BehaviorSubject emitting the array with messages every time the array updates
4455 */
45- getMessages$ ( ) {
56+ getMessages$ ( ) : BehaviorSubject < string [ ] > {
4657 return this . messages$ ;
4758 }
4859
4960 /**
5061 * Adds a message to the live-region messages array
5162 * @param message
63+ * @return The uuid of the message
5264 */
53- addMessage ( message : string ) {
54- this . messages . push ( message ) ;
65+ addMessage ( message : string ) : string {
66+ const uuid = this . uuidService . generate ( ) ;
67+ this . messages . push ( { message, uuid } ) ;
68+ setTimeout ( ( ) => this . clearMessageByUUID ( uuid ) , this . messageTimeOutDurationMs ) ;
5569 this . emitCurrentMessages ( ) ;
56-
57- // Clear the message once the timeOut has passed
58- setTimeout ( ( ) => this . pop ( ) , this . messageTimeOutDurationMs ) ;
70+ return uuid ;
5971 }
6072
6173 /**
@@ -67,12 +79,14 @@ export class LiveRegionService {
6779 }
6880
6981 /**
70- * Removes the longest living message from the array.
71- * @protected
82+ * Removes the message with the given UUID from the messages array
83+ * @param uuid The uuid of the message to clear
7284 */
73- protected pop ( ) {
74- if ( this . messages . length > 0 ) {
75- this . messages . shift ( ) ;
85+ clearMessageByUUID ( uuid : string ) {
86+ const index = this . messages . findIndex ( messageObj => messageObj . uuid === uuid ) ;
87+
88+ if ( index !== - 1 ) {
89+ this . messages . splice ( index , 1 ) ;
7690 this . emitCurrentMessages ( ) ;
7791 }
7892 }
0 commit comments