Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit 643b32e

Browse files
fix(#773): ensure listener are destroyed when components are unmounted
1 parent 7a74857 commit 643b32e

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

studio/src/app/components/editor/actions/deck/app-actions-deck/app-actions-deck.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,30 @@ export class AppActionsDeck {
6767

6868
private anonymousService: AnonymousService;
6969

70+
private destroyListener;
71+
7072
constructor() {
7173
this.anonymousService = AnonymousService.getInstance();
7274
}
7375

7476
async componentWillLoad() {
7577
this.fullscreenEnable = !isIPad();
7678

77-
const destroyListener = remoteStore.onChange('pendingRequests', async (requests: DeckdeckgoEventDeckRequest[] | undefined) => {
79+
this.destroyListener = remoteStore.onChange('pendingRequests', async (requests: DeckdeckgoEventDeckRequest[] | undefined) => {
7880
if (requests && requests.length > 0) {
7981
await this.clickToOpenRemote();
8082
}
8183

82-
destroyListener();
84+
this.destroyListener();
8385
});
8486
}
8587

88+
componentDidUnload() {
89+
if (this.destroyListener) {
90+
this.destroyListener();
91+
}
92+
}
93+
8694
async onActionOpenSlideAdd($event: CustomEvent) {
8795
if (!$event || !$event.detail) {
8896
return;

studio/src/app/components/feed/app-feed/app-feed.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,30 @@ export class AppFeed {
2727

2828
private presentationUrl: string = EnvironmentConfigService.getInstance().get('deckdeckgo').presentationUrl;
2929

30+
private destroyListener;
31+
3032
constructor() {
3133
this.feedService = FeedService.getInstance();
3234
}
3335

3436
async componentWillLoad() {
3537
this.initOffline(offlineStore.state.offline);
3638

37-
const destroyListner = offlineStore.onChange('offline', (offline: OfflineDeck | undefined) => {
39+
this.destroyListener = offlineStore.onChange('offline', (offline: OfflineDeck | undefined) => {
3840
this.initOffline(offline);
3941

40-
destroyListner();
42+
this.destroyListener();
4143
});
4244

4345
this.mobile = isMobile();
4446
}
4547

48+
componentDidUnload() {
49+
if (this.destroyListener) {
50+
this.destroyListener();
51+
}
52+
}
53+
4654
private initOffline(offline: OfflineDeck | undefined) {
4755
this.offline = navigator && !navigator.onLine && offline !== undefined ? offline : undefined;
4856
}

studio/src/app/pages/core/app-dashboard/app-dashboard.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ export class AppDashboard {
4444
private imageEventsHandler: ImageEventsHandler = new ImageEventsHandler();
4545
private chartEventsHandler: ChartEventsHandler = new ChartEventsHandler();
4646

47+
private destroyListener;
48+
4749
constructor() {
4850
this.deckService = DeckService.getInstance();
4951
this.slideService = SlideService.getInstance();
@@ -54,19 +56,19 @@ export class AppDashboard {
5456
await this.imageEventsHandler.init();
5557
await this.chartEventsHandler.init();
5658

57-
const destroyListener = authStore.onChange('authUser', async (_authUser: AuthUser | null) => {
58-
await this.initDashboard(destroyListener);
59+
this.destroyListener = authStore.onChange('authUser', async (_authUser: AuthUser | null) => {
60+
await this.initDashboard();
5961
});
6062

61-
await this.initDashboard(destroyListener);
63+
await this.initDashboard();
6264
}
6365

64-
private async initDashboard(destroyListener) {
66+
private async initDashboard() {
6567
if (!authStore.state.authUser) {
6668
return;
6769
}
6870

69-
destroyListener();
71+
this.destroyListener();
7072

7173
const userDecks: Deck[] = await this.deckService.getUserDecks(authStore.state.authUser.uid);
7274
this.decks = await this.fetchFirstSlides(userDecks);
@@ -77,6 +79,10 @@ export class AppDashboard {
7779
}
7880

7981
componentDidUnload() {
82+
if (this.destroyListener) {
83+
this.destroyListener();
84+
}
85+
8086
this.imageEventsHandler.destroy();
8187
this.chartEventsHandler.destroy();
8288
}

studio/src/app/pages/editor/app-editor/app-editor.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export class AppEditor {
9797
private fullscreen: boolean = false;
9898

9999
private destroyBusyListener;
100+
private destroyAuthListener;
100101

101102
constructor() {
102103
this.authService = AuthService.getInstance();
@@ -139,12 +140,12 @@ export class AppEditor {
139140
private async initWithAuth() {
140141
if (!authStore.state.authUser) {
141142
// As soon as the anonymous is created, we proceed
142-
const destroyListener = authStore.onChange('authUser', async (authUser: AuthUser | null) => {
143+
this.destroyAuthListener = authStore.onChange('authUser', async (authUser: AuthUser | null) => {
143144
if (authUser) {
144145
await this.initOrFetch();
145146
}
146147

147-
destroyListener();
148+
this.destroyAuthListener();
148149
});
149150

150151
// If no user create an anonymous one
@@ -204,6 +205,10 @@ export class AppEditor {
204205
if (this.destroyBusyListener) {
205206
this.destroyBusyListener();
206207
}
208+
209+
if (this.destroyAuthListener) {
210+
this.destroyAuthListener();
211+
}
207212
}
208213

209214
private updateInlineEditorListener(): Promise<void> {

0 commit comments

Comments
 (0)