All files / app-play/src/ui dialog-pause.ts

100% Statements 17/17
100% Branches 15/15
100% Functions 2/2
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                2x       2x       8x 4x 3x 3x 2x 2x     4x 4x   4x 4x 3x 3x 3x      
/** A play a modal can temporarily pause without resuming a play the user had already paused. */
export interface PausablePlay {
  isPaused(): boolean;
  requestTogglePause(): void;
}
 
/** Keeps one pause across overlapping Help and Settings dialogs. */
export class DialogPauseController {
  private readonly open = new Set<string>();
  private owned: PausablePlay | undefined;
  private readonly current: () => PausablePlay | undefined;
  constructor(current: () => PausablePlay | undefined) {
    this.current = current;
  }
 
  setOpen(dialog: string, open: boolean): void {
    if (open) {
      if (this.open.size === 0) {
        const play = this.current();
        if (play && !play.isPaused()) {
          play.requestTogglePause();
          this.owned = play;
        }
      }
      this.open.add(dialog);
      return;
    }
    this.open.delete(dialog);
    if (this.open.size > 0) return;
    const play = this.owned;
    this.owned = undefined;
    if (play && play === this.current() && play.isPaused()) play.requestTogglePause();
  }
}