Correctly unsubscribe on menu destroy

This commit is contained in:
Chocobozzz 2023-02-16 16:13:19 +01:00
parent 4835b374d0
commit 6e060713b4
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
3 changed files with 51 additions and 29 deletions

View File

@ -58,7 +58,6 @@ export class RecommendedVideosComponent implements OnInit, OnChanges, OnDestroy
.subscribe(user => { .subscribe(user => {
this.user = user this.user = user
this.autoPlayNextVideo = user.autoPlayNextVideo this.autoPlayNextVideo = user.autoPlayNextVideo
console.log(this.autoPlayNextVideo)
}) })
} }

View File

@ -1,8 +1,9 @@
import { HotkeysService } from 'angular2-hotkeys' import { HotkeysService } from 'angular2-hotkeys'
import * as debug from 'debug' import * as debug from 'debug'
import { switchMap } from 'rxjs/operators' import { forkJoin, Subscription } from 'rxjs'
import { first, switchMap } from 'rxjs/operators'
import { ViewportScroller } from '@angular/common' import { ViewportScroller } from '@angular/common'
import { Component, OnInit, ViewChild } from '@angular/core' import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
import { Router } from '@angular/router' import { Router } from '@angular/router'
import { import {
AuthService, AuthService,
@ -30,7 +31,7 @@ const debugLogger = debug('peertube:menu:MenuComponent')
templateUrl: './menu.component.html', templateUrl: './menu.component.html',
styleUrls: [ './menu.component.scss' ] styleUrls: [ './menu.component.scss' ]
}) })
export class MenuComponent implements OnInit { export class MenuComponent implements OnInit, OnDestroy {
@ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent @ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent
@ViewChild('quickSettingsModal', { static: true }) quickSettingsModal: QuickSettingsModalComponent @ViewChild('quickSettingsModal', { static: true }) quickSettingsModal: QuickSettingsModalComponent
@ViewChild('dropdown') dropdown: NgbDropdown @ViewChild('dropdown') dropdown: NgbDropdown
@ -62,6 +63,11 @@ export class MenuComponent implements OnInit {
[UserRight.MANAGE_CONFIGURATION]: '/admin/config' [UserRight.MANAGE_CONFIGURATION]: '/admin/config'
} }
private languagesSub: Subscription
private modalSub: Subscription
private hotkeysSub: Subscription
private authSub: Subscription
constructor ( constructor (
private viewportScroller: ViewportScroller, private viewportScroller: ViewportScroller,
private authService: AuthService, private authService: AuthService,
@ -102,8 +108,7 @@ export class MenuComponent implements OnInit {
this.updateUserState() this.updateUserState()
this.buildMenuSections() this.buildMenuSections()
this.authService.loginChangedSource.subscribe( this.authSub = this.authService.loginChangedSource.subscribe(status => {
status => {
if (status === AuthStatus.LoggedIn) { if (status === AuthStatus.LoggedIn) {
this.isLoggedIn = true this.isLoggedIn = true
} else if (status === AuthStatus.LoggedOut) { } else if (status === AuthStatus.LoggedOut) {
@ -112,27 +117,34 @@ export class MenuComponent implements OnInit {
this.updateUserState() this.updateUserState()
this.buildMenuSections() this.buildMenuSections()
} })
)
this.hotkeysService.cheatSheetToggle this.hotkeysSub = this.hotkeysService.cheatSheetToggle
.subscribe(isOpen => this.helpVisible = isOpen) .subscribe(isOpen => this.helpVisible = isOpen)
this.serverService.getVideoLanguages() this.languagesSub = forkJoin([
.subscribe(languages => { this.serverService.getVideoLanguages(),
this.authService.userInformationLoaded.pipe(first())
]).subscribe(([ languages ]) => {
this.languages = languages this.languages = languages
this.authService.userInformationLoaded this.buildUserLanguages()
.subscribe(() => this.buildUserLanguages())
}) })
this.serverService.getConfig() this.serverService.getConfig()
.subscribe(config => this.serverConfig = config) .subscribe(config => this.serverConfig = config)
this.modalService.openQuickSettingsSubject this.modalSub = this.modalService.openQuickSettingsSubject
.subscribe(() => this.openQuickSettings()) .subscribe(() => this.openQuickSettings())
} }
ngOnDestroy () {
if (this.modalSub) this.modalSub.unsubscribe()
if (this.languagesSub) this.languagesSub.unsubscribe()
if (this.hotkeysSub) this.hotkeysSub.unsubscribe()
if (this.authSub) this.authSub.unsubscribe()
}
isRegistrationAllowed () { isRegistrationAllowed () {
if (!this.serverConfig) return false if (!this.serverConfig) return false

View File

@ -1,6 +1,6 @@
import { ReplaySubject } from 'rxjs' import { ReplaySubject, Subscription } from 'rxjs'
import { filter } from 'rxjs/operators' import { filter } from 'rxjs/operators'
import { Component, OnInit, ViewChild } from '@angular/core' import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
import { ActivatedRoute, Router } from '@angular/router' import { ActivatedRoute, Router } from '@angular/router'
import { AuthService, AuthStatus, LocalStorageService, User, UserService } from '@app/core' import { AuthService, AuthStatus, LocalStorageService, User, UserService } from '@app/core'
import { NgbModal } from '@ng-bootstrap/ng-bootstrap' import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
@ -10,7 +10,7 @@ import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
selector: 'my-quick-settings', selector: 'my-quick-settings',
templateUrl: './quick-settings-modal.component.html' templateUrl: './quick-settings-modal.component.html'
}) })
export class QuickSettingsModalComponent implements OnInit { export class QuickSettingsModalComponent implements OnInit, OnDestroy {
private static readonly QUERY_MODAL_NAME = 'quick-settings' private static readonly QUERY_MODAL_NAME = 'quick-settings'
@ViewChild('modal', { static: true }) modal: NgbModal @ViewChild('modal', { static: true }) modal: NgbModal
@ -20,6 +20,10 @@ export class QuickSettingsModalComponent implements OnInit {
private openedModal: NgbModalRef private openedModal: NgbModalRef
private routeSub: Subscription
private loginSub: Subscription
private localStorageSub: Subscription
constructor ( constructor (
private modalService: NgbModal, private modalService: NgbModal,
private userService: UserService, private userService: UserService,
@ -32,14 +36,15 @@ export class QuickSettingsModalComponent implements OnInit {
ngOnInit () { ngOnInit () {
this.user = this.userService.getAnonymousUser() this.user = this.userService.getAnonymousUser()
this.localStorageService.watch()
this.localStorageSub = this.localStorageService.watch()
.subscribe({ .subscribe({
next: () => this.user = this.userService.getAnonymousUser() next: () => this.user = this.userService.getAnonymousUser()
}) })
this.userInformationLoaded.next(true) this.userInformationLoaded.next(true)
this.authService.loginChangedSource this.loginSub = this.authService.loginChangedSource
.pipe(filter(status => status !== AuthStatus.LoggedIn)) .pipe(filter(status => status !== AuthStatus.LoggedIn))
.subscribe({ .subscribe({
next: () => { next: () => {
@ -48,7 +53,7 @@ export class QuickSettingsModalComponent implements OnInit {
} }
}) })
this.route.queryParams.subscribe(params => { this.routeSub = this.route.queryParams.subscribe(params => {
if (params['modal'] === QuickSettingsModalComponent.QUERY_MODAL_NAME) { if (params['modal'] === QuickSettingsModalComponent.QUERY_MODAL_NAME) {
this.openedModal = this.modalService.open(this.modal, { centered: true }) this.openedModal = this.modalService.open(this.modal, { centered: true })
@ -57,6 +62,12 @@ export class QuickSettingsModalComponent implements OnInit {
}) })
} }
ngOnDestroy () {
if (this.routeSub) this.routeSub.unsubscribe()
if (this.loginSub) this.loginSub.unsubscribe()
if (this.localStorageSub) this.localStorageSub.unsubscribe()
}
isUserLoggedIn () { isUserLoggedIn () {
return this.authService.isLoggedIn() return this.authService.isLoggedIn()
} }