All files / app-play/src/services status.ts

100% Statements 11/11
100% Branches 0/0
100% Functions 4/4
100% Lines 11/11

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 36 37 38 39 40 41 42 43 44 45 46 47 48 49                              2x           2x 2x 2x         4x               4x 4x 4x 4x         2x 2x      
// The status line: the Options dialog's read-only Status row, which every load, play, result and failure writes to, and
// which the test hook logs (`statusLog`).
 
export interface StatusLineOptions {
  /** The state the line is kept in: the Options dialog's, whose Status row shows it. */
  readonly settings: { status: string };
  /** Repaints the Status row (`OptionsDialog.refreshStatus`). */
  readonly refresh: () => void;
  /** Hands the line to the test hook (`TestHookRecorder.recordStatus`), when it is installed. */
  readonly record: (text: string) => void;
}
 
/** The status line, and whether it describes a play or result. */
export class StatusLine {
  /** Whether the status line describes a play or result, which a return to the select screen replaces. */
  private playStatusShown = false;
  private readonly settings: StatusLineOptions['settings'];
  private readonly refresh: StatusLineOptions['refresh'];
  private readonly record: StatusLineOptions['record'];
 
  public constructor(options: StatusLineOptions) {
    this.settings = options.settings;
    this.refresh = options.refresh;
    this.record = options.record;
  }
 
  /** Whether the line was last set by {@link setPlay}. */
  public get playShown(): boolean {
    return this.playStatusShown;
  }
 
  /**
   * Single chokepoint for status-text updates. Writes the new value into `guiState` and refreshes the Tweakpane
   * controller so the read-only row repaints with the new text.
   */
  public set(text: string): void {
    this.settings.status = text;
    this.playStatusShown = false;
    this.refresh();
    this.record(text);
  }
 
  /** Shows a status about the play or result on screen, which the select screen replaces when it comes back. */
  public setPlay(text: string): void {
    this.set(text);
    this.playStatusShown = true;
  }
}