Skip to content

Conversation

@arturovt
Copy link
Contributor

@arturovt arturovt commented Feb 1, 2025

This avoids potential memory leaks and ensures correct behavior in reactive streams
(e.g. switchMap) by returning EMPTY, which completes immediately.

Use-case:

  • If load() is triggered via an observable (like within switchMap), and the service
    has already been destroyed (e.g. due to app shutdown, route unload, or SSR teardown),
    continuing execution would:
  • Create unnecessary subscriptions and pending async operations (like network calls).
  • Risk caching invalid/partial translation data.
  • In SSR, could retain memory or even leak state between requests.

Returning EMPTY ensures the observable chain completes cleanly without side effects.

Here’s a specific leak scenario this fix prevents:

componentLanguage$.pipe(
  switchMap(lang => translocoService.load(lang)),
).subscribe();

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 1, 2025

Open in StackBlitz

@jsverse/transloco

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco@835

@jsverse/transloco-locale

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-locale@835

@jsverse/transloco-messageformat

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-messageformat@835

@jsverse/transloco-optimize

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-optimize@835

@jsverse/transloco-persist-lang

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-persist-lang@835

@jsverse/transloco-persist-translations

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-persist-translations@835

@jsverse/transloco-preload-langs

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-preload-langs@835

@jsverse/transloco-schematics

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-schematics@835

@jsverse/transloco-scoped-libs

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-scoped-libs@835

@jsverse/transloco-utils

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-utils@835

@jsverse/transloco-validator

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-validator@835

commit: e31c6f7

@arturovt arturovt force-pushed the fix/transloco-destroyed branch 2 times, most recently from 052aa36 to 4396168 Compare October 12, 2025 09:26
@arturovt
Copy link
Contributor Author

@shaharkazaz friendly ping.

@shaharkazaz
Copy link
Collaborator

@arturovt Hey, thanks for the PR.
Please fill in the PR format as required.
Could you please specify the use case in which this might occur?

@arturovt arturovt force-pushed the fix/transloco-destroyed branch from 4396168 to b139f86 Compare October 12, 2025 17:31
@arturovt
Copy link
Contributor Author

I updated commit description.

This avoids potential memory leaks and ensures correct behavior in reactive streams
(e.g. switchMap) by returning EMPTY, which completes immediately.

Use-case:

- If load() is triggered via an observable (like within switchMap), and the service
has already been destroyed (e.g. due to app shutdown, route unload, or SSR teardown),
continuing execution would:
- Create unnecessary subscriptions and pending async operations (like network calls).
- Risk caching invalid/partial translation data.
- In SSR, could retain memory or even leak state between requests.

Returning EMPTY ensures the observable chain completes cleanly without side effects.

Here’s a specific leak scenario this fix prevents:

```js
componentLanguage$.pipe(
  switchMap(lang => translocoService.load(lang)),
).subscribe();
```
@arturovt arturovt force-pushed the fix/transloco-destroyed branch from b139f86 to e31c6f7 Compare October 28, 2025 11:07
@arturovt
Copy link
Contributor Author

@shaharkazaz I rebased, but ain't sure why CI may fail randomly...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants