npm.io
7.1.4 • Published 5 months ago

@travetto/registry

Licence
MIT
Version
7.1.4
Deps
1
Size
11 kB
Vulns
0
Weekly
0
Stars
23

Registry

Patterns and utilities for handling registration of metadata and functionality for run-time use

Install: @travetto/registry

npm install @travetto/registry

# or

yarn add @travetto/registry

This module is the backbone for all "discovered" and "registered" behaviors within the framework. This is primarily used for building modules within the framework and not directly useful for application development.

Flows

Registration, within the framework flows throw two main use cases:

Initial Flows

The primary flow occurs on initialization of the application. At that point, the module will:

  1. Initialize Registry and will automatically register/load all relevant files
  2. As files are imported, decorators within the files will record various metadata relevant to the respective registries
  3. When all files are processed, the Registry is finished, and it will signal to anything waiting on registered data that its free to use it.

This flow ensures all files are loaded and processed before application starts. A sample registry could like:

Code: Sample Registry

import type { Class } from '@travetto/runtime';
import { type RegistryAdapter, type RegistryIndex, RegistryIndexStore, Registry } from '@travetto/registry';

interface Group {
  class: Class;
  name: string;
  children: Child[];
}

interface Child {
  name: string;
  method: Function;
}

/**
 * The adapter to handle mapping/modeling a specific class
 */
class SampleRegistryAdapter implements RegistryAdapter<Group> {

  #class: Class;
  #config: Group;

  constructor(cls: Class) {
    this.#class = cls;
  }

  register(...groups: Partial<Partial<Group>>[]): Group {
    for (const group of groups) {
      Object.assign(this.#config, {
        ...group,
        children: [
          ...(this.#config?.children ?? []),
          ...(group.children ?? [])
        ]
      });
    }
    return this.#config;
  }

  registerChild(method: Function, name: string): void {
    this.register({ children: [{ method, name }] });
  }

  finalize?(parent?: Partial<Group> | undefined): void {
    // Nothing to do
  }

  get(): Group {
    return this.#config;
  }
}

/**
 * Basic Index that handles cross-class activity
 */
export class SampleRegistryIndex implements RegistryIndex {
  static #instance = Registry.registerIndex(SampleRegistryIndex);

  static getForRegister(cls: Class, allowFinalized = false): SampleRegistryAdapter {
    return this.#instance.store.getForRegister(cls, allowFinalized);
  }

  store = new RegistryIndexStore(SampleRegistryAdapter);

  onCreate(cls: Class): void {
    // Nothing to do
  }
}

The registry index is a RegistryIndex that similar to the Schema's Schema registry and Dependency Injection's Dependency registry.

Live Flow

At runtime, the framework is designed to listen for changes and restart any running processes as needed.