2626
2727import { Injectable } from '@angular/core' ;
2828import { Message , MessageTypes } from 'common' ;
29- import { BehaviorSubject , Observable } from 'rxjs' ;
29+ import { BehaviorSubject , Observable , Subscription } from 'rxjs' ;
3030import { SystemConfigStore } from '../../store/system-config/system-config.store' ;
31+ import { distinctUntilChanged , filter , map } from 'rxjs/operators' ;
3132
3233@Injectable ( {
3334 providedIn : 'root'
@@ -37,13 +38,40 @@ export class MessageService {
3738 protected messages : Message [ ] = [ ] ;
3839 protected messagesStage : BehaviorSubject < Message [ ] > ;
3940 protected timeout = 3 ;
41+ private subscription : Subscription ;
4042
4143 constructor ( public config : SystemConfigStore ) {
4244 this . messagesStage = new BehaviorSubject < Message [ ] > ( [ ] ) ;
4345 this . messages$ = this . messagesStage . asObservable ( ) ;
46+ this . listenForConfigChanges ( ) ;
4447 this . initTimeOut ( ) ;
4548 }
4649
50+ /**
51+ * Listens for configuration changes related to alert timeout settings.
52+ *
53+ * This method subscribes to changes in the configuration observable stream,
54+ * specifically monitoring the 'alert_timeout' UI configuration. When a change
55+ * is detected, it parses the new alert timeout value and updates the instance's
56+ * timeout property accordingly if the parsed value is a valid number.
57+ *
58+ * @return {void }
59+ */
60+ private listenForConfigChanges ( ) : void {
61+ this . subscription = this . config . configs$
62+ . pipe (
63+ map ( ( ) => this . config . getUi ( 'alert_timeout' ) ) ,
64+ filter ( alertTimeout => alertTimeout !== null ) ,
65+ distinctUntilChanged ( ) ,
66+ )
67+ . subscribe ( alertTimeout => {
68+ const parsedTimeout = parseInt ( alertTimeout , 10 ) ;
69+ if ( ! isNaN ( parsedTimeout ) ) {
70+ this . timeout = parsedTimeout ;
71+ }
72+ } ) ;
73+ }
74+
4775 updateState ( messages : Message [ ] ) : void {
4876 this . messagesStage . next ( this . messages = messages ) ;
4977 }
0 commit comments