All files / shell/src/queue resolve.ts

100% Statements 233/233
96.42% Branches 27/28
100% Functions 6/6
100% Lines 233/233

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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 2341x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 2x 5x 5x 5x 3x 3x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 2x 2x 2x 2x 2x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 17x 17x 17x 10x 17x 17x 17x 17x 7x 17x 17x 17x 17x 17x 17x 17x 17x 17x 10x 10x 10x 10x 5x 5x 5x 5x 7x 7x 5x 5x 5x 5x 5x 10x 17x 9x 9x 9x 5x 5x 5x 5x 6x 1x 6x 6x 5x 4x 4x 4x 4x 9x 9x 1x 1x  
import { Linker, Provider, Singleton } from '@sgrud/core';
import { Observable, ObservableInput, defer, finalize, forkJoin, from, switchMap, tap } from 'rxjs';
import { Component, component } from '../component/component';
import { customElements } from '../component/registry';
import { Router } from '../router/router';
import { Queue } from './queue';
 
/**
 * The **Resolve** type alias is used and intended to be used in conjunction
 * with the {@link ResolveQueue} {@link Queue} and the {@link Resolve}
 * decorator. The **Resolve** type alias represents a function that will be
 * called with the respective {@link Router.Segment} and {@link Router.State}.
 *
 * @typeParam S - The {@link Route} path string type.
 *
 * @see {@link Resolve}
 */
export type Resolve<S extends string> = (
 
  /**
   * The {@link Router.Segment} of the {@link Resolve}d {@link Component}.
   */
  segment: Router.Segment<S>,
 
  /**
   * The {@link Router.State} of the {@link Resolve}d {@link Component}.
   */
  state: Router.State<S>
 
) => ObservableInput<unknown>;
 
/**
 * {@link Component} prototype property decorator factory. Applying the
 * **Resolve** decorator to a property of a {@link Component}, while supplying
 * an {@link ObservableInput} to be `resolve`d, will replace the decorated
 * property with a getter returning the **Resolve**d value the supplied
 * {@link ObservableInput} `resolve`s to. To do so the **Resolve** decorator
 * relies on the built-in {@link ResolveQueue}.
 *
 * @param resolve - An {@link ObservableInput} to `resolve`.
 * @typeParam S - The {@link Route} path string type.
 * @returns A {@link Component} prototype property decorator.
 *
 * @example
 * **Resolve** the {@link Router.Segment} and {@link Router.State}:
 * ```tsx
 * import { Component, Resolve } from '@sgrud/shell';
 * import { of } from 'rxjs';
 *
 * declare global {
 *   interface HTMLElementTagNameMap {
 *     'example-component': ExampleComponent;
 *   }
 * }
 *
 * ⁠@Component('example-component')
 * export class ExampleComponent extends HTMLElement implements Component {
 *
 *   ⁠@Resolve((segment, state) => of([segment.route.path, state.search]))
 *   public readonly resolved!: [string, string];
 *
 *   public get template(): JSX.Element {
 *     return <span>Resolved: {this.resolved.join('?')}</span>;
 *   }
 *
 * }
 * ```
 *
 * @see {@link ResolveQueue}
 */
export function Resolve<S extends string>(resolve: Resolve<S>) {
 
  /**
   * @param prototype - The {@link Component} `prototype` to be decorated.
   * @param propertyKey - The {@link Component} property to be decorated.
   */
  return function(prototype: Component, propertyKey: PropertyKey): void {
    const linker = new Linker();
    const queued = new ResolveQueue();
    let required = queued.required.get(prototype.constructor);
 
    if (!linker.has(ResolveQueue)) {
      linker.set(ResolveQueue, queued);
    }
 
    if (!required) {
      required = new Map<PropertyKey, Resolve<string>>();
      queued.required.set(prototype.constructor, required);
    }
 
    required.set(propertyKey, resolve as unknown as Resolve<string>);
 
    Object.defineProperty(prototype, propertyKey, {
      enumerable: true,
      get(this: Component) {
        return queued.resolved.get(prototype.constructor)?.[propertyKey];
      },
      set(this: Component, value: unknown): void {
        if (this.isConnected) {
          let resolved = queued.resolved.get(prototype.constructor);
 
          if (!resolved) {
            queued.resolved.set(prototype.constructor, resolved = {});
          }
 
          resolved[propertyKey] = value;
        }
      }
    });
  };
 
}
 
