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))
20 public registerTypes(names : string[]) {
21 for (let name of names)
22 this.eventRegistrations[name] = { registrations: [] }
25 public fire<T>(name : string, event : T) {
26 this.eventRegistrations[name].registrations.forEach(x => x(event))
29 public addListener<T>(name : string, handler : EventHandler<T>) {
30 if (!this.eventRegistrations[name]) {
31 console.warn(`PeerTube: addEventListener(): The event '${name}' is not supported`)
35 this.eventRegistrations[name].registrations.push(handler)
39 public removeListener<T>(name : string, handler : EventHandler<T>) {
40 if (!this.eventRegistrations[name])
43 this.eventRegistrations[name].registrations =
44 this.eventRegistrations[name].registrations.filter(x => x === handler)