/
Slides.ts
147 lines (124 loc) · 3.37 KB
/
Slides.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import {PlaybackState} from '../app/PlaybackManager';
import {ValueDispatcher} from '../events';
import type {Scene} from './Scene';
export interface Slide {
id: string;
name: string;
time: number;
scene: Scene;
stack?: string;
}
export class Slides {
public get onChanged() {
return this.slides.subscribable;
}
private readonly slides = new ValueDispatcher<Slide[]>([]);
private readonly lookup = new Map<string, Slide>();
private readonly collisionLookup = new Set<string>();
private current: Slide | null = null;
private canResume = false;
private waitsForId: string | null = null;
private targetId: string | null = null;
public constructor(private readonly scene: Scene) {
this.scene.onReloaded.subscribe(this.handleReload);
this.scene.onReset.subscribe(this.handleReset);
this.scene.onRecalculated.subscribe(this.handleRecalculated);
}
public setTarget(target: string | null) {
this.targetId = target;
}
public resume() {
this.canResume = true;
}
public isWaitingFor(slide: string) {
return this.waitsForId === slide;
}
public isWaiting() {
return this.waitsForId !== null;
}
public didHappen(slide: string) {
if (this.current === null) {
return false;
}
for (const key of this.lookup.keys()) {
if (key === slide) {
return true;
}
if (key === this.current?.id) {
return false;
}
}
return false;
}
public getCurrent() {
return this.current;
}
public register(name: string, initialTime: number) {
if (this.waitsForId !== null) {
throw new Error(
`The animation already waits for a slide: ${this.waitsForId}.`,
);
}
const id = this.toId(name);
if (this.scene.playback.state !== PlaybackState.Presenting) {
if (!this.lookup.has(id)) {
this.lookup.set(id, {
id,
name,
time: initialTime,
scene: this.scene,
stack: new Error().stack,
});
}
if (this.collisionLookup.has(name)) {
this.scene.logger.warn({
message: `A slide named "${name}" already exists.`,
stack: new Error().stack,
});
} else {
this.collisionLookup.add(name);
}
}
this.waitsForId = id;
this.current = this.lookup.get(id) ?? null;
this.canResume = false;
}
public shouldWait(name: string): boolean {
const id = this.toId(name);
if (this.waitsForId !== id) {
throw new Error(
`The animation waits for a different slide: ${this.waitsForId}.`,
);
}
const data = this.lookup.get(id);
if (!data) {
throw new Error(`Could not find the "${name}" slide.`);
}
let canResume = this.canResume;
if (this.scene.playback.state !== PlaybackState.Presenting) {
canResume = id !== this.targetId;
}
if (canResume) {
this.waitsForId = null;
}
return !canResume;
}
private handleReload = () => {
this.lookup.clear();
this.collisionLookup.clear();
this.current = null;
this.waitsForId = null;
this.targetId = null;
};
private handleReset = () => {
this.collisionLookup.clear();
this.current = null;
this.waitsForId = null;
};
private handleRecalculated = () => {
this.slides.current = [...this.lookup.values()];
};
private toId(name: string) {
return `${this.scene.name}:${name}`;
}
}