]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/standalone/player/events.ts
Merge branch 'release/4.3.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / standalone / player / events.ts
CommitLineData
902aa3a0 1import { EventHandler } from './definitions'
99941732
WL
2
3interface PlayerEventRegistrar {
9df52d66 4 registrations: EventHandler<any>[]
99941732
WL
5}
6
7interface PlayerEventRegistrationMap {
902aa3a0 8 [ name: string ]: PlayerEventRegistrar
99941732
WL
9}
10
11export class EventRegistrar {
12
902aa3a0 13 private eventRegistrations: PlayerEventRegistrationMap = {}
99941732 14
902aa3a0 15 public bindToChannel (channel: Channel.MessagingChannel) {
c4710631 16 for (const name of Object.keys(this.eventRegistrations)) {
902aa3a0 17 channel.bind(name, (txn, params) => this.fire(name, params))
99941732 18 }
902aa3a0 19 }
99941732 20
902aa3a0 21 public registerTypes (names: string[]) {
c4710631 22 for (const name of names) {
9df52d66 23 this.eventRegistrations[name] = { registrations: [] }
99941732 24 }
902aa3a0 25 }
99941732 26
902aa3a0 27 public fire<T> (name: string, event: T) {
9df52d66 28 this.eventRegistrations[name].registrations.forEach(x => x(event))
902aa3a0
C
29 }
30
31 public addListener<T> (name: string, handler: EventHandler<T>) {
9df52d66 32 if (!this.eventRegistrations[name]) {
902aa3a0
C
33 console.warn(`PeerTube: addEventListener(): The event '${name}' is not supported`)
34 return false
99941732
WL
35 }
36
9df52d66 37 this.eventRegistrations[name].registrations.push(handler)
902aa3a0
C
38 return true
39 }
99941732 40
902aa3a0 41 public removeListener<T> (name: string, handler: EventHandler<T>) {
9df52d66 42 if (!this.eventRegistrations[name]) return false
99941732 43
c360e6cc 44 this.eventRegistrations[name].registrations = this.eventRegistrations[name].registrations.filter(x => x !== handler)
99941732 45
902aa3a0
C
46 return true
47 }
99941732 48}