]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/video-playlist/video-playlist.service.ts
Fix angular 9 build
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video-playlist / video-playlist.service.ts
index 1ec9315efddb41de0e4bb4fd1a8cfb5870d81080..bae6f9e04f24c93f56ebc3eef5eae956962f7c29 100644 (file)
@@ -1,6 +1,6 @@
-import { bufferTime, catchError, filter, map, share, switchMap, tap } from 'rxjs/operators'
-import { Injectable } from '@angular/core'
-import { merge, Observable, of, ReplaySubject, Subject } from 'rxjs'
+import { bufferTime, catchError, filter, map, observeOn, share, switchMap, tap } from 'rxjs/operators'
+import { Injectable, NgZone } from '@angular/core'
+import { asyncScheduler, merge, Observable, of, ReplaySubject, Subject } from 'rxjs'
 import { RestExtractor } from '../rest/rest-extractor.service'
 import { HttpClient, HttpParams } from '@angular/common/http'
 import { ResultList, VideoPlaylistElementCreate, VideoPlaylistElementUpdate } from '../../../../../shared'
@@ -23,6 +23,7 @@ import { VideoPlaylistElement as ServerVideoPlaylistElement } from '@shared/mode
 import { VideoPlaylistElement } from '@app/shared/video-playlist/video-playlist-element.model'
 import { uniq } from 'lodash-es'
 import * as debug from 'debug'
+import { enterZone, leaveZone } from '@app/shared/rxjs/zone'
 
 const logger = debug('peertube:playlists:VideoPlaylistService')
 
@@ -42,19 +43,23 @@ export class VideoPlaylistService {
   private videoExistsCache: { [ id: number ]: VideoExistInPlaylist[] } = {}
 
   private myAccountPlaylistCache: ResultList<CachedPlaylist> = undefined
+  private myAccountPlaylistCacheRunning: Observable<ResultList<CachedPlaylist>>
   private myAccountPlaylistCacheSubject = new Subject<ResultList<CachedPlaylist>>()
 
   constructor (
     private authHttp: HttpClient,
     private serverService: ServerService,
     private restExtractor: RestExtractor,
-    private restService: RestService
+    private restService: RestService,
+    private ngZone: NgZone
   ) {
     this.videoExistsInPlaylistObservable = merge(
       this.videoExistsInPlaylistNotifier.pipe(
-        bufferTime(500),
+        // 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()
       ),
@@ -78,14 +83,24 @@ export class VideoPlaylistService {
   }
 
   listMyPlaylistWithCache (user: AuthUser, search?: string) {
-    if (!search && this.myAccountPlaylistCache) return of(this.myAccountPlaylistCache)
+    if (!search) {
+      if (this.myAccountPlaylistCacheRunning) return this.myAccountPlaylistCacheRunning
+      if (this.myAccountPlaylistCache) return of(this.myAccountPlaylistCache)
+    }
 
-    return this.listAccountPlaylists(user.account, undefined, '-updatedAt', search)
+    const obs = this.listAccountPlaylists(user.account, undefined, '-updatedAt', search)
                .pipe(
                  tap(result => {
-                   if (!search) this.myAccountPlaylistCache = result
-                 })
+                   if (!search) {
+                     this.myAccountPlaylistCacheRunning = undefined
+                     this.myAccountPlaylistCache = result
+                   }
+                 }),
+                 share()
                )
+
+    if (!search) this.myAccountPlaylistCacheRunning = obs
+    return obs
   }
 
   listAccountPlaylists (