]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/helpers/utils.ts
Fix async issues with channels list
[github/Chocobozzz/PeerTube.git] / client / src / app / helpers / utils.ts
index 6d7e76b1168d7de3b20900ac61eecc0cb5826a04..a1747af3cae1f17db516a00537b3ebfb1bbf80b7 100644 (file)
@@ -1,3 +1,4 @@
+import { map } from 'rxjs/operators'
 import { SelectChannelItem } from 'src/types/select-options-item.model'
 import { DatePipe } from '@angular/common'
 import { HttpErrorResponse } from '@angular/common/http'
@@ -20,31 +21,22 @@ function getParameterByName (name: string, url: string) {
   return decodeURIComponent(results[2].replace(/\+/g, ' '))
 }
 
-function populateAsyncUserVideoChannels (
-  authService: AuthService,
-  channel: SelectChannelItem[]
-) {
-  return new Promise<void>(res => {
-    authService.userInformationLoaded
-      .subscribe(
-        () => {
-          const user = authService.getUser()
-          if (!user) return
-
-          const videoChannels = user.videoChannels
-          if (Array.isArray(videoChannels) === false) return
-
-          videoChannels.forEach(c => channel.push({
-            id: c.id,
-            label: c.displayName,
-            support: c.support,
-            avatarPath: c.avatar?.path
-          }))
-
-          return res()
-        }
-      )
-  })
+function listUserChannels (authService: AuthService) {
+  return authService.userInformationLoaded
+    .pipe(map(() => {
+      const user = authService.getUser()
+      if (!user) return undefined
+
+      const videoChannels = user.videoChannels
+      if (Array.isArray(videoChannels) === false) return undefined
+
+      return videoChannels.map(c => ({
+        id: c.id,
+        label: c.displayName,
+        support: c.support,
+        avatarPath: c.avatar?.path
+      }) as SelectChannelItem)
+    }))
 }
 
 function getAbsoluteAPIUrl () {
@@ -207,7 +199,6 @@ export {
   durationToString,
   lineFeedToHtml,
   getParameterByName,
-  populateAsyncUserVideoChannels,
   getAbsoluteAPIUrl,
   dateToHuman,
   immutableAssign,
@@ -218,5 +209,6 @@ export {
   scrollToTop,
   isInViewport,
   isXPercentInViewport,
+  listUserChannels,
   uploadErrorHandler
 }