All files / shell/src/component registry.ts

100% Statements 58/58
100% Branches 10/10
100% Functions 1/1
100% Lines 58/58

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 591x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 87x 87x 49x 49x 49x 49x 49x 49x 49x 76x 76x 14x 14x 14x 14x 14x 14x 4x 4x 4x 3x 3x 14x 14x 14x 14x 87x 87x 87x 87x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
/**
 * Internal {@link Map}ping of all registered **elements** to their name.
 */
const elements = new Map<CustomElementConstructor, string>();
 
/**
 * {@link Proxy} around the built-in {@link CustomElementRegistry}, maintaining
 * a mapping of all registered elements and their corresponding names, which can
 * be queried by calling {@link registry.getName}.
 *
 * @remarks https://github.com/WICG/webcomponents/issues/566
 */
const registry = new Proxy(customElements, {
  get: (_, propertyKey: keyof CustomElementRegistry | 'getName') => {
    switch (propertyKey) {
      case 'define': return (
        name: string,
        constructor: CustomElementConstructor,
        options?: ElementDefinitionOptions
      ) => {
        customElements.define(name, constructor, options);
        elements.set(constructor, name);
      };
 
      case 'getName': return (
        constructor: CustomElementConstructor
      ) => {
        let name = elements.get(constructor);
 
        if (!name) {
          try {
            name = Reflect.construct(HTMLElement, [], constructor).localName;
            elements.set(constructor, name);
          } catch {
            return undefined;
          }
        }
 
        return name;
      };
    }
 
    return customElements[propertyKey].bind(customElements);
  }
}) as CustomElementRegistry & {
 
  /**
   * Retrieve the name under which the supplied `constructor` was registered
   * with the {@link CustomElementRegistry}.
   *
   * @param constructor - The class `constructor` to get the name for.
   * @returns The name under which the `constructor` was registered, if.
   */
  getName(constructor: CustomElementConstructor): string | undefined;
 
};
 
export { registry as customElements };