From 75ec2580484d2fa2d6d21a062ddb33ffea9ab046 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 29 Dec 2022 11:13:07 +0100 Subject: Process requests in correct order --- .../videos-list.component.ts | 62 +++++++++++++--------- 1 file changed, 38 insertions(+), 24 deletions(-) (limited to 'client/src/app') diff --git a/client/src/app/shared/shared-video-miniature/videos-list.component.ts b/client/src/app/shared/shared-video-miniature/videos-list.component.ts index d5cdd958e..a423377de 100644 --- a/client/src/app/shared/shared-video-miniature/videos-list.component.ts +++ b/client/src/app/shared/shared-video-miniature/videos-list.component.ts @@ -1,6 +1,6 @@ import * as debug from 'debug' import { fromEvent, Observable, Subject, Subscription } from 'rxjs' -import { debounceTime, switchMap } from 'rxjs/operators' +import { concatMap, debounceTime, map, switchMap } from 'rxjs/operators' import { Component, EventEmitter, Input, OnChanges, OnDestroy, OnInit, Output, SimpleChanges } from '@angular/core' import { ActivatedRoute } from '@angular/router' import { @@ -111,6 +111,8 @@ export class VideosListComponent implements OnInit, OnChanges, OnDestroy { private lastQueryLength: number + private videoRequests = new Subject<{ reset: boolean, obs: Observable> }>() + constructor ( private notifier: Notifier, private authService: AuthService, @@ -124,6 +126,8 @@ export class VideosListComponent implements OnInit, OnChanges, OnDestroy { } ngOnInit () { + this.subscribeToVideoRequests() + const hiddenFilters = this.hideScopeFilter ? [ 'scope' ] : [] @@ -228,30 +232,12 @@ export class VideosListComponent implements OnInit, OnChanges, OnDestroy { } loadMoreVideos (reset = false) { - if (reset) this.hasDoneFirstQuery = false - - this.getVideosObservableFunction(this.pagination, this.filters) - .subscribe({ - next: ({ data }) => { - this.hasDoneFirstQuery = true - this.lastQueryLength = data.length - - if (reset) this.videos = [] - this.videos = this.videos.concat(data) - - if (this.groupByDate) this.buildGroupedDateLabels() - - this.onDataSubject.next(data) - this.videosLoaded.emit(this.videos) - }, - - error: err => { - const message = $localize`Cannot load more videos. Try again later.` + if (reset) { + this.hasDoneFirstQuery = false + this.videos = [] + } - logger.error(message, err) - this.notifier.error(message) - } - }) + this.videoRequests.next({ reset, obs: this.getVideosObservableFunction(this.pagination, this.filters) }) } reloadVideos () { @@ -423,4 +409,32 @@ export class VideosListComponent implements OnInit, OnChanges, OnDestroy { this.onFiltersChanged(true) }) } + + private subscribeToVideoRequests () { + this.videoRequests + .pipe(concatMap(({ reset, obs }) => obs.pipe(map(({ data }) => ({ data, reset }))))) + .subscribe({ + next: ({ data, reset }) => { + console.log(data[0].name) + + this.hasDoneFirstQuery = true + this.lastQueryLength = data.length + + if (reset) this.videos = [] + this.videos = this.videos.concat(data) + + if (this.groupByDate) this.buildGroupedDateLabels() + + this.onDataSubject.next(data) + this.videosLoaded.emit(this.videos) + }, + + error: err => { + const message = $localize`Cannot load more videos. Try again later.` + + logger.error(message, err) + this.notifier.error(message) + } + }) + } } -- cgit v1.2.3