Skip to content

1681-engine / ui / Store

Interface: Store<T> ​

Defined in: src/ui/dom/createStore.ts:8

A tiny shared state store between the game and its UI, framework-free. Same shape as the engine's settings store and as zustand's vanilla stores (getState, setState, subscribe), so React reads it with useSyncExternalStore(store.subscribe, store.getState).

Type Parameters ​

Type Parameter
T extends object

Methods ​

getState() ​

ts
getState(): T;

Defined in: src/ui/dom/createStore.ts:9

Returns ​

T


reset() ​

ts
reset(): void;

Defined in: src/ui/dom/createStore.ts:20

Back to the state it was made with.

Returns ​

void


select() ​

ts
select<S>(
   select, 
   listener, 
   equals?
): () => void;

Defined in: src/ui/dom/createStore.ts:18

Calls listener only when select(state) changes (by equals, default Object.is).

Type Parameters ​

Type Parameter
S

Parameters ​

ParameterType
select(state) => S
listener(value, previous) => void
equals?(a, b) => boolean

Returns ​

() => void


setState() ​

ts
setState(patch): void;

Defined in: src/ui/dom/createStore.ts:14

Merges a patch (or what a function of the state returns) into a new state object. A patch that changes nothing notifies nobody: a game may set the player's health every frame and the UI renders only when it moved.

Parameters ​

ParameterType
patchPartial<T> | ((state) => Partial<T>)

Returns ​

void


subscribe() ​

ts
subscribe(listener): () => void;

Defined in: src/ui/dom/createStore.ts:16

Calls listener after every change; the returned function unsubscribes.

Parameters ​

ParameterType
listenerStoreListener<T>

Returns ​

() => void

Released under the MIT License.