TypeEntry

View source

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.

Examples

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


constructor(info: TypeInfo, constructorFn?: ConstructorFn)

View source

Creates an entry around type metadata and an optional runtime constructor.

Parameters

NameDescriptionDefault
info——
constructorFn?——

Properties


info: TypeInfo

View source

Reflection metadata associated with this entry. The reference is retained as supplied; it is not copied when the entry is constructed.

constructorFn: ConstructorFn

View source

Runtime constructor associated with the type, when one is available. Explicit type-ID registrations and deserialized entries may leave this value undefined.

Methods


serialize(): TypeInfoSerial

View source

Serializes 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.

call<T extends unknown[]>(name: string, args?: [...T[]]): unknown

View source

Invokes 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.

Type Parameters

T extends unknown[]

Parameters

NameDescriptionDefault
name——
args?

Arguments passed to the registered function.

—

getMethod(name: string): MethodEntry

View source

Returns 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.

Parameters

NameDescriptionDefault
name——

setMethod(method: Function): void

View source

Registers 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.

Parameters

NameDescriptionDefault
method——

getMethods(): Map<string, MethodEntry>

View source

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): TypeEntry

View source

Registers 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.

Type Parameters

T extends object

Parameters

NameDescriptionDefault
typeId——
attribute——

setAttribute<T extends object>(attribute: T): TypeEntry

View source

Registers 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.

Type Parameters

T extends object

Parameters

NameDescriptionDefault
attribute——

getAttributeByTypeId<T extends object>(typeId: TypeId): T

View source

Returns an attribute by explicit type ID.

Type Parameters

T extends object

Parameters

NameDescriptionDefault
typeId——

getAttribute<T extends object>(type: Constructor<T>): T

View source

Returns an attribute by its runtime constructor.

Type Parameters

T extends object

Parameters

NameDescriptionDefault
type——

hasAttributeByTypeId(typeId: TypeId): boolean

View source

Checks whether an attribute is registered under an explicit type ID.

Parameters

NameDescriptionDefault
typeId——

hasAttribute(type: ConstructorFn): boolean

View source

Checks whether an attribute is registered for a runtime constructor.

Parameters

NameDescriptionDefault
type——

removeAttributeByTypeId(typeId: TypeId): boolean

View source

Removes an attribute registered under an explicit type ID.

Parameters

NameDescriptionDefault
typeId——

Returns

Whether an attribute was removed.

removeAttribute(type: ConstructorFn): boolean

View source

Removes an attribute registered for a runtime constructor.

Parameters

NameDescriptionDefault
type——

Returns

Whether an attribute was removed.