ilokesto

API Reference

The public surface of the Store class.

The @ilokesto/store package exports a single class: Store<T>.

Constructor

new Store<T>(initialState: T)

Creates a new store instance with the provided initial state.

const store = new Store({ count: 0 });
  • initialState: The starting value for the store.

Methods

store.getState(): Readonly<T>

Returns the current state of the store.

const state = store.getState();

The returned state is typed as Readonly<T> to discourage direct mutation. See Semantics for more on the meaning of Readonly<T>.

store.getInitialState(): Readonly<T>

Returns the state originally passed to the constructor. This is useful for resetting the store to its original value.

const initial = store.getInitialState();

store.setState(nextState: T | ((prevState: Readonly<T>) => T)): void

Updates the store state. It accepts either a new state value or an updater function.

// Replacement
store.setState({ count: 1 });

// Updater function
store.setState((prev) => ({ ...prev, count: prev.count + 1 }));

For detailed behavior on updates, including bailout rules and immutable patterns, see Update Semantics.

store.subscribe(listener: () => void): () => void

Registers a callback that will be executed synchronously whenever the state changes.

const unsubscribe = store.subscribe(() => {
  console.log("New state:", store.getState());
});

// To stop listening:
unsubscribe();

See Subscriptions for more on the subscription lifecycle and notification guarantees.


Public Export Summary

The package exposes one public class, Store<T>, with the constructor and methods documented above.

Current source code also contains middleware-related internals, but they are documented in Implementation Notes rather than treated as the main public contract here.

On this page