diff options
Diffstat (limited to 'client/src/standalone/player/events.ts')
-rw-r--r-- | client/src/standalone/player/events.ts | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/client/src/standalone/player/events.ts b/client/src/standalone/player/events.ts new file mode 100644 index 000000000..c01328352 --- /dev/null +++ b/client/src/standalone/player/events.ts | |||
@@ -0,0 +1,48 @@ | |||
1 | import { EventHandler } from "./definitions" | ||
2 | |||
3 | interface PlayerEventRegistrar { | ||
4 | registrations : Function[] | ||
5 | } | ||
6 | |||
7 | interface PlayerEventRegistrationMap { | ||
8 | [name : string] : PlayerEventRegistrar | ||
9 | } | ||
10 | |||
11 | export class EventRegistrar { | ||
12 | |||
13 | private eventRegistrations : PlayerEventRegistrationMap = {} | ||
14 | |||
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)) | ||
18 | } | ||
19 | |||
20 | public registerTypes(names : string[]) { | ||
21 | for (let name of names) | ||
22 | this.eventRegistrations[name] = { registrations: [] } | ||
23 | } | ||
24 | |||
25 | public fire<T>(name : string, event : T) { | ||
26 | this.eventRegistrations[name].registrations.forEach(x => x(event)) | ||
27 | } | ||
28 | |||
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`) | ||
32 | return false | ||
33 | } | ||
34 | |||
35 | this.eventRegistrations[name].registrations.push(handler) | ||
36 | return true | ||
37 | } | ||
38 | |||
39 | public removeListener<T>(name : string, handler : EventHandler<T>) { | ||
40 | if (!this.eventRegistrations[name]) | ||
41 | return false | ||
42 | |||
43 | this.eventRegistrations[name].registrations = | ||
44 | this.eventRegistrations[name].registrations.filter(x => x === handler) | ||
45 | |||
46 | return true | ||
47 | } | ||
48 | } | ||