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

100% Statements 18/18
100% Branches 8/8
100% Functions 5/5
100% Lines 16/16

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 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                              2x                                       5x         5x 5x         1x         5x               5x 3x 3x 3x   1x           3x 2x 2x 2x   1x        
// The player's replays: the app named as their recorder, and "Auto-save replays": a play's replay
// (`*.bmplay.json`, an input replay), or a course's (`*.bmcourse.json`), downloaded the moment its result shows.
 
import {
  resolveCourseReplayFilename,
  resolveReplayFilename,
  serializeCourseReplay,
  serializeReplay,
  type BeMusicCourseReplay,
  type BeMusicReplay,
  type ReplayRecorderInfo,
} from '@be-music/player-engine/replay';
import playerPackageJSON from '../../package.json' with { type: 'json' };
 
/** This app as the recorder of its replays (`recorder` in `*.bmplay.json`); the engine adds its own version. */
export const REPLAY_RECORDER: Omit<ReplayRecorderInfo, 'engine'> = {
  name: 'be-music-web',
  version: playerPackageJSON.version,
  ...(__BE_MUSIC_BUILD__.length > 0 ? { build: __BE_MUSIC_BUILD__ } : {}),
  platform: 'web',
};
 
export interface ReplayAutoSaveOptions {
  /** Saves `blob` under `filename` (`downloadBlob`). */
  readonly download: (blob: Blob, filename: string) => void;
  /** A save that failed; the result screen goes on. */
  readonly warn: (message: string, error: unknown) => void;
}
 
/**
 * Saves replays by the "Auto-save replays" setting as it was when the play started (`latch`): the checkbox is
 * disabled during a play, so a change can only come between plays, and it applies from the next one. A course's log is
 * saved by the value latched for its last stage played (a NONSTOP run is one play), the same as that stage's own log.
 */
export class ReplayAutoSave {
  private latched = false;
  private readonly download: (blob: Blob, filename: string) => void;
  private readonly warn: (message: string, error: unknown) => void;
 
  public constructor(options: ReplayAutoSaveOptions) {
    this.download = options.download;
    this.warn = options.warn;
  }
 
  /** Whether the play in flight, or the last one, saves its log. */
  public get enabled(): boolean {
    return this.latched;
  }
 
  /** Takes the setting for the play that is about to start. */
  public latch(enabled: boolean): void {
    this.latched = enabled;
  }
 
  /**
   * Downloads a play's log when the setting was on at its start. Failures are non-fatal — the result screen must never
   * be blocked by a download hiccup.
   */
  public savePlay(replay: BeMusicReplay | undefined): void {
    if (!this.latched || replay === undefined) return;
    try {
      const blob = new Blob([serializeReplay(replay)], { type: 'application/json' });
      this.download(blob, resolveReplayFilename(replay));
    } catch (error) {
      this.warn('replay auto-save failed', error);
    }
  }
 
  /** Downloads a course run's log (`<course>-<timestamp>.bmcourse.json`) when the setting was on at its last stage's start. */
  public saveCourse(courseLog: BeMusicCourseReplay): void {
    if (!this.latched) return;
    try {
      const blob = new Blob([serializeCourseReplay(courseLog)], { type: 'application/json' });
      this.download(blob, resolveCourseReplayFilename(courseLog));
    } catch (error) {
      this.warn('course replay auto-save failed', error);
    }
  }
}