Skip to content

Commit 640dc02

Browse files
FrancescoMolinaroAndrea Barbasso
authored andcommitted
Merged in task/dspace-cris-2023_02_x/DSC-1794 (pull request DSpace#1972)
[DSC-1794] fix klaro manager multiple initialization Approved-by: Andrea Barbasso
2 parents f81924c + 2b3376d commit 640dc02

5 files changed

Lines changed: 52 additions & 18 deletions

File tree

src/app/shared/cookies/browser-klaro.service.ts

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { BehaviorSubject, combineLatest as observableCombineLatest, Observable,
33
import { AuthService } from '../../core/auth/auth.service';
44
import { TranslateService } from '@ngx-translate/core';
55
import { environment } from '../../../environments/environment';
6-
import { map, switchMap, take } from 'rxjs/operators';
6+
import { filter, map, switchMap, take } from 'rxjs/operators';
77
import { EPerson } from '../../core/eperson/models/eperson.model';
88
import { CookieConsents, KlaroService } from './klaro.service';
99
import { 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);

src/app/shared/cookies/klaro.service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,8 @@ export abstract class KlaroService {
3333
* Subject to emit updates in the consents
3434
*/
3535
abstract consentsUpdates$: BehaviorSubject<CookieConsents>;
36+
/**
37+
* Subject to emit initialization
38+
*/
39+
abstract initialized$: BehaviorSubject<boolean>;
3640
}

src/app/shared/datadog-rum/browser-datadog-rum.service.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { Injectable } from '@angular/core';
22
import { environment } from '../../../environments/environment';
33
import { datadogRum } from '@datadog/browser-rum';
44
import { CookieConsents, KlaroService } from '../cookies/klaro.service';
5-
import { BehaviorSubject, Observable } from 'rxjs';
5+
import { BehaviorSubject, Observable, switchMap } from 'rxjs';
66
import { createSelector, Store } from '@ngrx/store';
77
import { setDatadogRumStatusAction } from './datadog-rum.actions';
88
import { DatadogRumState } from './datadog-rum.reducer';
9-
import { distinctUntilChanged, take } from 'rxjs/operators';
9+
import { distinctUntilChanged, filter, take, tap } from 'rxjs/operators';
1010
import { coreSelector } from '../../core/core.selectors';
1111
import { CoreState } from '../../core/core-state.model';
1212
import { DatadogRumService } from './datadog-rum.service';
@@ -22,12 +22,18 @@ export class BrowserDatadogRumService extends DatadogRumService {
2222
private store: Store
2323
) {
2424
super();
25+
this.consentsUpdates$ = this.klaroService.consentsUpdates$;
2526
}
2627

2728
initDatadogRum() {
28-
this.klaroService.watchConsentUpdates();
29-
this.consentsUpdates$ = this.klaroService.consentsUpdates$;
30-
this.consentsUpdates$.subscribe(savedPreferences => {
29+
this.klaroService.initialized$.pipe(
30+
filter(initalized => initalized),
31+
take(1),
32+
tap(() => {
33+
this.klaroService.watchConsentUpdates();
34+
}),
35+
switchMap(() => this.consentsUpdates$)
36+
).subscribe(savedPreferences => {
3137
this.getDatadogRumState().subscribe((state) => {
3238
if (savedPreferences?.datadog &&
3339
environment.datadogRum?.clientToken && environment.datadogRum?.applicationId &&

src/app/shared/datadog-rum/datadog-rum.service.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ describe('DatadogRumService', () => {
2828
getSavedPreferences: jasmine.createSpy('getSavedPreferences'),
2929
watchConsentUpdates: jasmine.createSpy('watchConsentUpdates')
3030
}, {
31-
consentsUpdates$: of(consentsAccepted)
31+
consentsUpdates$: of(consentsAccepted),
32+
initialized$: of(true),
3233
});
3334

3435
const datadogRumEnvironmentOptions = {

src/app/shared/metric/metric-altmetric/metric-altmetric.component.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ export class MetricAltmetricComponent extends BaseEmbeddedMetricComponent implem
4040
}
4141

4242
ngAfterViewInit(): void {
43-
// Show the altmetric label only when the altmetric component is ready
44-
this.renderer.listen(this.metricChild.nativeElement, 'altmetric:show', () => {
45-
this.showAltmetricLabel$.next(true);
46-
});
43+
if (hasValue(this.metricChild?.nativeElement)) {
44+
// Show the altmetric label only when the altmetric component is ready
45+
this.renderer.listen(this.metricChild.nativeElement, 'altmetric:show', () => {
46+
this.showAltmetricLabel$.next(true);
47+
});
48+
}
4749
}
4850
}

0 commit comments

Comments
 (0)