Keep track of pause/play state so that player doesn't accidentally 'unpause' a user pause action (#189)

This commit is contained in:
Eoghan Murray
2026-04-01 12:00:00 +08:00
committed by GitHub
parent f8b20530d3
commit 3d4280b23a

View File

@@ -57,6 +57,8 @@ export class Replayer {
private missingNodeRetryMap: missingNodeMap = {}; private missingNodeRetryMap: missingNodeMap = {};
private playing: boolean = false;
constructor(events: eventWithTime[], config?: Partial<playerConfig>) { constructor(events: eventWithTime[], config?: Partial<playerConfig>) {
if (events.length < 2) { if (events.length < 2) {
throw new Error('Replayer need at least 2 events.'); throw new Error('Replayer need at least 2 events.');
@@ -144,11 +146,13 @@ export class Replayer {
} }
this.timer.addActions(actions); this.timer.addActions(actions);
this.timer.start(); this.timer.start();
this.playing = true;
this.emitter.emit(ReplayerEvents.Start); this.emitter.emit(ReplayerEvents.Start);
} }
public pause() { public pause() {
this.timer.clear(); this.timer.clear();
this.playing = false;
this.emitter.emit(ReplayerEvents.Pause); this.emitter.emit(ReplayerEvents.Pause);
} }
@@ -171,6 +175,7 @@ export class Replayer {
} }
this.timer.addActions(actions); this.timer.addActions(actions);
this.timer.start(); this.timer.start();
this.playing = true;
this.emitter.emit(ReplayerEvents.Resume); this.emitter.emit(ReplayerEvents.Resume);
} }
@@ -324,10 +329,12 @@ export class Replayer {
.forEach((css: HTMLLinkElement) => { .forEach((css: HTMLLinkElement) => {
if (!css.sheet) { if (!css.sheet) {
if (unloadSheets.size === 0) { if (unloadSheets.size === 0) {
this.pause(); this.timer.clear(); // artificial pause
this.emitter.emit(ReplayerEvents.LoadStylesheetStart); this.emitter.emit(ReplayerEvents.LoadStylesheetStart);
timer = window.setTimeout(() => { timer = window.setTimeout(() => {
if (this.playing) {
this.resume(this.getCurrentTime()); this.resume(this.getCurrentTime());
}
// mark timer was called // mark timer was called
timer = -1; timer = -1;
}, this.config.loadTimeout); }, this.config.loadTimeout);
@@ -336,7 +343,9 @@ export class Replayer {
css.addEventListener('load', () => { css.addEventListener('load', () => {
unloadSheets.delete(css); unloadSheets.delete(css);
if (unloadSheets.size === 0 && timer !== -1) { if (unloadSheets.size === 0 && timer !== -1) {
if (this.playing) {
this.resume(this.getCurrentTime()); this.resume(this.getCurrentTime());
}
this.emitter.emit(ReplayerEvents.LoadStylesheetEnd); this.emitter.emit(ReplayerEvents.LoadStylesheetEnd);
if (timer) { if (timer) {
window.clearTimeout(timer); window.clearTimeout(timer);