-
Notifications
You must be signed in to change notification settings - Fork 3
/
ProvenanceSlidedeckPlayer.ts
68 lines (59 loc) · 1.73 KB
/
ProvenanceSlidedeckPlayer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
export interface ISlide {
duration: number;
}
export enum STATUS {
IDLE,
PLAYING
}
const wait = (duration: number) => new Promise(resolve => setTimeout(resolve, duration));
export class ProvenanceSlidedeckPlayer<T extends ISlide> {
private readonly _selectCallback: (slide: T) => any;
private readonly _slides: T[];
private _currentSlideIndex: number;
private _status: STATUS;
constructor(slides: T[], selectCallback: (slide: T) => any) {
this._selectCallback = selectCallback;
this._slides = slides;
this._currentSlideIndex = 0;
this._status = STATUS.IDLE;
}
public setSlideIndex(slideIndex: number) {
this._currentSlideIndex = slideIndex;
}
public get slides() {
return this._slides;
}
public get currentSlideIndex() {
return this._currentSlideIndex;
}
public set currentSlideIndex(index: number) {
this._currentSlideIndex = index;
}
public async play() {
const shouldPlayNext = () =>
this._status === STATUS.PLAYING && this._currentSlideIndex < this._slides.length - 1;
if (this._status === STATUS.IDLE) {
this._status = STATUS.PLAYING;
this._selectCallback(this._slides[this._currentSlideIndex]);
do {
const slide = this._slides[this._currentSlideIndex];
await wait(slide.duration);
if (shouldPlayNext()) {
this._currentSlideIndex += 1;
this._selectCallback(this._slides[this._currentSlideIndex]);
}
} while (shouldPlayNext());
}
this._status = STATUS.IDLE;
}
public next() {
this._currentSlideIndex += 1;
this._selectCallback(this._slides[this._currentSlideIndex]);
}
public get status() {
return this._status;
}
public stop() {
this._status = STATUS.IDLE;
}
}