]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/shared-video-playlist/video-playlist.service.ts
Fetch things in bulk for the homepage
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-video-playlist / video-playlist.service.ts
index cc3d04b9e2f30254ddf52ae7a1026e1fc6f896be..a3f1393ff3318929d33fa462a93fe2fd0ca0d3fe 100644 (file)
@@ -1,11 +1,10 @@
 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 { 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,
@@ -52,15 +51,12 @@ export class VideoPlaylistService {
     private ngZone: NgZone
   ) {
     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,
+        ngZone: this.ngZone,
+        bulkGet: this.doVideosExistInPlaylist.bind(this),
+        notifierObservable: this.videoExistsInPlaylistNotifier
+      }),
 
       this.videoExistsInPlaylistCacheSubject
     )
@@ -215,10 +211,13 @@ export class VideoPlaylistService {
                  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)
                  }),
@@ -233,7 +232,11 @@ export class VideoPlaylistService {
                  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))