1 import { EventHandler } from './definitions'
3 interface PlayerEventRegistrar {
4 registrations: Function[]
7 interface PlayerEventRegistrationMap {
8 [ name: string ]: PlayerEventRegistrar
11 export class EventRegistrar {
13 private eventRegistrations: PlayerEventRegistrationMap = {}
15 public bindToChannel (channel: Channel.MessagingChannel) {
16 for (let name of Object.keys(this.eventRegistrations)) {
17 channel.bind(name, (txn, params) => this.fire(name, params))
21 public registerTypes (names: string[]) {
22 for (let name of names) {
23 this.eventRegistrations[ name ] = { registrations: [] }
27 public fire<T> (name: string, event: T) {
28 this.eventRegistrations[ name ].registrations.forEach(x => x(event))
31 public addListener<T> (name: string, handler: EventHandler<T>) {
32 if (!this.eventRegistrations[ name ]) {
33 console.warn(`PeerTube: addEventListener(): The event '${name}' is not supported`)
37 this.eventRegistrations[ name ].registrations.push(handler)
41 public removeListener<T> (name: string, handler: EventHandler<T>) {
42 if (!this.eventRegistrations[ name ]) return false
44 this.eventRegistrations[ name ].registrations = this.eventRegistrations[ name ].registrations.filter(x => x === handler)