All files / player-browser/src/session controller.ts

97.1% Statements 67/69
91.89% Branches 34/37
93.33% Functions 14/15
100% Lines 57/57

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                      90x   90x 90x 90x     90x       57x 56x 55x 55x 55x       119x 46x 46x 46x         105x 104x 104x 104x 102x 102x 5x 5x   102x       16x       19x     4x   5x 5x 5x 5x 2x                 1x 1x 1x 1x 10x 5x 5x 4x 4x 4x   1x 1x   5x   1x     1x 1x 3x 3x 2x 2x               76x 75x 75x 75x 75x 75x      
import type { SceneSession, SessionKeyEvent } from './contracts.ts';
 
/** A stable selected snapshot suitable for framework subscriptions; undefined before the first advance. */
export interface SceneStore<Value> {
  getSnapshot(): Value | undefined;
  subscribe(listener: () => void): () => void;
}
 
/** Drives a session without a renderer, DOM, global input or an owned clock. One driver, any number of observers. */
export class SceneController<Frame, Intent> {
  private readonly session: SceneSession<Frame, Intent>;
  private readonly listeners = new Set<() => void>();
  private snapshot: Frame | undefined;
  private active = false;
  private disposed = false;
  private generation = 0;
 
  public constructor(session: SceneSession<Frame, Intent>) {
    this.session = session;
  }
 
  public enter(nowMs: number): void {
    if (this.disposed) throw new Error('Cannot enter a disposed scene controller');
    if (this.active) return;
    this.active = true;
    this.generation++;
    this.session.enter(nowMs);
  }
 
  public exit(): void {
    if (!this.active) return;
    this.active = false;
    this.generation++;
    this.session.exit();
  }
 
  /** Advances once and broadcasts the same frame; observers never drain the session's transient events again. */
  public advance(nowMs: number): Frame | undefined {
    if (!this.active) return undefined;
    const generation = this.generation;
    const frame = this.session.frame(nowMs);
    if (!this.active || generation !== this.generation) return undefined;
    this.snapshot = frame;
    for (const listener of Array.from(this.listeners)) {
      listener();
      Iif (!this.active || generation !== this.generation) return undefined;
    }
    return frame;
  }
 
  public dispatch(intent: Intent): void {
    if (this.active) this.session.dispatch(intent);
  }
 
  public handleKey(event: SessionKeyEvent): boolean {
    return this.active && this.session.handleKey(event);
  }
 
  public getSnapshot = (): Frame | undefined => this.snapshot;
 
  public subscribe = (listener: () => void): (() => void) => {
    Iif (this.disposed) return () => {};
    this.listeners.add(listener);
    return () => {
      this.listeners.delete(listener);
    };
  };
 
  /** Select a read-only value. Use equals for allocated selections; only changed values notify the framework. */
  public createStore<Value>(
    select: (frame: Frame) => Value,
    equals: (a: Value, b: Value) => boolean = Object.is,
  ): SceneStore<Value> {
    let source = this.snapshot;
    let initialized = source !== undefined;
    let value = source === undefined ? undefined : select(source);
    const read = (): Value | undefined => {
      if (source === this.snapshot) return value;
      source = this.snapshot;
      if (source !== undefined) {
        const next = select(source);
        if (!initialized || !equals(value as Value, next)) value = next;
        initialized = true;
      } else {
        initialized = false;
        value = undefined;
      }
      return value;
    };
    return {
      getSnapshot: read,
      subscribe: (listener) => {
        let previous = read();
        return this.subscribe(() => {
          const next = read();
          if (!Object.is(previous, next)) {
            previous = next;
            listener();
          }
        });
      },
    };
  }
 
  public dispose(): void {
    if (this.disposed) return;
    this.exit();
    this.disposed = true;
    this.listeners.clear();
    this.snapshot = undefined;
    this.session.dispose();
  }
}