]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/core/notification/peertube-socket.service.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / client / src / app / core / notification / peertube-socket.service.ts
index 2f17fd70910f946586b19a4c7d3083dcb88ac34c..50a11e9485bf2e0cd36e7dddfc6f0a809ee9b219 100644 (file)
@@ -1,15 +1,15 @@
 import { Subject } from 'rxjs'
-import { Injectable, NgZone } from '@angular/core'
+import { ManagerOptions, Socket, SocketOptions } from 'socket.io-client'
+import { Injectable } from '@angular/core'
 import { LiveVideoEventPayload, LiveVideoEventType, UserNotification as UserNotificationServer } from '@shared/models'
 import { environment } from '../../../environments/environment'
 import { AuthService } from '../auth'
-import { io, Socket } from 'socket.io-client'
 
 export type NotificationEvent = 'new' | 'read' | 'read-all'
 
 @Injectable()
 export class PeerTubeSocket {
-  private io: typeof io
+  private io: (uri: string, opts?: Partial<ManagerOptions & SocketOptions>) => Socket
 
   private notificationSubject = new Subject<{ type: NotificationEvent, notification?: UserNotificationServer }>()
   private liveVideosSubject = new Subject<{ type: LiveVideoEventType, payload: LiveVideoEventPayload }>()
@@ -18,8 +18,7 @@ export class PeerTubeSocket {
   private liveVideosSocket: Socket
 
   constructor (
-    private auth: AuthService,
-    private ngZone: NgZone
+    private auth: AuthService
   ) {}
 
   async getMyNotificationsSocket () {
@@ -38,7 +37,7 @@ export class PeerTubeSocket {
     this.liveVideosSocket.emit('subscribe', { videoId })
   }
 
-  async unsubscribeLiveVideos (videoId: number) {
+  unsubscribeLiveVideos (videoId: number) {
     if (!this.liveVideosSocket) return
 
     this.liveVideosSocket.emit('unsubscribe', { videoId })
@@ -53,12 +52,12 @@ export class PeerTubeSocket {
 
     await this.importIOIfNeeded()
 
-    this.ngZone.runOutsideAngular(() => {
-      this.notificationSocket = this.io(environment.apiUrl + '/user-notifications', {
-        query: { accessToken: this.auth.getAccessToken() }
-      })
+    this.notificationSocket = this.io(environment.apiUrl + '/user-notifications', {
+      query: { accessToken: this.auth.getAccessToken() }
+    })
 
-      this.notificationSocket.on('new-notification', (n: UserNotificationServer) => this.dispatchNotificationEvent('new', n))
+    this.notificationSocket.on('new-notification', (n: UserNotificationServer) => {
+      this.dispatchNotificationEvent('new', n)
     })
   }
 
@@ -67,12 +66,15 @@ export class PeerTubeSocket {
 
     await this.importIOIfNeeded()
 
-    this.ngZone.runOutsideAngular(() => {
-      this.liveVideosSocket = this.io(environment.apiUrl + '/live-videos')
+    this.liveVideosSocket = this.io(environment.apiUrl + '/live-videos')
 
-      const type: LiveVideoEventType = 'state-change'
-      this.liveVideosSocket.on(type, (payload: LiveVideoEventPayload) => this.dispatchLiveVideoEvent(type, payload))
-    })
+    const types: LiveVideoEventType[] = [ 'views-change', 'state-change' ]
+
+    for (const type of types) {
+      this.liveVideosSocket.on(type, (payload: LiveVideoEventPayload) => {
+        this.dispatchLiveVideoEvent(type, payload)
+      })
+    }
   }
 
   private async importIOIfNeeded () {