Class
TypeRegistry
View sourceWorld 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
Methods
register<T>(type: Constructor<T>, info: TypeInfo): void
View sourceRegisters 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.
registerTypeId(typeId: TypeId, info: TypeInfo, constructorFn?: ConstructorFn): void
View sourceRegisters 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.
unregister<T>(type: Constructor<T>): void
View sourceRemoves the entry registered for a runtime constructor, if present.
unregisterTypeId(typeId: TypeId): void
View sourceRemoves the entry registered for an explicit type ID, if present.
get<T>(type: Constructor<T>): TypeEntry
View sourceLooks up the metadata entry for a runtime constructor.
getByTypeId(typeId: TypeId): TypeEntry
View sourceLooks up the metadata entry for an explicit type ID.
staticserialize(value: TypeRegistry): TypeRegistrySerial
View sourceSerializes 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.
staticdeserialize(value: TypeRegistrySerial, out?: TypeRegistry): TypeRegistry
View sourceRestores 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.
staticvalidateSerial(value: unknown): value is TypeRegistrySerial
View sourceChecks whether a value has the supported serialized registry shape.