/**
 * This built-in **ResolveQueue** extension of the {@link Queue} base class
 * intercepts all navigational events of the {@link Router} to {@link Resolve}
 * {@link ObservableInput}s before invoking subsequent {@link Queue}s. Thereby
 * this **ResolveQueue** allows asynchronous evaluations to be executed and
 * their {@link Resolve}d values to be provided to a {@link Component}, before
 * it is rendered into a {@link Document} for the first time. When the
 * {@link Catch} decorator is applied at least once this **ResolveQueue** will
 * be automatically provided as {@link Queue} to the {@link Linker}.
 *
 * @decorator {@link Singleton}
 *
 * @see {@link Queue}
 */
@Singleton()
export class ResolveQueue
  extends Provider<typeof Queue>('sgrud.shell.Queue') {
 
  /**
   * {@link Map}ping of all decorated {@link Component}s to a {@link Map} of
   * property keys and their **required** {@link Resolve}rs.
   */
  public readonly required: Map<Function, Map<PropertyKey, Resolve<string>>>;
 
  /**
   * {@link Map}ping of all decorated {@link Component}s to an object consisting
   * of property keys and their corresponding {@link Resolve}d return values.
   */
  public readonly resolved: Map<Function, Record<PropertyKey, unknown>>;
 
  /**
   * Public {@link Singleton} **constructor**. Called by the {@link Resolve}
   * decorator to link this {@link Queue} into the {@link Router} and to access
   * the {@link required} and {@link resolved} properties.
   */
  public constructor() {
    super();
 
    this.required = new Map<Function, Map<PropertyKey, Resolve<string>>>();
    this.resolved = new Map<Function, Record<PropertyKey, unknown>>();
  }
 
  /**
   * Overridden **handle** method of the {@link Queue} base class. Iterates all
   * {@link Router.Segment}s of the `next` {@link Router.State} and collects all
   * {@link Resolve}rs for any encountered {@link Component}s in those iterated
   * {@link Router.Segment}s. The collected {@link Resolve}rs are run before
   * invoking the subsequent {@link Queue}.
   *
   * @param _prev - The `_prev`iously active {@link Router.State} (ignored).
   * @param next - The `next` {@link Router.State} {@link Router.navigate}d to.
   * @param queue - The next {@link Queue} to **handle** the navigation.
   * @returns An {@link Observable} of the **handle**d {@link Router.State}.
   */
  public override handle(
    _prev: Router.State,
    next: Router.State,
    queue: Router.Queue
  ): Observable<Router.State> {
    return defer(() => {
      const components = [] as Function[];
      const resolvers = [];
      let segment = next.segment;
 
      do {
        const elements = [];
 
        if (segment.route.component) {
          elements.push(segment.route.component);
        }
 
        if (segment.route.slots) {
          for (const key in segment.route.slots) {
            elements.push(segment.route.slots[key]);
          }
        }
 
        for (const element of elements) {
          let constructor = customElements.get(element) as (
            CustomElementConstructor & { [component]?: new () => Component }
          ) | undefined;
 
          if (constructor) {
            constructor = constructor[component] || constructor;
            const required = this.required.get(constructor);
 
            if (required?.size) {
              components.push(constructor);
              const require = {} as Record<PropertyKey, Observable<unknown>>;
 
              for (const [key, handler] of required) {
                require[key] = from(handler(segment, next));
              }
 
              resolvers.push(forkJoin(require).pipe(
                tap((resolved) => this.resolved.set(constructor!, resolved))
              ));
            }
          }
        }
      } while (segment = segment.child!);
 
      if (resolvers.length) {
        return forkJoin(resolvers).pipe(
          switchMap(() => queue.handle(next)),
          finalize(() => {
            for (const [key] of this.resolved) {
              if (!components.includes(key)) {
                this.resolved.delete(key);
              }
            }
          })
        );
      }
 
      return queue.handle(next);
    });
  }
 
}