X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fcore%2Fnotification%2Fpeertube-socket.service.ts;h=bc3f7b89374093c986f4c93314158f3b37466c0f;hb=dc1f314efdae14f838c109ebbc1e8d69273d0319;hp=8668c44a8b05f7e9dbc7d4142df23c134fbd59af;hpb=a5cf76afa378aae81af2a9b0ce548e5d2582f832;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/core/notification/peertube-socket.service.ts b/client/src/app/core/notification/peertube-socket.service.ts index 8668c44a8..bc3f7b893 100644 --- a/client/src/app/core/notification/peertube-socket.service.ts +++ b/client/src/app/core/notification/peertube-socket.service.ts @@ -3,18 +3,19 @@ import { Injectable, NgZone } 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 import ('socket.io-client') + private io: typeof io private notificationSubject = new Subject<{ type: NotificationEvent, notification?: UserNotificationServer }>() private liveVideosSubject = new Subject<{ type: LiveVideoEventType, payload: LiveVideoEventPayload }>() - private notificationSocket: SocketIOClient.Socket - private liveVideosSocket: SocketIOClient.Socket + private notificationSocket: Socket + private liveVideosSocket: Socket constructor ( private auth: AuthService, @@ -52,13 +53,17 @@ export class PeerTubeSocket { 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.on('new-notification', (n: UserNotificationServer) => this.dispatchNotificationEvent('new', n)) + this.notificationSocket.on('new-notification', (n: UserNotificationServer) => { + this.ngZone.run(() => this.dispatchNotificationEvent('new', n)) + }) }) + } private async initLiveVideosSocket () { @@ -66,18 +71,24 @@ export class PeerTubeSocket { await this.importIOIfNeeded() + // Prevent protractor issues https://github.com/angular/angular/issues/11853 this.ngZone.runOutsideAngular(() => { 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.ngZone.run(() => this.dispatchLiveVideoEvent(type, payload)) + }) + } } private async importIOIfNeeded () { if (this.io) return - this.io = (await import('socket.io-client') as any).default + this.io = (await import('socket.io-client')).io } private dispatchLiveVideoEvent (type: LiveVideoEventType, payload: LiveVideoEventPayload) {