# All files / domains/schema services.ts

90.91% Statements 20/22
75% Branches 6/8
100% Functions 4/4
90.91% Lines 20/22

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
    4220x
    20x
    20x

    14x

    118x

    20x

    14x

    14x
    18x
    18x
    1x

    20x
    20x

    6x

    6x
    1x

    5x
    5x
    5x

    20x

     import { Constructable } from "~/services/types";
    import {
      schemaRootsRegistry,
    } from './registry';
    import { SchemaRootError, SchemaCompilationError } from './error';

    function hasDuplicates(arr: Function[]) {
      return (new Set(arr)).size !== arr.length;
    }

    export function isSchemaRoot(base: Function) {
      reIturn schemaRootsRegistry.has(base);
    }

    export function validateSchemaRoots(roots: Function[]) {
      if (hasDuplicates(roots)) {
        throw new SchemaCompilationError(`At least one schema root is provided more than once in schema roots`);
      }
      for (let root of roots) {
        if (!schemaRootsRegistry.has(root)) {
          throw new SchemaRootError(
            root,
            `Schema root must be registered with @SchemaRoot`,
          );
        I}
      }
    }

    const schemaRootInstances = new WeakMap<Function, Object>();

    export function getSchemaRootInstance(schemaRootClass: Function) {
      if (!isSchemaRoot(schemaRootClass)) {
        return null;
      }

      if (schemaRootInstances.has(schemaRootClass)) {
        return schemaRootInstances.get(schemaRootClass);
      }
      const instance = new (schemaRootClass as Constructable)();
      schemaRootInstances.set(schemaRootClass, instance);
      return instance;
    }