aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/standalone/player/events.ts
diff options
context:
space:
mode:
authorWilliam Lahti <wilahti@gmail.com>2018-07-10 08:47:56 -0700
committerChocobozzz <me@florianbigard.com>2018-07-10 17:47:56 +0200
commit999417328bde0e60cd59318fc1c18672356254ce (patch)
tree22673fcbd4dc333e3362912b2c813e97a41c765f /client/src/standalone/player/events.ts
parent0b755f3b27190ea4d9c301ede0955b2736605f4c (diff)
downloadPeerTube-999417328bde0e60cd59318fc1c18672356254ce.tar.gz
PeerTube-999417328bde0e60cd59318fc1c18672356254ce.tar.zst
PeerTube-999417328bde0e60cd59318fc1c18672356254ce.zip
Ability to programmatically control embeds (#776)
* first stab at jschannel based player api * semicolon purge * more method-level docs; consolidate definitions * missing definitions * better match peertube's class conventions * styling for embed tester * basic docs * add `getVolume` * document the test-embed feature
Diffstat (limited to 'client/src/standalone/player/events.ts')
-rw-r--r--client/src/standalone/player/events.ts48
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 @@
1import { EventHandler } from "./definitions"
2
3interface PlayerEventRegistrar {
4 registrations : Function[]
5}
6
7interface PlayerEventRegistrationMap {
8 [name : string] : PlayerEventRegistrar
9}
10
11export 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}