All files / app-play/src/app scene-router.ts

93.54% Statements 116/124
92.47% Branches 86/93
91.3% Functions 21/23
96.07% Lines 98/102

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 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347                                                                                                                                                                                                                                                        169x 169x         169x   169x 169x     169x 169x 169x 169x                   24x         241x         46x               353x 353x 353x         986x                           325x 325x 325x 9x 9x   316x 316x       316x 316x 325x   316x 325x 316x 315x   315x 315x 41x   315x   315x 315x 315x                     106x 106x 106x 106x 106x 106x 106x 106x 106x 106x   106x 106x                     86x 86x 1x 1x     85x 85x 85x 85x 85x   5x 5x   57x 2x 2x   55x               14x 14x 14x 12x 9x 9x   12x 12x         445x 445x 445x         294x 168x 168x   1x         422x 422x   1x         126x         401x 401x 7x 7x         17x 13x         90x 90x 90x 90x 87x 87x 87x 59x   1x        
// The web player's scene router (ADR 0006): the one owner of the scene on stage. Every transition finalizes a recording
// in progress, hands the stage over with one `setScene` (the scene showing exits as the next enters, so the stage never
// shows an empty frame) and then disposes the scene that went, before the transition resolves. Transitions run one at a
// time, and a scene built for a navigation that a newer one replaced is disposed instead of shown.
//
// The select scene is kept alive across a decide, play and result (`docs/player-web.md`, "Scene lifecycle"); a play
// can load off stage behind a decide screen (`prepare`); and a play's sound can outlive its screen (`playDispose`).
//
// A run is the screens the scenes lead through on their own: a decide screen, a play and its result, and a course's
// every stage. A navigation the scenes asked for carries their run on (`continuesRun`); any other one (a drop, a skin
// or theme change, files or a replay loaded) interrupts it, and the host ends it (`onInterrupt`).
 
/** What the router asks of a scene: it disposes the scenes it owns. */
export interface RoutedScene {
  dispose(): void;
}
 
/** What the router asks of a play scene: it also loads it, off stage or while it shows. */
export interface RoutedPlayScene extends RoutedScene {
  prepare(onProgress?: (progress: number) => void): Promise<void>;
}
 
/**
 * The stage: `setScene(next, role)` exits the scene showing, then enters `next` (`undefined` leaves the stage empty),
 * and resolves once `next` has entered. `role` is what `next` is shown as (`undefined` with an empty stage).
 * `PixiSceneHost` is one.
 */
export interface SceneRouterHost<S> {
  setScene(scene: S | undefined, role?: SceneRole): Promise<void>;
}
 
/** The video recording of a play: `finalize` stops and saves one in progress, and does nothing otherwise. */
export interface SceneRouterRecorder {
  finalize(): Promise<void>;
}
 
/** The screen a scene is shown as. */
export type SceneRole = 'select' | 'decide' | 'play' | 'result';
 
/**
 * A navigation's generation (`begin`). A transition or a `prepare` handed a token runs only while no navigation has
 * begun since.
 */
export type SceneRouteToken = number;
 
/** How a transition ends the play leaving the stage with it; the default is `play.dispose()`. */
export type PlayDispose<P> = (play: P) => void;
 
/** What a router step reports to `onError`: a recording's finalize, a scene's dispose or the host's `onInterrupt`. */
export type SceneRouterErrorStep = 'finalize' | 'dispose' | 'interrupt';
 
export interface SceneNavigationOptions {
  /**
   * The scenes asked for this navigation, and it carries their run on: a play's result, a result's next stage or retry,
   * a course's decide screen and stages. Without it the navigation interrupts the run
   * ({@link SceneRouterOptions.onInterrupt}). Read only by a call that begins the navigation (`begin`, or a `show` or
   * `leave` with no `token`).
   */
  readonly continuesRun?: boolean;
}
 
export interface SceneShowOptions<P> extends SceneNavigationOptions {
  readonly role: SceneRole;
  /**
   * Keep the scene alive after another one takes over: the router exits it but never disposes it. The default is `true`
   * for the select scene, which the router then holds as {@link SceneRouter.select}, and `false` for every other
   * screen. The caller disposes a kept scene of another role itself.
   */
  readonly keep?: boolean;
  /** How the play leaving the stage with this transition is ended (`preserveAudioTail` when its result shows). */
  readonly playDispose?: PlayDispose<P>;
  /**
   * The navigation the scene was built for (`begin`). When a newer navigation has begun, the scene is disposed instead
   * of shown. Without one the transition begins a navigation of its own.
   */
  readonly token?: SceneRouteToken;
}
 
export interface SceneLeaveOptions<P> extends SceneNavigationOptions {
  /** How the play leaving the stage is ended. */
  readonly playDispose?: PlayDispose<P>;
  /** The navigation the leave belongs to; without one it begins a navigation of its own. */
  readonly token?: SceneRouteToken;
  /**
   * Keeps the play loading off stage, for a decide screen of the same navigation that shows next: its load began as the
   * chart was picked, before the select screen left.
   */
  readonly keepHeldPlay?: boolean;
}
 
