TypeRegistry

View source

World resource that stores reflection metadata keyed by a stable type ID.

A registry is installed by ReflectPlugin. Use TypeRegistry#register when a runtime constructor represents the type, or TypeRegistry#registerTypeId for enums, function signatures, generic types, and other types that need an explicit ID. Each entry contains its TypeInfo and can also retain the constructor used during registration.

TypeRegistry#get and TypeRegistry#getByTypeId return a TypeEntry or undefined. Registering an ID replaces its existing entry; the matching unregister methods remove it.

Serialization produces an object keyed by type ID and stores each entry’s type-info serial form. Constructor functions are not serialized, so entries restored with TypeRegistry.deserialize contain metadata without a constructorFn unless a constructor is registered again.

Examples

import { App } from '@wimaengine/app'
import { OpaqueInfo, ReflectPlugin, TypeRegistry } from '@wimaengine/reflect'

class MainWorld {}
class SaveData {}

const app = new App()
app
  .setWorld(MainWorld)
  .defaultWorld(MainWorld)
  .registerPlugin(new ReflectPlugin())
  .setRunner(() => {})
  .run()

const registry = app.getWorld().getResource(TypeRegistry)
registry.register(SaveData, OpaqueInfo.default())
const entry = registry.get(SaveData)

Constructor


constructor()

Methods


register<T>(type: Constructor<T>, info: TypeInfo): void

View source

Registers metadata under the type ID derived from a runtime constructor. The resulting TypeEntry retains type as its constructorFn. Registering the same constructor again replaces its existing entry.

Type Parameters

T

Parameters

NameDescriptionDefault
type——
info——

registerTypeId(typeId: TypeId, info: TypeInfo, constructorFn?: ConstructorFn): void

View source

Registers metadata under an explicit type ID.

Use this for types without a runtime constructor, such as enums, or when the type ID is composed manually. A supplied constructor is retained on the resulting TypeEntry; registering an existing ID replaces its entry.

Parameters

NameDescriptionDefault
typeId——
info——
constructorFn?——

unregister<T>(type: Constructor<T>): void

View source

Removes the entry registered for a runtime constructor, if present.

Type Parameters

T

Parameters

NameDescriptionDefault
type——

unregisterTypeId(typeId: TypeId): void

View source

Removes the entry registered for an explicit type ID, if present.

Parameters

NameDescriptionDefault
typeId——

get<T>(type: Constructor<T>): TypeEntry

View source

Looks up the metadata entry for a runtime constructor.

Type Parameters

T

Parameters

NameDescriptionDefault
type——

Returns

The registered entry or undefined if absent.

getByTypeId(typeId: TypeId): TypeEntry

View source

Looks up the metadata entry for an explicit type ID.

Parameters

NameDescriptionDefault
typeId——

Returns

The registered entry or undefined if absent.

staticserialize(value: TypeRegistry): TypeRegistrySerial

View source

Serializes a registry’s type metadata into a type-ID keyed object. Constructor functions and runtime method registrations are not included; only each entry’s TypeInfo serial form is written.

Parameters

NameDescriptionDefault
value——

staticdeserialize(value: TypeRegistrySerial, out?: TypeRegistry): TypeRegistry

View source

Restores type metadata from a serialized registry.

out is cleared before the serialized entries are added. If it is not supplied, a new registry is created. Deserialized entries do not have constructor functions because constructors are not part of the serial format.

Parameters

NameDescriptionDefault
value——
out?—...

Throws

If an entry contains an unsupported type-info serial.

staticvalidateSerial(value: unknown): value is TypeRegistrySerial

View source

Checks whether a value has the supported serialized registry shape.

Parameters

NameDescriptionDefault
value——