]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/notification/user-notification-socket.service.ts
Lazy import some modules
[github/Chocobozzz/PeerTube.git] / client / src / app / core / notification / user-notification-socket.service.ts
CommitLineData
9a39392a
C
1import { Injectable } from '@angular/core'
2import { environment } from '../../../environments/environment'
3import { UserNotification as UserNotificationServer } from '../../../../../shared'
4import { Subject } from 'rxjs'
5import * as io from 'socket.io-client'
6import { AuthService } from '../auth'
7
8export type NotificationEvent = 'new' | 'read' | 'read-all'
9
10@Injectable()
11export class UserNotificationSocket {
12 private notificationSubject = new Subject<{ type: NotificationEvent, notification?: UserNotificationServer }>()
13
14 private socket: SocketIOClient.Socket
15
16 constructor (
17 private auth: AuthService
18 ) {}
19
20 dispatch (type: NotificationEvent, notification?: UserNotificationServer) {
21 this.notificationSubject.next({ type, notification })
22 }
23
41d71344
C
24 async getMyNotificationsSocket () {
25 await this.initSocket()
9a39392a
C
26
27 return this.notificationSubject.asObservable()
28 }
29
41d71344
C
30 private async initSocket () {
31 if (this.socket) return
32
33 // FIXME: import('..') returns a struct module, containing a "default" field corresponding to our sanitizeHtml function
34 const io: typeof import ('socket.io-client') = (await import('socket.io-client') as any).default
9a39392a
C
35
36 this.socket = io(environment.apiUrl + '/user-notifications', {
37 query: { accessToken: this.auth.getAccessToken() }
38 })
39
41d71344 40 this.socket.on('new-notification', (n: UserNotificationServer) => this.dispatch('new', n))
9a39392a
C
41 }
42}