|
1 | 1 | /** |
2 | | - * ManagerCallbackFunction defines a callback function which is used when registering a new player |
| 2 | + * RegisterFunc defines a callback function which is used when registering a new player |
3 | 3 | */ |
4 | | -export type ManagerCallbackFunction = (factory: any, uid: string) => void; |
| 4 | +export type RegisterFunc = (factory: any, uid: string) => void; |
5 | 5 |
|
6 | 6 | /** |
7 | | - * Manager manages multiple player instances |
| 7 | + * EventsTree defines a lookup table for events emitted by the youtube player |
8 | 8 | */ |
9 | | -export interface Manager { |
10 | | - factory: any | undefined; |
11 | | - players: Array<ManagerCallbackFunction>; |
12 | | - events: Record<string, string>; |
13 | | - uid: number; |
| 9 | +export interface EventsTree { |
| 10 | + [key: string]: string; |
| 11 | +} |
14 | 12 |
|
15 | | - /** |
16 | | - * registerFactory registers the player factory |
17 | | - * @param factory The underlying YT factory (usually window.YT) |
18 | | - */ |
19 | | - registerFactory(factory: any): void; |
| 13 | +/** |
| 14 | + * Manager keeps track of player events and player instances |
| 15 | + */ |
| 16 | +export class Manager { |
| 17 | + private players!: Array<RegisterFunc>; |
| 18 | + private events!: EventsTree; |
| 19 | + private factory!: any; |
| 20 | + private uid!: number; |
20 | 21 |
|
21 | | - /** |
22 | | - * registerEvents registers all player state events |
23 | | - */ |
24 | | - registerEvents(): void; |
| 22 | + public constructor() { |
| 23 | + this.events = {}; |
| 24 | + this.events[YT.PlayerState.ENDED] = 'ended'; |
| 25 | + this.events[YT.PlayerState.PLAYING] = 'playing'; |
| 26 | + this.events[YT.PlayerState.PAUSED] = 'paused'; |
| 27 | + this.events[YT.PlayerState.BUFFERING] = 'buffering'; |
| 28 | + this.events[YT.PlayerState.CUED] = 'cued'; |
| 29 | + } |
25 | 30 |
|
26 | | - /** |
27 | | - * register registers a new player via the manager |
28 | | - * @param callback the callback function executed when the player gets loaded into the component |
29 | | - */ |
30 | | - register(callback: ManagerCallbackFunction): void; |
| 31 | + public registerFactory(factory: any): void { |
| 32 | + this.factory = factory; |
| 33 | + } |
31 | 34 |
|
32 | | - /** |
33 | | - * runBacklog iterates over registered players, which were unable to initiate because the YouTube API script wasn't |
34 | | - * ready yet |
35 | | - */ |
36 | | - runBacklog(): void; |
37 | | -} |
| 35 | + public register(callback: RegisterFunc): void { |
| 36 | + this.uid++; |
38 | 37 |
|
39 | | -/** |
40 | | - * createManager creates and returns the default manager |
41 | | - */ |
42 | | -export function createManager(): Manager { |
43 | | - return { |
44 | | - factory: undefined, |
45 | | - players: [], |
46 | | - events: {}, |
47 | | - uid: 0, |
48 | | - registerFactory(factory: any): void { |
49 | | - this.factory = factory; |
50 | | - }, |
51 | | - registerEvents(): void { |
52 | | - this.events[YT.PlayerState.ENDED] = 'ended'; |
53 | | - this.events[YT.PlayerState.PLAYING] = 'playing'; |
54 | | - this.events[YT.PlayerState.PAUSED] = 'paused'; |
55 | | - this.events[YT.PlayerState.BUFFERING] = 'buffering'; |
56 | | - this.events[YT.PlayerState.CUED] = 'cued'; |
57 | | - }, |
58 | | - register(callback: ManagerCallbackFunction): void { |
59 | | - this.uid++; |
| 38 | + if (this.factory != undefined) { |
| 39 | + callback(this.factory, `vue-youtube-iframe-${this.uid}`); |
| 40 | + return; |
| 41 | + } |
60 | 42 |
|
| 43 | + this.players.push(callback); |
| 44 | + } |
| 45 | + |
| 46 | + public runQueue(): void { |
| 47 | + this.players.forEach((callback) => { |
61 | 48 | if (this.factory != undefined) { |
| 49 | + this.uid++; |
62 | 50 | callback(this.factory, `vue-youtube-iframe-${this.uid}`); |
63 | | - return; |
64 | 51 | } |
65 | 52 |
|
66 | | - this.players.push(callback); |
67 | | - }, |
68 | | - runBacklog(): void { |
69 | | - this.players.forEach((callback) => { |
70 | | - if (this.factory != undefined) { |
71 | | - this.uid++; |
72 | | - callback(this.factory, `vue-youtube-iframe-${this.uid}`); |
73 | | - } |
| 53 | + this.players = []; |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + public getEvent(key: string): string { |
| 58 | + return this.events[key]; |
| 59 | + } |
| 60 | +} |
74 | 61 |
|
75 | | - this.players = []; |
76 | | - }); |
77 | | - }, |
78 | | - }; |
| 62 | +/** |
| 63 | + * createManager creates and returns the default manager |
| 64 | + */ |
| 65 | +export function createManager(): Manager { |
| 66 | + return new Manager(); |
79 | 67 | } |
80 | 68 |
|
81 | 69 | const manager = createManager(); |
| 70 | +Object.freeze(manager); |
| 71 | + |
82 | 72 | export default manager; |
0 commit comments