Skip to content

Commit 925c4f6

Browse files
committed
fix(transloco-service): complete subject when root injector is destroyed
1 parent 2e3c8db commit 925c4f6

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

libs/transloco-locale/src/lib/transloco-locale.service.ts

+20-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { inject, Injectable, OnDestroy } from '@angular/core';
1+
import { DestroyRef, inject, Injectable } from '@angular/core';
22
import { TranslocoService } from '@jsverse/transloco';
3-
import { BehaviorSubject, Subscription } from 'rxjs';
3+
import { BehaviorSubject } from 'rxjs';
44
import { distinctUntilChanged, filter, map } from 'rxjs/operators';
55

66
import { isLocaleFormat, toDate } from './helpers';
@@ -27,7 +27,7 @@ import {
2727
@Injectable({
2828
providedIn: 'root',
2929
})
30-
export class TranslocoLocaleService implements OnDestroy {
30+
export class TranslocoLocaleService {
3131
private translocoService = inject(TranslocoService);
3232
private langLocaleMapping = inject(TRANSLOCO_LOCALE_LANG_MAPPING);
3333
private defaultLocale = inject(TRANSLOCO_LOCALE_DEFAULT_LOCALE);
@@ -39,16 +39,26 @@ export class TranslocoLocaleService implements OnDestroy {
3939

4040
private _locale =
4141
this.defaultLocale || this.toLocale(this.translocoService.getActiveLang());
42-
private locale: BehaviorSubject<Locale> = new BehaviorSubject(this._locale);
43-
private subscription: Subscription | null = this.translocoService.langChanges$
44-
.pipe(
45-
map((lang) => this.toLocale(lang)),
46-
filter(Boolean),
47-
)
48-
.subscribe((locale: Locale) => this.setLocale(locale));
42+
private locale = new BehaviorSubject<Locale>(this._locale);
4943

5044
localeChanges$ = this.locale.asObservable().pipe(distinctUntilChanged());
5145

46+
constructor() {
47+
inject(DestroyRef).onDestroy(() => {
48+
// Complete subjects to release observers if users forget to unsubscribe manually.
49+
// This is important in server-side rendering.
50+
this.locale.complete();
51+
});
52+
53+
this.translocoService.langChanges$
54+
.pipe(
55+
map((lang) => this.toLocale(lang)),
56+
filter(Boolean),
57+
)
58+
// Note: No need to unsubscribe because `lang` is completed by the `TranslocoService`.
59+
.subscribe((locale: Locale) => this.setLocale(locale));
60+
}
61+
5262
getLocale() {
5363
return this._locale;
5464
}
@@ -143,13 +153,6 @@ export class TranslocoLocaleService implements OnDestroy {
143153
return this.localeCurrencyMapping[locale] || this.defaultCurrency;
144154
}
145155

146-
ngOnDestroy() {
147-
this.subscription?.unsubscribe();
148-
// Caretaker note: it's important to clean up references to subscriptions since they save the `next`
149-
// callback within its `destination` property, preventing classes from being GC'd.
150-
this.subscription = null;
151-
}
152-
153156
private toLocale(val: string | Locale): Locale {
154157
if (this.langLocaleMapping[val]) {
155158
return this.langLocaleMapping[val];

0 commit comments

Comments
 (0)