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 }>()
private liveVideosSocket: Socket
constructor (
- private auth: AuthService,
- private ngZone: NgZone
+ private auth: AuthService
) {}
async getMyNotificationsSocket () {
this.liveVideosSocket.emit('subscribe', { videoId })
}
- async unsubscribeLiveVideos (videoId: number) {
+ unsubscribeLiveVideos (videoId: number) {
if (!this.liveVideosSocket) return
this.liveVideosSocket.emit('unsubscribe', { videoId })
await this.importIOIfNeeded()
- // Prevent protractor issues https://github.com/angular/angular/issues/11853
- 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.ngZone.run(() => this.dispatchNotificationEvent('new', n))
+ this.dispatchNotificationEvent('new', n)
})
}
await this.importIOIfNeeded()
- // Prevent protractor issues https://github.com/angular/angular/issues/11853
- this.ngZone.runOutsideAngular(() => {
- this.liveVideosSocket = this.io(environment.apiUrl + '/live-videos')
- })
+ this.liveVideosSocket = this.io(environment.apiUrl + '/live-videos')
const types: LiveVideoEventType[] = [ 'views-change', 'state-change' ]
for (const type of types) {
this.liveVideosSocket.on(type, (payload: LiveVideoEventPayload) => {
- this.ngZone.run(() => this.dispatchLiveVideoEvent(type, payload))
+ this.dispatchLiveVideoEvent(type, payload)
})
}
}