diff options
author | Chocobozzz <me@florianbigard.com> | 2023-07-10 16:08:53 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-07-10 16:08:53 +0200 |
commit | 8953f055c86ca74f145d7ac5ac93bb6104d73af9 (patch) | |
tree | 4fd67ba6c2ba32f45bcc92e931cffe2fa57c12a4 /client/src/standalone/embed-player-api/events.ts | |
parent | a1bd2b77d99cec5c27d38501f5f12f9dc339de17 (diff) | |
download | PeerTube-8953f055c86ca74f145d7ac5ac93bb6104d73af9.tar.gz PeerTube-8953f055c86ca74f145d7ac5ac93bb6104d73af9.tar.zst PeerTube-8953f055c86ca74f145d7ac5ac93bb6104d73af9.zip |
Rename player embed api
Diffstat (limited to 'client/src/standalone/embed-player-api/events.ts')
-rw-r--r-- | client/src/standalone/embed-player-api/events.ts | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/client/src/standalone/embed-player-api/events.ts b/client/src/standalone/embed-player-api/events.ts new file mode 100644 index 000000000..77d21c78c --- /dev/null +++ b/client/src/standalone/embed-player-api/events.ts | |||
@@ -0,0 +1,48 @@ | |||
1 | import { EventHandler } from './definitions' | ||
2 | |||
3 | interface PlayerEventRegistrar { | ||
4 | registrations: EventHandler<any>[] | ||
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 (const name of Object.keys(this.eventRegistrations)) { | ||
17 | channel.bind(name, (txn, params) => this.fire(name, params)) | ||
18 | } | ||
19 | } | ||
20 | |||
21 | public registerTypes (names: string[]) { | ||
22 | for (const name of names) { | ||
23 | this.eventRegistrations[name] = { registrations: [] } | ||
24 | } | ||
25 | } | ||
26 | |||
27 | public fire<T> (name: string, event: T) { | ||
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 | ||
35 | } | ||
36 | |||
37 | this.eventRegistrations[name].registrations.push(handler) | ||
38 | return true | ||
39 | } | ||
40 | |||
41 | public removeListener<T> (name: string, handler: EventHandler<T>) { | ||
42 | if (!this.eventRegistrations[name]) return false | ||
43 | |||
44 | this.eventRegistrations[name].registrations = this.eventRegistrations[name].registrations.filter(x => x !== handler) | ||
45 | |||
46 | return true | ||
47 | } | ||
48 | } | ||