Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(store-devtools): resolve memory leak when devtools are destroyed #3965

Merged
merged 1 commit into from
Jul 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions modules/store-devtools/src/devtools.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, Inject, ErrorHandler } from '@angular/core';
import { Injectable, Inject, ErrorHandler, OnDestroy } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import {
Action,
Expand Down Expand Up @@ -33,8 +33,8 @@ import { DevtoolsDispatcher } from './devtools-dispatcher';
import { PERFORM_ACTION } from './actions';

@Injectable()
export class StoreDevtools implements Observer<any> {
private stateSubscription: Subscription;
export class StoreDevtools implements Observer<any>, OnDestroy {
private liftedStateSubscription: Subscription;
private extensionStartSubscription: Subscription;
public dispatcher: ActionsSubject;
public liftedState: Observable<LiftedState>;
Expand Down Expand Up @@ -71,7 +71,7 @@ export class StoreDevtools implements Observer<any> {

const liftedStateSubject = new ReplaySubject<LiftedState>(1);

const liftedStateSubscription = liftedAction$
this.liftedStateSubscription = liftedAction$
.pipe(
withLatestFrom(liftedReducer$),
scan<
Expand Down Expand Up @@ -110,7 +110,7 @@ export class StoreDevtools implements Observer<any> {
}
});

const extensionStartSubscription = extension.start$.subscribe(() => {
this.extensionStartSubscription = extension.start$.subscribe(() => {
this.refresh();
});

Expand All @@ -121,13 +121,21 @@ export class StoreDevtools implements Observer<any> {
value: toSignal(state$, { manualCleanup: true, requireSync: true }),
});

this.extensionStartSubscription = extensionStartSubscription;
this.stateSubscription = liftedStateSubscription;
Comment on lines -124 to -125
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference, why not just unsubscribe from these in the ngOnDestroy instead of adding a destroy Subject?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, my VSCode initially marked them as unused, so I removed them first. Then I added takeUntil since I saw it's already being used in other places too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can revert properties back and unsubscribe.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that approach would be better in this case because the subscription references are already there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

this.dispatcher = dispatcher;
this.liftedState = liftedState$;
this.state = state$;
}

ngOnDestroy(): void {
// Even though the store devtools plugin is recommended to be
// used only in development mode, it can still cause a memory leak
// in microfrontend applications that are being created and destroyed
// multiple times during development. This results in excessive memory
// consumption, as it prevents entire apps from being garbage collected.
this.liftedStateSubscription.unsubscribe();
this.extensionStartSubscription.unsubscribe();
}

dispatch(action: Action) {
this.dispatcher.next(action);
}
Expand Down