TypeStore

View source

Registers component constructors and explicit TypeId values, mapping each type to a dense ComponentId and retaining its ComponentInfo metadata.

A World owns one store for its component types. Constructor-based methods derive a TypeId with typeid; TypeId-based methods accept that key directly. The first registration of a type receives the next zero-based component ID. Registering the same TypeId returns its existing ID and keeps its existing ComponentInfo.

Use the lookup methods to inspect registration and metadata. A returned ComponentInfo exposes its ComponentHooks through ComponentInfo#getHooks; normal hook configuration goes through World#setComponentHooks.

Examples

import { TypeStore } from '@wimaengine/ecs'

class Position {}
const store = new TypeStore()
const id = store.getOrSet(Position)

store.getId(Position) === id // true
store.get(Position)?.getHooks() // the component's ComponentHooks
store.getOrSet(Position) === id // true: registration is idempotent

Constructor


constructor()

Methods


set<T>(type: Constructor<T>): number

View source

Registers a component constructor and returns its component ID.

The constructor is converted to a TypeId before registration. If that TypeId is already registered, this method returns the existing ID and does not replace its ComponentInfo; otherwise it assigns the next zero-based ID and creates a metadata record.

Type Parameters

T

Parameters

NameDescriptionDefault
type

Component constructor to register.

—

Returns

The existing or newly assigned component ID.

setByTypeId(id: TypeId): number

View source

Registers a component TypeId and returns its component ID.

Registration is idempotent. An existing TypeId keeps its original zero-based ID and metadata; a new TypeId is appended to the metadata list and receives the list’s previous length.

Parameters

NameDescriptionDefault
id

Component type ID to register.

—

Returns

The existing or newly assigned component ID.

has<T>(type: Constructor<T>): boolean

View source

Checks whether a component constructor has been registered.

This is a lookup only: it derives the constructor’s TypeId and does not add an entry when the type is missing.

Type Parameters

T

Parameters

NameDescriptionDefault
type

Component constructor to look up.

—

Returns

Whether the constructor’s TypeId is registered.

hasId(id: number): boolean

View source

Checks whether a component ID is within the store’s current ID range.

The check uses only the numeric bounds 0 <= id < getInfos().length; it does not read or validate the entry at that index.

Parameters

NameDescriptionDefault
id

Component ID to check.

—

Returns

Whether id is in the current component-ID range.

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

View source

Looks up ComponentInfo metadata by a component constructor.

The constructor is converted to its TypeId. Missing types are not registered and return undefined.

Type Parameters

T

Parameters

NameDescriptionDefault
type

Component constructor to look up.

—

Returns

The metadata record, if registered.

getId<T>(type: Constructor<T>): number

View source

Looks up a component ID by constructor without registering the type.

Type Parameters

T

Parameters

NameDescriptionDefault
type

Component constructor to look up.

—

Returns

The ID, or undefined if it is missing.

getIdByTypeId(id: TypeId): number

View source

Looks up a component ID by explicit TypeId without registering the type.

Parameters

NameDescriptionDefault
id

Component type ID to look up.

—

Returns

The ID, or undefined if it is missing.

getOrSet<T>(component: Constructor<T>): number

View source

Returns the existing component ID or registers a constructor when missing.

The constructor is converted to a TypeId. This is equivalent to calling TypeStore#set; both preserve an existing registration.

Type Parameters

T

Parameters

NameDescriptionDefault
component

Component constructor to retrieve or register.

—

Returns

The existing or newly assigned component ID.

getOrSetByTypeId(typeId: TypeId): number

View source

Returns the existing component ID or registers a TypeId when missing.

A missing TypeId is registered with TypeStore#setByTypeId; an existing TypeId keeps its current ID and metadata.

Parameters

NameDescriptionDefault
typeId

Component type ID to retrieve or register.

—

Returns

The existing or newly assigned component ID.