constructor(info: TypeInfo, constructorFn?: ConstructorFn)
View sourceCreates an entry around type metadata and an optional runtime constructor.
Class
Registry entry that groups reflection metadata and runtime method hooks for one registered type.
TypeRegistry creates one entry for each registered type ID. The
entry keeps the exact TypeInfo instance supplied to its
constructor; it does not clone the metadata. constructorFn is optional
because entries registered by an explicit type ID do not need a runtime
constructor, and deserialized entries never restore one.
Methods are process-local hooks keyed by each function’s name. Register a
hook with TypeEntry#setMethod, inspect one with
TypeEntry#getMethod, or invoke one with TypeEntry#call. A
missing method is not an error: getMethod returns undefined and call
also returns undefined without invoking a function. Methods and
constructorFn are not included by TypeEntry#serialize;
serialization only delegates to the stored TypeInfo. Attributes
are process-local metadata as well and are not included in the type-info
serial form.
import { OpaqueInfo, TypeEntry } from '@wimaengine/reflect'
class SaveData {}
const info = OpaqueInfo.default()
const entry = new TypeEntry(info, SaveData)
entry.setMethod(function describe(value) {
return value.constructor.name
})
entry.info === info // true: the metadata reference is retained
entry.call('describe', [new SaveData()]) // 'SaveData'
entry.getMethod('missing') // undefined
entry.call('missing', []) // undefined
entry.serialize() // { kind: 'opaque' }constructor(info: TypeInfo, constructorFn?: ConstructorFn)Creates an entry around type metadata and an optional runtime constructor.
Reflection metadata associated with this entry. The reference is retained as supplied; it is not copied when the entry is constructed.
Runtime constructor associated with the type, when one is available.
Explicit type-ID registrations and deserialized entries may leave this
value undefined.
serialize(): TypeInfoSerialSerializes only the stored type metadata.
Runtime constructors and methods are process-local references, so they are
omitted from the result. The returned value is the result of calling
TypeInfo.serialize on info.
serialize(value: TypeInfo): TypeInfoSerialdeserialize(value: TypeInfoSerial, out?: TypeInfo): TypeInfovalidateSerial(value: unknown): value is TypeInfoSerialcall<T extends unknown[]>(name: string, args?: [...T[]]): unknownInvokes a registered method by its exact name.
args is spread into the registered function in order. If no method is
registered under name, nothing is invoked and undefined is returned.
A registered method that returns undefined has the same return value, so
use TypeEntry#getMethod to distinguish a missing method
from a method that returned undefined.
getMethod(name: string): MethodEntryReturns the method registered under an exact function name, if any.
The lookup does not derive a name from an alias or constructor; it uses the
same Function.name value that TypeEntry#setMethod used
when the method was registered.
setMethod(method: Function): voidRegisters a function under its name property.
Registering another function with the same name replaces the previous
method. The function reference is wrapped in a MethodEntry for
later calls; it is not copied or serialized.
getMethods(): Map<string, MethodEntry>Returns the live map of registered methods.
Keys are the functions’ name values and values are the corresponding
MethodEntry wrappers. The returned map is the entry’s actual map,
so changes made through it affect subsequent lookups and calls.
setAttributeByTypeId<T extends object>(typeId: TypeId, attribute: T): TypeEntryRegisters an attribute under an explicit type ID.
Registering another value under the same type ID replaces the previous attribute. The value is retained by reference and is not cloned.
setAttribute<T extends object>(attribute: T): TypeEntryRegisters an attribute using the runtime constructor’s type ID.
Attribute values are non-null objects with a runtime constructor.
Plain objects are therefore keyed as Object; use
TypeEntry#setAttributeByTypeId when an explicit attribute type
ID is required.
getAttributeByTypeId<T extends object>(typeId: TypeId): TReturns an attribute by explicit type ID.
getAttribute<T extends object>(type: Constructor<T>): TReturns an attribute by its runtime constructor.
hasAttributeByTypeId(typeId: TypeId): booleanChecks whether an attribute is registered under an explicit type ID.
hasAttribute(type: ConstructorFn): booleanChecks whether an attribute is registered for a runtime constructor.
removeAttributeByTypeId(typeId: TypeId): booleanRemoves an attribute registered under an explicit type ID.
removeAttribute(type: ConstructorFn): booleanRemoves an attribute registered for a runtime constructor.