@travetto/registry
Version:
Patterns and utilities for handling registration of metadata and functionality for run-time use
105 lines (80 loc) • 3.65 kB
Markdown
<!-- This file was generated by /doc and should not be modified directly -->
<!-- Please modify https://github.com/travetto/travetto/tree/main/module/registry/DOC.tsx and execute "npx trv doc" to rebuild -->
# Registry
## Patterns and utilities for handling registration of metadata and functionality for run-time use
**Install: /registry**
```bash
npm install /registry
# or
yarn add /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](https://github.com/travetto/travetto/tree/main/module/registry/src/registry.ts#L5) and will automatically register/load all relevant files
1. As files are imported, decorators within the files will record various metadata relevant to the respective registries
1. When all files are processed, the [Registry](https://github.com/travetto/travetto/tree/main/module/registry/src/registry.ts#L5) 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**
```typescript
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](https://github.com/travetto/travetto/tree/main/module/registry/src/types.ts#L32) that similar to the [Schema](https://github.com/travetto/travetto/tree/main/module/schema#readme "Data type registry for runtime validation, reflection and binding.")'s Schema registry and [Dependency Injection](https://github.com/travetto/travetto/tree/main/module/di#readme "Dependency registration/management and injection support.")'s Dependency registry.
### Live Flow
At runtime, the framework is designed to listen for changes and restart any running processes as needed.