@@ -3,7 +3,7 @@ import { BehaviorSubject, combineLatest as observableCombineLatest, Observable,
33import { AuthService } from '../../core/auth/auth.service' ;
44import { TranslateService } from '@ngx-translate/core' ;
55import { environment } from '../../../environments/environment' ;
6- import { map , switchMap , take } from 'rxjs/operators' ;
6+ import { filter , map , switchMap , take } from 'rxjs/operators' ;
77import { EPerson } from '../../core/eperson/models/eperson.model' ;
88import { CookieConsents , KlaroService } from './klaro.service' ;
99import { hasValue , isEmpty , isNotEmpty } from '../empty.util' ;
@@ -65,9 +65,6 @@ export class BrowserKlaroService extends KlaroService {
6565 private readonly REGISTRATION_VERIFICATION_ENABLED_KEY = 'registration.verification.enabled' ;
6666
6767 private readonly GOOGLE_ANALYTICS_SERVICE_NAME = 'google-analytics' ;
68-
69- private lastCookiesConsents : CookieConsents ;
70-
7168 /**
7269 * Initial Klaro configuration
7370 */
@@ -77,8 +74,21 @@ export class BrowserKlaroService extends KlaroService {
7774 * Subject to emit updates in the consents
7875 */
7976 consentsUpdates$ : BehaviorSubject < CookieConsents > = new BehaviorSubject < CookieConsents > ( null ) ;
77+ /**
78+ * Subject to emit initialization
79+ */
80+ initialized$ : BehaviorSubject < boolean > = new BehaviorSubject < boolean > ( false ) ;
8081
81-
82+ /**
83+ * Boolean to check if a new watch method from the manager needs to be fired
84+ * @private
85+ */
86+ private isKlaroManagerWatching = false ;
87+ /**
88+ * Boolean to check if service has been initialized
89+ * @private
90+ */
91+ private initialized = false ;
8292 constructor (
8393 private translateService : TranslateService ,
8494 private authService : AuthService ,
@@ -180,8 +190,16 @@ export class BrowserKlaroService extends KlaroService {
180190 this . translateConfiguration ( ) ;
181191
182192 this . klaroConfig . services = this . filterConfigServices ( servicesToHide ) ;
183- this . lazyKlaro . then ( ( { setup } ) => setup ( this . klaroConfig ) ) ;
193+ this . lazyKlaro . then ( ( { setup } ) => {
194+ setup ( this . klaroConfig ) ;
195+ this . initialized = true ;
196+ this . initialized$ . next ( this . initialized ) ;
197+ } ) ;
184198 } ) ;
199+
200+ this . consentsUpdates$ . pipe (
201+ filter ( ( ) => this . initialized )
202+ ) . subscribe ( ( consents ) => this . isKlaroManagerWatching = hasValue ( consents ) ) ;
185203 }
186204
187205 /**
@@ -366,15 +384,18 @@ export class BrowserKlaroService extends KlaroService {
366384 }
367385
368386 watchConsentUpdates ( ) : void {
387+ if ( this . isKlaroManagerWatching || ! this . initialized ) {
388+ return ;
389+ }
390+
369391 this . lazyKlaro . then ( ( { getManager} ) => {
370392 const manager = getManager ( this . klaroConfig ) ;
371393 const consentsSubject$ = this . consentsUpdates$ ;
372- let lastCookiesConsents = this . lastCookiesConsents ;
394+ let lastCookiesConsents ;
373395
374396 consentsSubject$ . next ( manager . consents ) ;
375397 manager . watch ( {
376398 update ( _ , eventName , consents ) {
377-
378399 if ( eventName === 'consents' && ! isEqual ( consents , lastCookiesConsents ) ) {
379400 lastCookiesConsents = deepClone ( consents ) ;
380401 consentsSubject$ . next ( consents ) ;
0 commit comments