ActivityPub-federated video streaming platform using P2P directly in your web browser
Fix client memory leaks
Chocobozzz361d51ae5263
diff --git a/client/src/app/+about/about-instance/children/about-instance-home.component.ts b/client/src/app/+about/about-instance/children/about-instance-home.component.tsindex f13008dcff..c7a87aaf07 100644--- a/client/src/app/+about/about-instance/children/about-instance-home.component.ts+++ b/client/src/app/+about/about-instance/children/about-instance-home.component.ts@@ -1,4 +1,4 @@-import { Component, OnInit, inject, viewChild } from '@angular/core'+import { Component, DestroyRef, OnInit, inject, viewChild } from '@angular/core'import { ActivatedRoute, Router } from '@angular/router'import { ServerService } from '@app/core'import { AboutHTML } from '@app/shared/shared-main/instance/instance.service'@@ -6,6 +6,7 @@ import { SupportModalComponent } from '@app/shared/shared-support-modal/support-import { HTMLServerConfig } from '@peertube/peertube-models'import { CustomMarkupContainerComponent } from '../../../shared/shared-custom-markup/custom-markup-container.component'import { ResolverData } from '../about-instance.resolver'+import { takeUntilDestroyed } from '@angular/core/rxjs-interop'@Component({templateUrl: './about-instance-home.component.html',@@ -16,6 +17,7 @@ import { ResolverData } from '../about-instance.resolver']})export class AboutInstanceHomeComponent implements OnInit {+ private destroyRef = inject(DestroyRef)private router = inject(Router)private route = inject(ActivatedRoute)private serverService = inject(ServerService)@@ -46,14 +48,16 @@ export class AboutInstanceHomeComponent implements OnInit {this.languages = languagesthis.categories = categories- this.route.data.subscribe(data => {- if (!data?.isSupport) return+ this.route.data+ .pipe(takeUntilDestroyed(this.destroyRef))+ .subscribe(data => {+ if (!data?.isSupport) return- setTimeout(() => {- const modal = this.supportModal().show()+ setTimeout(() => {+ const modal = this.supportModal().show()- modal.hidden.subscribe(() => this.router.navigateByUrl('/about/instance/home'))- }, 0)- })+ modal.hidden.subscribe(() => this.router.navigateByUrl('/about/instance/home'))+ }, 0)+ })}}diff --git a/client/src/app/+video-watch/video-watch.component.ts b/client/src/app/+video-watch/video-watch.component.tsindex 364011d683..a784612f0d 100644--- a/client/src/app/+video-watch/video-watch.component.ts+++ b/client/src/app/+video-watch/video-watch.component.ts@@ -190,6 +190,32 @@ export class VideoWatchComponent implements OnInit, OnDestroy {private hotkeys: Hotkey[] = []+ // ---------------------------------------------------------------------------++ private readonly onPlayerTimeUpdate = () => {+ const player = this.peertubePlayer?.getPlayer()+ if (!player) return++ const newTime = Math.floor(player.currentTime())++ // Update only if we have at least 1 second difference+ if (!this.currentTime || Math.abs(newTime - this.currentTime) >= 1) {+ debugLogger('Updating current time to ' + newTime)++ this.zone.run(() => this.currentTime = newTime)+ }+ }++ private readonly onPlayerEnded = () => {+ this.zone.run(() => this.endLive())+ }++ private readonly onPlayerTheaterChange = (_: any, enabled: boolean) => {+ this.zone.run(() => this.theaterEnabled = enabled)+ }++ // ---------------------------------------------------------------------------+get authUser () {return this.authService.getUser()}@@ -198,6 +224,8 @@ export class VideoWatchComponent implements OnInit, OnDestroy {return this.userService.getAnonymousUser()}+ // ---------------------------------------------------------------------------+async ngOnInit () {this.serverConfig = this.serverService.getHTMLConfig()@@ -222,6 +250,8 @@ export class VideoWatchComponent implements OnInit, OnDestroy {}ngOnDestroy () {+ this.unbindPlayerEventListeners()+if (this.peertubePlayer) this.peertubePlayer.destroy()// Unsubscribe subscriptions@@ -236,6 +266,8 @@ export class VideoWatchComponent implements OnInit, OnDestroy {this.metaService.revertMetaTags()}+ // ---------------------------------------------------------------------------+getCurrentTime () {return this.currentTime}@@ -622,26 +654,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy {const player = this.peertubePlayer.getPlayer()- player.on('timeupdate', () => {- const newTime = Math.floor(player.currentTime())-- // Update only if we have at least 1 second difference- if (!this.currentTime || Math.abs(newTime - this.currentTime) >= 1) {- debugLogger('Updating current time to ' + newTime)-- this.zone.run(() => this.currentTime = newTime)- }- })-- if (this.video.isLive) {- player.one('ended', () => {- this.zone.run(() => this.endLive())- })- }-- player.on('theater-change', (_: any, enabled: boolean) => {- this.zone.run(() => this.theaterEnabled = enabled)- })+ this.bindPlayerEventListeners(player, this.video.isLive)this.hooks.runAction('action:video-watch.player.loaded', 'video-watch', {player,@@ -653,6 +666,26 @@ export class VideoWatchComponent implements OnInit, OnDestroy {})}+ private bindPlayerEventListeners (player: VideojsPlayer, isLive: boolean) {+ this.unbindPlayerEventListeners(player)++ player.on('timeupdate', this.onPlayerTimeUpdate)+ player.on('theater-change', this.onPlayerTheaterChange)++ if (isLive) {+ player.one('ended', this.onPlayerEnded)+ }+ }++ private unbindPlayerEventListeners (player?: VideojsPlayer) {+ const playerInstance = player ?? this.peertubePlayer?.getPlayer()+ if (!playerInstance) return++ playerInstance.off('timeupdate', this.onPlayerTimeUpdate)+ playerInstance.off('theater-change', this.onPlayerTheaterChange)+ playerInstance.off('ended', this.onPlayerEnded)+ }+private hasNextVideo () {if (this.playlist) {return this.videoWatchPlaylist().hasNextVideo()diff --git a/client/src/app/shared/shared-custom-markup/custom-markup-container.component.ts b/client/src/app/shared/shared-custom-markup/custom-markup-container.component.tsindex 53e09c9398..1bc02d781f 100644--- a/client/src/app/shared/shared-custom-markup/custom-markup-container.component.ts+++ b/client/src/app/shared/shared-custom-markup/custom-markup-container.component.ts@@ -1,12 +1,13 @@-import { Component, ElementRef, OnChanges, inject, input, viewChild } from '@angular/core'+import { Component, ComponentRef, ElementRef, OnChanges, OnDestroy, inject, input, viewChild } from '@angular/core'import { CustomMarkupService } from './custom-markup.service'+import { CustomMarkupComponent } from './peertube-custom-tags/shared'@Component({selector: 'my-custom-markup-container',templateUrl: './custom-markup-container.component.html',standalone: true})-export class CustomMarkupContainerComponent implements OnChanges {+export class CustomMarkupContainerComponent implements OnChanges, OnDestroy {private customMarkupService = inject(CustomMarkupService)readonly contentWrapper = viewChild<ElementRef<HTMLInputElement>>('contentWrapper')@@ -15,22 +16,41 @@ export class CustomMarkupContainerComponent implements OnChanges {displayed = false+ private dynamicComponentRefs: ComponentRef<CustomMarkupComponent>[] = []+async ngOnChanges () {await this.rebuild()}+ ngOnDestroy () {+ this.destroyDynamicComponents()+ }+private async rebuild () {+ this.destroyDynamicComponents()+ const wrapper = this.contentWrapper()+ if (wrapper) wrapper.nativeElement.innerHTML = ''+const content = this.content()if (content instanceof HTMLDivElement) {return this.loadElement(content)}- const { rootElement, componentsLoaded } = await this.customMarkupService.buildElement(content)+ const { rootElement, componentsLoaded, componentRefs } = await this.customMarkupService.buildElement(content)+ this.dynamicComponentRefs = componentRefsawait componentsLoadedreturn this.loadElement(rootElement)}+ private destroyDynamicComponents () {+ for (const ref of this.dynamicComponentRefs) {+ ref.destroy()+ }++ this.dynamicComponentRefs = []+ }+private loadElement (el: HTMLDivElement) {this.contentWrapper().nativeElement.appendChild(el)diff --git a/client/src/app/shared/shared-custom-markup/custom-markup.service.ts b/client/src/app/shared/shared-custom-markup/custom-markup.service.tsindex 1f67286d2b..d58ea6ed34 100644--- a/client/src/app/shared/shared-custom-markup/custom-markup.service.ts+++ b/client/src/app/shared/shared-custom-markup/custom-markup.service.ts@@ -82,6 +82,8 @@ export class CustomMarkupService {const loadedPromises: Promise<boolean>[] = []+ const componentRefs: ComponentRef<CustomMarkupComponent>[] = []+for (const selector of Object.keys(this.angularBuilders)) {rootElement.querySelectorAll(selector).forEach((e: HTMLElement) => {@@ -89,6 +91,7 @@ export class CustomMarkupService {const { component, loadedPromise } = this.execAngularBuilder(selector, e)if (loadedPromise) loadedPromises.push(loadedPromise)+ componentRefs.push(component)this.dynamicElementService.injectElement(e, component)} catch (err) {logger.error(`Cannot inject component ${selector}`, err)@@ -96,7 +99,7 @@ export class CustomMarkupService {})}- return { rootElement, componentsLoaded: Promise.all(loadedPromises) }+ return { rootElement, componentsLoaded: Promise.all(loadedPromises), componentRefs }}private getSupportedTags () {diff --git a/client/src/app/shared/shared-forms/search-input.component.ts b/client/src/app/shared/shared-forms/search-input.component.tsindex 14f209c234..df34d17c03 100644--- a/client/src/app/shared/shared-forms/search-input.component.ts+++ b/client/src/app/shared/shared-forms/search-input.component.ts@@ -1,5 +1,6 @@import { NgClass } from '@angular/common'-import { AfterViewInit, Component, OnInit, booleanAttribute, inject, input, output } from '@angular/core'+import { AfterViewInit, Component, DestroyRef, OnInit, booleanAttribute, inject, input, output } from '@angular/core'+import { takeUntilDestroyed } from '@angular/core/rxjs-interop'import { FormsModule } from '@angular/forms'import { ActivatedRoute, Router } from '@angular/router'import debug from 'debug'@@ -21,6 +22,7 @@ const debugLogger = debug('peertube:SearchInputComponent')export class SearchInputComponent implements OnInit, AfterViewInit {private route = inject(ActivatedRoute)private router = inject(Router)+ private destroyRef = inject(DestroyRef)readonly placeholder = input($localize`Filter...`)readonly inputId = input('search-input')@@ -72,6 +74,7 @@ export class SearchInputComponent implements OnInit, AfterViewInit {private listenToRouteSearchChange () {this.route.queryParams+ .pipe(takeUntilDestroyed(this.destroyRef)).subscribe(params => {const search = params['search'] || ''@@ -90,7 +93,8 @@ export class SearchInputComponent implements OnInit, AfterViewInit {this.searchStream.pipe(debounceTime(300),- distinctUntilChanged()+ distinctUntilChanged(),+ takeUntilDestroyed(this.destroyRef)).subscribe(() => {this.setQueryParam(this.searchValue)diff --git a/client/src/app/shared/shared-user-subscription/subscribe-button.component.ts b/client/src/app/shared/shared-user-subscription/subscribe-button.component.tsindex 4aef712e22..59f3c9d5c3 100644--- a/client/src/app/shared/shared-user-subscription/subscribe-button.component.ts+++ b/client/src/app/shared/shared-user-subscription/subscribe-button.component.ts@@ -1,9 +1,11 @@import { NgClass, NgTemplateOutlet } from '@angular/common'-import { Component, OnChanges, inject, input, viewChild } from '@angular/core'+import { takeUntilDestroyed } from '@angular/core/rxjs-interop'+import { Component, DestroyRef, OnChanges, inject, input, viewChild } from '@angular/core'import { AuthService, Notifier, RedirectService } from '@app/core'import { NgbDropdown, NgbDropdownMenu, NgbDropdownToggle } from '@ng-bootstrap/ng-bootstrap'import { FeedFormat, FeedType } from '@peertube/peertube-models'import { concat, forkJoin, merge } from 'rxjs'+import { take } from 'rxjs/operators'import { Account } from '../shared-main/account/account.model'import { VideoChannel } from '../shared-main/channel/video-channel.model'import { VideoService } from '../shared-main/video/video.service'@@ -24,6 +26,7 @@ import { UserSubscriptionService } from './user-subscription.service']})export class SubscribeButtonComponent implements OnChanges {+ private destroyRef = inject(DestroyRef)private authService = inject(AuthService)private redirectService = inject(RedirectService)private notifier = inject(Notifier)@@ -228,16 +231,17 @@ export class SubscribeButtonComponent implements OnChanges {merge(this.userSubscriptionService.listenToSubscriptionCacheChange(handle),- this.userSubscriptionService.doesSubscriptionExist(handle)- ).subscribe({- next: res => {- this.subscribed.set(handle, res)+ this.userSubscriptionService.doesSubscriptionExist(handle).pipe(take(1))+ ).pipe(takeUntilDestroyed(this.destroyRef))+ .subscribe({+ next: res => {+ this.subscribed.set(handle, res)- this.buildClasses()- },+ this.buildClasses()+ },- error: err => this.notifier.handleError(err)- })+ error: err => this.notifier.handleError(err)+ })}}diff --git a/client/src/standalone/player/src/peertube-player.ts b/client/src/standalone/player/src/peertube-player.tsindex f882235900..1528870382 100644--- a/client/src/standalone/player/src/peertube-player.ts+++ b/client/src/standalone/player/src/peertube-player.ts@@ -140,7 +140,14 @@ export class PeerTubePlayer {}destroy () {- if (this.player) this.player.dispose()+ if (this.player) {+ this.disposeDynamicPluginsIfNeeded()+ this.player.dispose()+ }++ this.player = undefined+ this.currentLoadOptions = undefined+ this.pluginsManager = undefined}setPoster (thumbnails: Thumbnail[]) {diff --git a/client/src/standalone/player/src/shared/control-bar/peertube-link-button.ts b/client/src/standalone/player/src/shared/control-bar/peertube-link-button.tsindex 7e4079af9d..7e57030b50 100644--- a/client/src/standalone/player/src/shared/control-bar/peertube-link-button.ts+++ b/client/src/standalone/player/src/shared/control-bar/peertube-link-button.ts@@ -18,7 +18,7 @@ class PeerTubeLinkButton extends Component {}dispose () {- if (this.el()) return+ if (!this.el()) returnthis.el().removeEventListener('mouseenter', this.mouseEnterHandler)this.el().removeEventListener('click', this.clickHandler)