All files / core/src/RenderingEngine/GenericViewport DefaultRenderPathResolver.ts

18.75% Statements 3/16
14.28% Branches 1/7
14.28% Functions 1/7
20% Lines 3/15

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                    128x     128x                                                                                       128x  
import type {
  BaseViewportRenderContext,
  DataAddOptions,
  LoadedData,
  RenderPathDefinition,
  RenderPathResolver,
} from './ViewportArchitectureTypes';
import type ViewportType from '../../enums/ViewportType';
 
export class DefaultRenderPathResolver implements RenderPathResolver {
  private paths: RenderPathDefinition[] = [];
 
  constructor(paths: ReadonlyArray<RenderPathDefinition> = []) {
    paths.forEach((path) => this.add(path));
  }
 
  register<
    TRootContext extends BaseViewportRenderContext,
    TAdapterContext extends BaseViewportRenderContext,
  >(path: RenderPathDefinition<TRootContext, TAdapterContext>): void {
    this.add(path);
  }
 
  private add(path: RenderPathDefinition): void {
    const existingPath = this.paths.find(
      (candidate) => candidate.id === path.id
    );
 
    if (existingPath) {
      console.debug(
        `[DefaultRenderPathResolver] Duplicate render path registration ignored for "${path.id}"`
      );
      return;
    }
 
    this.paths.push(path);
  }
 
  resolve<TContext extends BaseViewportRenderContext>(
    type: ViewportType,
    data: LoadedData,
    options: DataAddOptions
  ): RenderPathDefinition<TContext, BaseViewportRenderContext> {
    const path = this.paths.find(
      (candidate) => candidate.type === type && candidate.matches(data, options)
    );
 
    if (!path) {
      throw new Error(
        `No render path for ${type}/${data.type}/${options.renderMode}`
      );
    }
 
    return path as RenderPathDefinition<TContext, BaseViewportRenderContext>;
  }
}
 
export const defaultRenderPathResolver = new DefaultRenderPathResolver();