From db914ee4bff9fb9c627754dc2430a54b0295def1 Mon Sep 17 00:00:00 2001 From: Baptiste Grob <60621355+baptiste-grob@users.noreply.github.com> Date: Sat, 27 Jun 2020 13:08:58 +0200 Subject: [PATCH] fix: remove app observer on controller deinit --- app/assets/javascripts/views/footer/footer_view.ts | 11 +++++++---- app/assets/javascripts/views/notes/notes_view.ts | 11 +++++++---- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/app/assets/javascripts/views/footer/footer_view.ts b/app/assets/javascripts/views/footer/footer_view.ts index 115e54f20..543a9a7a7 100644 --- a/app/assets/javascripts/views/footer/footer_view.ts +++ b/app/assets/javascripts/views/footer/footer_view.ts @@ -55,6 +55,7 @@ class FooterViewCtrl extends PureViewCtrl { public newUpdateAvailable = false public dockShortcuts: DockShortcut[] = [] public roomShowState: Partial> = {} + private observerRemovers: Array<() => void> = []; /* @ngInject */ constructor( @@ -69,6 +70,8 @@ class FooterViewCtrl extends PureViewCtrl { } deinit() { + for (const remove of this.observerRemovers) remove(); + this.observerRemovers.length = 0; this.rooms.length = 0; this.themesWithIcons.length = 0; this.unregisterComponent(); @@ -219,7 +222,7 @@ class FooterViewCtrl extends PureViewCtrl { } ) - this.application!.streamItems( + this.observerRemovers.push(this.application!.streamItems( ContentType.Component, async () => { const components = this.application!.getItems(ContentType.Component) as SNComponent[]; @@ -231,16 +234,16 @@ class FooterViewCtrl extends PureViewCtrl { this.reloadExtendedData(); } } - ); + )); - this.application!.streamItems( + this.observerRemovers.push(this.application!.streamItems( ContentType.Theme, async () => { const themes = this.application!.getDisplayableItems(ContentType.Theme) as SNTheme[]; this.themesWithIcons = themes; this.reloadDockShortcuts(); } - ); + )); } registerComponentHandler() { diff --git a/app/assets/javascripts/views/notes/notes_view.ts b/app/assets/javascripts/views/notes/notes_view.ts index 8bc4d4886..47158d1bc 100644 --- a/app/assets/javascripts/views/notes/notes_view.ts +++ b/app/assets/javascripts/views/notes/notes_view.ts @@ -66,6 +66,7 @@ class NotesViewCtrl extends PureViewCtrl { private searchKeyObserver: any private noteFlags: Partial> = {} private unsubEditorChange: any + private removeObservers: Array<() => void> = []; /* @ngInject */ constructor($timeout: ng.ITimeoutService, ) { @@ -89,6 +90,8 @@ class NotesViewCtrl extends PureViewCtrl { } deinit() { + for (const remove of this.removeObservers) remove(); + this.removeObservers.length = 0; this.panelPuppet!.onReady = undefined; this.panelPuppet = undefined; window.removeEventListener('resize', this.onWindowResize, true); @@ -198,7 +201,7 @@ class NotesViewCtrl extends PureViewCtrl { } streamNotesAndTags() { - this.application!.streamItems( + this.removeObservers.push(this.application!.streamItems( [ContentType.Note], async (items) => { const notes = items as SNNote[]; @@ -222,9 +225,9 @@ class NotesViewCtrl extends PureViewCtrl { this.selectFirstNote(); } } - ); + )); - this.application!.streamItems( + this.removeObservers.push(this.application!.streamItems( [ContentType.Tag], async (items) => { const tags = items as SNTag[]; @@ -236,7 +239,7 @@ class NotesViewCtrl extends PureViewCtrl { this.reloadPanelTitle(); } } - ); + )); } async selectNote(note: SNNote) {