import * as debug from 'debug'
-import { uniq } from 'lodash-es'
-import { asyncScheduler, merge, Observable, of, ReplaySubject, Subject } from 'rxjs'
-import { bufferTime, catchError, filter, map, observeOn, share, switchMap, tap } from 'rxjs/operators'
+import { merge, Observable, of, ReplaySubject, Subject } from 'rxjs'
+import { catchError, filter, map, share, switchMap, tap } from 'rxjs/operators'
import { HttpClient, HttpParams } from '@angular/common/http'
-import { Injectable, NgZone } from '@angular/core'
+import { Injectable } from '@angular/core'
import { AuthUser, ComponentPaginationLight, RestExtractor, RestService, ServerService } from '@app/core'
-import { enterZone, leaveZone, objectToFormData } from '@app/helpers'
+import { buildBulkObservable, objectToFormData } from '@app/helpers'
import { Account, AccountService, VideoChannel, VideoChannelService } from '@app/shared/shared-main'
import {
ResultList,
private authHttp: HttpClient,
private serverService: ServerService,
private restExtractor: RestExtractor,
- private restService: RestService,
- private ngZone: NgZone
+ private restService: RestService
) {
this.videoExistsInPlaylistObservable = merge(
- this.videoExistsInPlaylistNotifier.pipe(
- // We leave Angular zone so Protractor does not get stuck
- bufferTime(500, leaveZone(this.ngZone, asyncScheduler)),
- filter(videoIds => videoIds.length !== 0),
- map(videoIds => uniq(videoIds)),
- observeOn(enterZone(this.ngZone, asyncScheduler)),
- switchMap(videoIds => this.doVideosExistInPlaylist(videoIds)),
- share()
- ),
+ buildBulkObservable({
+ time: 500,
+ bulkGet: this.doVideosExistInPlaylist.bind(this),
+ notifierObservable: this.videoExistsInPlaylistNotifier
+ }).pipe(map(({ response }) => response)),
this.videoExistsInPlaylistCacheSubject
)
map(this.restExtractor.extractDataBool),
tap(() => {
const existsResult = this.videoExistsCache[videoId]
- const elem = existsResult.find(e => e.playlistElementId === playlistElementId)
- elem.startTimestamp = body.startTimestamp
- elem.stopTimestamp = body.stopTimestamp
+ if (existsResult) {
+ const elem = existsResult.find(e => e.playlistElementId === playlistElementId)
+
+ elem.startTimestamp = body.startTimestamp
+ elem.stopTimestamp = body.stopTimestamp
+ }
this.runPlaylistCheck(videoId)
}),
tap(() => {
if (!videoId) return
- this.videoExistsCache[videoId] = this.videoExistsCache[videoId].filter(e => e.playlistElementId !== playlistElementId)
+ if (this.videoExistsCache[videoId]) {
+ this.videoExistsCache[videoId] = this.videoExistsCache[videoId]
+ .filter(e => e.playlistElementId !== playlistElementId)
+ }
+
this.runPlaylistCheck(videoId)
}),
catchError(err => this.restExtractor.handleError(err))
)
}
- getPlaylistVideos (
- videoPlaylistId: number | string,
+ getPlaylistVideos (options: {
+ videoPlaylistId: number | string
componentPagination: ComponentPaginationLight
- ): Observable<ResultList<VideoPlaylistElement>> {
- const path = VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + videoPlaylistId + '/videos'
- const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
+ }): Observable<ResultList<VideoPlaylistElement>> {
+ const path = VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + options.videoPlaylistId + '/videos'
+ const pagination = this.restService.componentPaginationToRestPagination(options.componentPagination)
let params = new HttpParams()
params = this.restService.addRestGetParams(params, pagination)
}
listenToVideoPlaylistChange (videoId: number) {
- if (this.videoExistsObservableCache[ videoId ]) {
- return this.videoExistsObservableCache[ videoId ]
+ if (this.videoExistsObservableCache[videoId]) {
+ return this.videoExistsObservableCache[videoId]
}
const obs = this.videoExistsInPlaylistObservable
.pipe(
- map(existsResult => existsResult[ videoId ]),
+ map(existsResult => existsResult[videoId]),
filter(r => !!r),
- tap(result => this.videoExistsCache[ videoId ] = result)
+ tap(result => this.videoExistsCache[videoId] = result)
)
- this.videoExistsObservableCache[ videoId ] = obs
+ this.videoExistsObservableCache[videoId] = obs
return obs
}