All files / viewer/src readiness.ts

96.66% Statements 29/30
83.33% Branches 5/6
100% Functions 13/13
100% Lines 26/26

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    75x               38x         66x       27x 27x 27x   9x     6x 2x 2x 2x         62x 62x   62x 62x 62x     99x 99x       35x 35x 2x       37x 37x 37x           137x 137x    
import type { LightViewerCaptureOptions, LightViewerController } from './viewer.js';
 
const notConnected = () => settled(Promise.reject(new Error('The viewer is not connected to a document')));
 
/**
 * Readiness and capture shared by `<light-viewer>` and the React `LightViewer`. The host marks loads with
 * {@link pending}/{@link finish}, and reports a new viewer with {@link attach} and teardown with {@link disconnect}.
 */
export class ViewerReadiness {
  /** Settles when the latest load settles; replaced by every load, so a superseded load never settles a waiter. */
  #ready = notConnected();
  #settle?: (error?: unknown) => void;
  #viewer?: LightViewerController;
  /** Whether a load is in progress. */
  get loading(): boolean {
    return this.#settle !== undefined;
  }
  async whenReady(): Promise<void> {
    let ready: Promise<void>;
    do {
      ready = this.#ready;
      await ready;
    } while (ready !== this.#ready);
    if (this.#viewer?.renderer.getContext().isContextLost()) throw new Error('The WebGL context was lost');
  }
  async toBlob(options?: LightViewerCaptureOptions): Promise<Blob> {
    await this.whenReady();
    const viewer = this.#viewer;
    Iif (!viewer) throw new Error('The viewer is not connected to a document');
    return viewer.capture(options);
  }
  /** Start waiting for a load. A superseded load resolves its promise so waiters move on to the new one. */
  pending(): void {
    let settle!: (error?: unknown) => void;
    const next = new Promise<void>((resolve, reject) => {
      settle = (error) => (error === undefined ? resolve() : reject(error));
    });
    this.#settle?.();
    this.#settle = settle;
    this.#ready = settled(next);
  }
  finish(error?: unknown): void {
    this.#settle?.(error);
    this.#settle = undefined;
  }
  /** Capture from a newly created viewer, and reject the current waiters if it loses its WebGL context. */
  attach(viewer: LightViewerController): void {
    this.#viewer = viewer;
    viewer.renderer.domElement.addEventListener('webglcontextlost', () =>
      this.finish(new Error('The WebGL context was lost')),
    );
  }
  disconnect(): void {
    this.finish(new Error('The viewer was disconnected'));
    this.#ready = notConnected();
    this.#viewer = undefined;
  }
}
 
/** Mark a stored promise handled, so an unawaited rejection is not reported; awaiting it still rejects. */
function settled(promise: Promise<void>): Promise<void> {
  promise.catch(() => {});
  return promise;
}