blob: 77d21c78c469b94541b5dbb5614e75d5e972809c (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
import { EventHandler } from './definitions'
interface PlayerEventRegistrar {
registrations: EventHandler<any>[]
}
interface PlayerEventRegistrationMap {
[ name: string ]: PlayerEventRegistrar
}
export class EventRegistrar {
private eventRegistrations: PlayerEventRegistrationMap = {}
public bindToChannel (channel: Channel.MessagingChannel) {
for (const name of Object.keys(this.eventRegistrations)) {
channel.bind(name, (txn, params) => this.fire(name, params))
}
}
public registerTypes (names: string[]) {
for (const name of names) {
this.eventRegistrations[name] = { registrations: [] }
}
}
public fire<T> (name: string, event: T) {
this.eventRegistrations[name].registrations.forEach(x => x(event))
}
public addListener<T> (name: string, handler: EventHandler<T>) {
if (!this.eventRegistrations[name]) {
console.warn(`PeerTube: addEventListener(): The event '${name}' is not supported`)
return false
}
this.eventRegistrations[name].registrations.push(handler)
return true
}
public removeListener<T> (name: string, handler: EventHandler<T>) {
if (!this.eventRegistrations[name]) return false
this.eventRegistrations[name].registrations = this.eventRegistrations[name].registrations.filter(x => x !== handler)
return true
}
}
|