export interface ScenePrepareOptions {
  /** The navigation the play was built for; a stale play is disposed instead of loaded. */
  readonly token?: SceneRouteToken;
  readonly onProgress?: (progress: number) => void;
}
 
export interface SceneRouterOptions<S> {
  readonly host: SceneRouterHost<S>;
  readonly recorder: SceneRouterRecorder;
  /**
   * A navigation begins that does not carry the run on ({@link SceneNavigationOptions.continuesRun}): the host ends
   * what the run was part of (a course's stages), so that it does not reach into the next play. Called as the
   * navigation begins, before any of its transitions runs; the scene showing then is still the run's.
   */
  readonly onInterrupt?: () => void;
  /**
   * A recording that failed to finalize, a scene that threw on dispose or an `onInterrupt` that threw; the navigation
   * goes on. Without it the error is rethrown on its own, as an uncaught error.
   */
  readonly onError?: (error: unknown, step: SceneRouterErrorStep) => void;
}
 
/**
 * Owns the scene on stage, the kept select scene and the play (`S` is any scene, `P` a play scene and `Sel` a select
 * scene). Transitions run one at a time, and only those of the newest navigation: `begin` starts one and hands out its
 * token, and a `show` or `leave` without a token starts one of its own. A navigation that does not carry the run on
 * interrupts it as it begins (`onInterrupt`). Read `current`, `select` and `play` for what the router holds.
 */
export class SceneRouter<S extends RoutedScene, P extends S & RoutedPlayScene, Sel extends S = S> {
  private readonly host: SceneRouterHost<S>;
  private readonly recorder: SceneRouterRecorder;
  private readonly onInterrupt: (() => void) | undefined;
  private readonly onError: (error: unknown, step: SceneRouterErrorStep) => void;
  /** The last transition in line; each one starts once it has settled. */
  private queue: Promise<unknown> = Promise.resolve();
  private generation = 0;
  private currentScene: S | undefined;
  private selectScene: Sel | undefined;
  private playScene: P | undefined;
  /** Scenes the router never disposes on its own. */
  private readonly kept = new WeakSet<S>();
  /** Scenes registered as plays (`prepare` or `role: 'play'`), ended with a `playDispose` when they leave. */
  private readonly plays = new WeakSet<S>();
  private readonly disposed = new WeakSet<S>();
 
  public constructor(options: SceneRouterOptions<S>) {
    this.host = options.host;
    this.recorder = options.recorder;
    this.onInterrupt = options.onInterrupt;
    this.onError =
      options.onError ??
      ((error) =>
        queueMicrotask(() => {
          throw error;
        }));
  }
 
  /** The scene on stage, once its transition has handed the stage over. */
  public get current(): S | undefined {
    return this.currentScene;
  }
 
  /** The kept select scene, on stage or off it. */
  public get select(): Sel | undefined {
    return this.selectScene;
  }
 
  /** The play: loading off stage behind a decide screen, or on stage. */
  public get play(): P | undefined {
    return this.playScene;
  }
 
  /**
   * Begins a navigation: every token handed out before is stale from now on. Unless it carries the run on
   * (`options.continuesRun`), it interrupts the run (`onInterrupt`).
   */
  public begin(options: SceneNavigationOptions = {}): SceneRouteToken {
    this.generation += 1;
    if (options.continuesRun !== true) this.interrupt();
    return this.generation;
  }
 
  /** Whether no navigation has begun since `token`'s. */
  public isCurrent(token: SceneRouteToken): boolean {
    return token === this.generation;
  }
 
  /**
   * Shows `scene` as `role`, after the transitions already in line: the recording in progress is finalized, `scene`
   * takes the stage from the scene showing (which exits as it enters), and the scene that went is disposed, unless it
   * is kept. A play loading off stage survives only a decide screen. Resolves `false`, with `scene` disposed unless it
   * is kept, when a newer navigation has begun by its turn (it begins one itself without `options.token`, interrupting
   * the run unless `options.continuesRun`), or when `scene` was already disposed.
   */
  public show(scene: P, options: SceneShowOptions<P> & { readonly role: 'play' }): Promise<boolean>;
  public show(scene: Sel, options: SceneShowOptions<P> & { readonly role: 'select' }): Promise<boolean>;
  public show(scene: S, options: SceneShowOptions<P> & { readonly role: 'decide' | 'result' }): Promise<boolean>;
  public show(scene: S, options: SceneShowOptions<P>): Promise<boolean> {
    const token = options.token ?? this.begin(options);
    return this.enqueue(async () => {
      if (!this.isCurrent(token) || this.disposed.has(scene)) {
        this.discard(scene);
        return false;
      }
      await this.finalizeRecording();
      Iif (!this.isCurrent(token)) {
        this.discard(scene);
        return false;
      }
      const previous = this.currentScene;
      const keep = options.keep ?? options.role === 'select';
      if (keep) this.kept.add(scene);
      const replacedSelect =
        options.role === 'select' && keep && this.selectScene !== scene ? this.selectScene : undefined;
      if (options.role === 'play') this.plays.add(scene);
      await this.host.setScene(scene, options.role);
      this.currentScene = scene;
      // The overloads take only a select scene as `role: 'select'`.
      if (options.role === 'select' && keep) this.selectScene = scene as Sel;
      if (previous !== undefined && previous !== scene && !this.kept.has(previous)) {
        this.release(previous, options.playDispose);
      }
      if (replacedSelect !== undefined) this.release(replacedSelect);
      // A play loading off stage waits only behind a decide screen.
      if (options.role !== 'decide') this.dropHeldPlay(scene);
      if (options.role === 'play' && this.isPlay(scene)) this.playScene = scene;
      return true;
    });
  }
 
