Tables

View source

Registers Table instances under allocated TableId values.

Tables assigns zero-based IDs in insertion order. Use Tables#resolveTableFor to reuse a table with an exact component TypeId set, or Tables#createTable to always append a new table. Matching table IDs can be collected with Tables#getTableIds.

Examples

import { Tables } from '@wimaengine/ecs'
import { typeid } from '@wimaengine/type'

class Position {}
const tables = new Tables()
const position = typeid(Position)
const [id, table] = tables.resolveTableFor([position])
tables.get(id) === table // true
tables.getTableIds([position], []) // [id]

Constructor


constructor()

Methods


createTable(typeIds: TypeId[]): [TableId, Table]

View source

Appends a new empty table and assigns its next table ID.

The ID is the current list length, so IDs are zero-based and follow allocation order. This method always creates a new Table; it does not search for an existing layout. Use Tables#resolveTableFor when an exact layout should be reused.

Parameters

NameDescriptionDefault
typeIds

Component TypeIds for the new table’s columns.

—

Returns

The new table’s ID and table instance.

get(id: TableId): Table

View source

Returns the table stored at an allocated ID.

IDs are direct indexes into the registry. An unallocated or cleared ID therefore returns undefined; this lookup does not create a table or validate the table’s contents.

Parameters

NameDescriptionDefault
id

Zero-based table ID to look up.

—

Returns

The table at id, or undefined when none exists.

values(): readonly Table[]

View source

Returns the live, ID-ordered table collection.

The returned value is the registry’s backing array, typed as read-only. Tables appended by Tables#createTable are visible through the same reference. The Tables#clear method replaces the backing array, so a reference obtained before clearing remains a snapshot of the old array and does not receive later tables.

Returns

The live table collection.

resolveTableFor(ids: TypeId[]): [TableId, Table]

View source

Finds or creates the table for an exact component TypeId layout.

The search follows table-ID order and delegates exact matching to Table#hasOnly: the input and table column sets must have the same length and every input ID must be present, regardless of order. Pass one occurrence of each TypeId. The first exact match is returned; when none exists, this method delegates to Tables#createTable and returns the newly allocated pair.

Parameters

NameDescriptionDefault
ids

Complete, deduplicated TypeId set for the layout.

—

Returns

The existing or newly created table ID and table.

getTableIds(comps: TypeId[], filtered: TableId[]): TableId[]

View source

Appends IDs of tables containing every requested component TypeId.

This is subset filtering: a table may contain additional columns. The registry is scanned in ID order, matching IDs are appended to the supplied filtered array, and that same array is returned. Existing entries in filtered are preserved; this method does not clear or deduplicate it and does not inspect table rows.

Parameters

NameDescriptionDefault
comps

Component TypeIds that every selected table must contain.

—
filtered

Accumulator to which matching IDs are appended.

—

Returns

The same filtered array after matching IDs are appended.

clear(): void

View source

Removes all tables from the registry.

Clearing replaces the backing list with a new empty array. Future tables therefore start again at table ID 0; table objects or arrays returned before the clear are not mutated, but they are no longer registered here.