[API reference](https://jsprismarine.dev/api/index.md) / [@jsprismarine/entities](https://jsprismarine.dev/api/@jsprismarine/entities/index.md) / ComponentStates

# Class: ComponentStates

Defined in: [entities/src/componentState.ts:222](https://github.com/JSPrismarine/JSPrismarine/blob/9db8dc5e83d04708d63ff9d73cae14f6c6b880f6/packages/entities/src/componentState.ts#L222)

An entity's component state, keyed by component name.

Components use [ComponentStates.get](#get), [ComponentStates.set](#set) and
[ComponentStates.clear](#clear) with their declared key. A host uses
[ComponentStates.entries](#entries), [ComponentStates.snapshot](#snapshot),
[ComponentStates.restore](#restore) and [ComponentStates.write](#write). All of them write through
[ComponentStates.write](#write), which reports each change to the owner.

Every value is frozen when it is written. A component that changes its state writes a new
value; it never edits the one it read.

## Constructors

### Constructor

> **new ComponentStates**(`owner`): `ComponentStates`

Defined in: [entities/src/componentState.ts:226](https://github.com/JSPrismarine/JSPrismarine/blob/9db8dc5e83d04708d63ff9d73cae14f6c6b880f6/packages/entities/src/componentState.ts#L226)

#### Parameters

##### owner

[`ComponentStatesOwner`](https://jsprismarine.dev/api/@jsprismarine/entities/index.md#componentstatesowner)

#### Returns

`ComponentStates`

## Methods

### clear()

> **clear**\<`S`\>(`key`): `void`

Defined in: [entities/src/componentState.ts:257](https://github.com/JSPrismarine/JSPrismarine/blob/9db8dc5e83d04708d63ff9d73cae14f6c6b880f6/packages/entities/src/componentState.ts#L257)

Removes a component's state, which is what a finished timer leaves.

#### Type Parameters

##### S

`S` *extends* [`ComponentStateRecord`](https://jsprismarine.dev/api/@jsprismarine/entities/index.md#componentstaterecord)

#### Parameters

##### key

[`ComponentStateKey`](https://jsprismarine.dev/api/@jsprismarine/entities/index.md#componentstatekey)\<`S`\>

#### Returns

`void`

#### Throws

When another declaration already claimed the component name.

***

### entries()

> **entries**(): `IterableIterator`\<\[`component`: `string`, `state`: [`ComponentStateRecord`](https://jsprismarine.dev/api/@jsprismarine/entities/index.md#componentstaterecord)\]\>

Defined in: [entities/src/componentState.ts:285](https://github.com/JSPrismarine/JSPrismarine/blob/9db8dc5e83d04708d63ff9d73cae14f6c6b880f6/packages/entities/src/componentState.ts#L285)

Every component that holds state now, with that state, in the order each was first written.

#### Returns

`IterableIterator`\<\[`component`: `string`, `state`: [`ComponentStateRecord`](https://jsprismarine.dev/api/@jsprismarine/entities/index.md#componentstaterecord)\]\>

***

### get()

> **get**\<`S`\>(`key`): `S` \| `undefined`

Defined in: [entities/src/componentState.ts:236](https://github.com/JSPrismarine/JSPrismarine/blob/9db8dc5e83d04708d63ff9d73cae14f6c6b880f6/packages/entities/src/componentState.ts#L236)

A component's state, or undefined when it holds none.

A stored value that fails the key's guard reads as undefined, so a component never sees a
shape it did not declare.

#### Type Parameters

##### S

`S` *extends* [`ComponentStateRecord`](https://jsprismarine.dev/api/@jsprismarine/entities/index.md#componentstaterecord)

#### Parameters

##### key

[`ComponentStateKey`](https://jsprismarine.dev/api/@jsprismarine/entities/index.md#componentstatekey)\<`S`\>

#### Returns

`S` \| `undefined`

#### Throws

When another declaration already claimed the component name.

***

### restore()

> **restore**(`saved`): readonly `string`[]

Defined in: [entities/src/componentState.ts:314](https://github.com/JSPrismarine/JSPrismarine/blob/9db8dc5e83d04708d63ff9d73cae14f6c6b880f6/packages/entities/src/componentState.ts#L314)

Writes a saved form back after the entity's components have initialized.

Only active components are written. A component the form leaves out keeps the state its
`onInitialize` set, and a value that is not plain data, or fails the component's declared
shape, is skipped in the same way. State declared with `saved: false` is never restored,
so a form written before the declaration said so loads as if it left that state out.

#### Parameters

##### saved

`Readonly`\<`Record`\<`string`, `unknown`\>\>

A saved form, possibly parsed from an older or damaged record.

#### Returns

readonly `string`[]

The names of the components whose saved value was skipped as malformed.

***

### set()

> **set**\<`S`\>(`key`, `value`): `void`

Defined in: [entities/src/componentState.ts:247](https://github.com/JSPrismarine/JSPrismarine/blob/9db8dc5e83d04708d63ff9d73cae14f6c6b880f6/packages/entities/src/componentState.ts#L247)

Replaces a component's state.

#### Type Parameters

##### S

`S` *extends* [`ComponentStateRecord`](https://jsprismarine.dev/api/@jsprismarine/entities/index.md#componentstaterecord)

#### Parameters

##### key

[`ComponentStateKey`](https://jsprismarine.dev/api/@jsprismarine/entities/index.md#componentstatekey)\<`S`\>

##### value

`NoInfer`\<`S`\>

#### Returns

`void`

#### Throws

When another declaration already claimed the component name.

***

### snapshot()

> **snapshot**(): [`ComponentStateSnapshot`](https://jsprismarine.dev/api/@jsprismarine/entities/index.md#componentstatesnapshot)

Defined in: [entities/src/componentState.ts:294](https://github.com/JSPrismarine/JSPrismarine/blob/9db8dc5e83d04708d63ff9d73cae14f6c6b880f6/packages/entities/src/componentState.ts#L294)

The saved form: each active component that has declared saved state, sorted by name, so
two entities in the same state save the same form. State declared with `saved: false` is
left out.

#### Returns

[`ComponentStateSnapshot`](https://jsprismarine.dev/api/@jsprismarine/entities/index.md#componentstatesnapshot)

***

### write()

> **write**(`component`, `value`): `boolean`

Defined in: [entities/src/componentState.ts:272](https://github.com/JSPrismarine/JSPrismarine/blob/9db8dc5e83d04708d63ff9d73cae14f6c6b880f6/packages/entities/src/componentState.ts#L272)

The one write path. Every change to the slot, whether from a component, a restore or a
host's undo, comes through here and is reported to the owner with its before and after
values. A write of a value equal, field by field, to the one held changes nothing and
reports nothing, so a component may write its state every tick without filling a log.

#### Parameters

##### component

`string`

The component whose state is written.

##### value

[`ComponentStateRecord`](https://jsprismarine.dev/api/@jsprismarine/entities/index.md#componentstaterecord) \| `null`

The new state, or `null` to remove it.

#### Returns

`boolean`

False, and nothing written, when the component declared a shape the value fails.