  /**
   * Takes the scene showing off the stage, after the transitions already in line, and leaves the stage empty (files
   * loading, a play being built): the recording in progress is finalized, a kept scene stays for later and any other
   * is disposed, and so is a play loading off stage (unless `options.keepHeldPlay`). Resolves `false` when a newer navigation has begun by its turn
   * (it begins one itself without `options.token`, interrupting the run unless `options.continuesRun`).
   */
  public leave(options: SceneLeaveOptions<P> = {}): Promise<boolean> {
    const token = options.token ?? this.begin(options);
    return this.enqueue(async () => {
      Iif (!this.isCurrent(token)) return false;
      await this.finalizeRecording();
      Iif (!this.isCurrent(token)) return false;
      const previous = this.currentScene;
      Eif (previous !== undefined) {
        await this.host.setScene(undefined);
        this.currentScene = undefined;
        if (!this.kept.has(previous)) this.release(previous, options.playDispose);
      }
      if (options.keepHeldPlay !== true) this.dropHeldPlay();
      return true;
    });
  }
 
  /**
   * Loads the play `scene` (its `prepare`) and holds it as {@link play}: off stage behind a decide screen, or on stage
   * after `show`. A play it replaces is disposed when it is off stage. Not a transition: it runs at once. A play that
   * fails to load is disposed when off stage and the error rethrown; one whose `options.token` goes stale is disposed
   * and resolves `false`.
   */
  public async prepare(scene: P, options: ScenePrepareOptions = {}): Promise<boolean> {
    const { token } = options;
    if ((token !== undefined && !this.isCurrent(token)) || this.disposed.has(scene)) {
      this.discard(scene);
      return false;
    }
    // A play it replaces goes when off stage; one on stage goes with the transition that takes it off.
    if (this.playScene !== this.currentScene) this.dropHeldPlay(scene);
    this.plays.add(scene);
    this.playScene = scene;
    try {
      await scene.prepare(options.onProgress);
    } catch (error) {
      this.discard(scene);
      throw error;
    }
    if (token !== undefined && !this.isCurrent(token)) {
      this.discard(scene);
      return false;
    }
    return !this.disposed.has(scene);
  }
 
  /**
   * Disposes the kept select scene, after the transitions already in line, first taking it off the stage when it is
   * showing (a new theme or skin family: the next select scene is built afresh).
   */
  public dropSelect(): Promise<void> {
    return this.enqueue(async () => {
      const select = this.selectScene;
      if (select === undefined) return;
      if (this.currentScene === select) {
        await this.host.setScene(undefined);
        this.currentScene = undefined;
      }
      this.selectScene = undefined;
      this.release(select);
    });
  }
 
  private enqueue<T>(step: () => Promise<T>): Promise<T> {
    const run = this.queue.then(step);
    this.queue = run.catch(() => undefined);
    return run;
  }
 
  /** Tells the host the run is interrupted (`onInterrupt`); an error goes to `onError` and the navigation goes on. */
  private interrupt(): void {
    if (this.onInterrupt === undefined) return;
    try {
      this.onInterrupt();
    } catch (error) {
      this.onError(error, 'interrupt');
    }
  }
 
  private async finalizeRecording(): Promise<void> {
    try {
      await this.recorder.finalize();
    } catch (error) {
      this.onError(error, 'finalize');
    }
  }
 
  private isPlay(scene: S): scene is P {
    return this.plays.has(scene);
  }
 
  /** Stops holding the play (other than `except`), disposing it unless it is kept. */
  private dropHeldPlay(except?: S): void {
    const held = this.playScene;
    if (held === undefined || held === except) return;
    Iif (this.kept.has(held)) this.playScene = undefined;
    else this.release(held);
  }
 
  /** Disposes a scene that will not be shown, unless the router keeps it, shows it or already disposed it. */
  private discard(scene: S): void {
    if (this.kept.has(scene) || scene === this.currentScene || scene === this.selectScene) return;
    this.release(scene);
  }
 
  /** Disposes `scene` once (a play with `playDispose`), and forgets it. */
  private release(scene: S, playDispose?: PlayDispose<P>): void {
    const held: S | undefined = this.playScene;
    if (held === scene) this.playScene = undefined;
    Iif (this.selectScene === scene) this.selectScene = undefined;
    if (this.disposed.has(scene)) return;
    this.disposed.add(scene);
    try {
      if (playDispose !== undefined && this.isPlay(scene)) playDispose(scene);
      else scene.dispose();
    } catch (error) {
      this.onError(error, 'dispose');
    }
  }
}