aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/standalone/player/events.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/standalone/player/events.ts')
-rw-r--r--client/src/standalone/player/events.ts56
1 files changed, 28 insertions, 28 deletions
diff --git a/client/src/standalone/player/events.ts b/client/src/standalone/player/events.ts
index c01328352..f1639ef19 100644
--- a/client/src/standalone/player/events.ts
+++ b/client/src/standalone/player/events.ts
@@ -1,48 +1,48 @@
1import { EventHandler } from "./definitions" 1import { EventHandler } from './definitions'
2 2
3interface PlayerEventRegistrar { 3interface PlayerEventRegistrar {
4 registrations : Function[] 4 registrations: Function[]
5} 5}
6 6
7interface PlayerEventRegistrationMap { 7interface PlayerEventRegistrationMap {
8 [name : string] : PlayerEventRegistrar 8 [ name: string ]: PlayerEventRegistrar
9} 9}
10 10
11export class EventRegistrar { 11export class EventRegistrar {
12 12
13 private eventRegistrations : PlayerEventRegistrationMap = {} 13 private eventRegistrations: PlayerEventRegistrationMap = {}
14 14
15 public bindToChannel(channel : Channel.MessagingChannel) { 15 public bindToChannel (channel: Channel.MessagingChannel) {
16 for (let name of Object.keys(this.eventRegistrations)) 16 for (let name of Object.keys(this.eventRegistrations)) {
17 channel.bind(name, (txn, params) => this.fire(name, params)) 17 channel.bind(name, (txn, params) => this.fire(name, params))
18 } 18 }
19 }
19 20
20 public registerTypes(names : string[]) { 21 public registerTypes (names: string[]) {
21 for (let name of names) 22 for (let name of names) {
22 this.eventRegistrations[name] = { registrations: [] } 23 this.eventRegistrations[ name ] = { registrations: [] }
23 } 24 }
25 }
24 26
25 public fire<T>(name : string, event : T) { 27 public fire<T> (name: string, event: T) {
26 this.eventRegistrations[name].registrations.forEach(x => x(event)) 28 this.eventRegistrations[ name ].registrations.forEach(x => x(event))
29 }
30
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`)
34 return false
27 } 35 }
28 36
29 public addListener<T>(name : string, handler : EventHandler<T>) { 37 this.eventRegistrations[ name ].registrations.push(handler)
30 if (!this.eventRegistrations[name]) { 38 return true
31 console.warn(`PeerTube: addEventListener(): The event '${name}' is not supported`) 39 }
32 return false
33 }
34 40
35 this.eventRegistrations[name].registrations.push(handler) 41 public removeListener<T> (name: string, handler: EventHandler<T>) {
36 return true 42 if (!this.eventRegistrations[ name ]) return false
37 }
38 43
39 public removeListener<T>(name : string, handler : EventHandler<T>) { 44 this.eventRegistrations[ name ].registrations = this.eventRegistrations[ name ].registrations.filter(x => x === handler)
40 if (!this.eventRegistrations[name])
41 return false
42 45
43 this.eventRegistrations[name].registrations = 46 return true
44 this.eventRegistrations[name].registrations.filter(x => x === handler) 47 }
45
46 return true
47 }
48} 48}