Store class
Store<T> is the only public class exported by @ilokesto/store.
import { Store } from "@ilokesto/store";
const store = new Store({ ready: false });Constructor
new Store<T>(initialState: T)The constructor saves initialState as both the first current state and the value returned by getInitialState().
No clone is made. If you pass an object, keep ownership discipline: treat the object as immutable after giving it to the store.
getState()
getState(): Readonly<T>Returns the current state synchronously. The Readonly<T> type discourages direct assignment through the returned value, but it does not create a runtime freeze and it is shallow from TypeScript's point of view.
const state = store.getState();
console.log(state.ready);Use getState():
- before deriving an immediate value,
- inside a
subscribe()listener, - inside adapter code that needs to render the latest snapshot.
Do not use it as a reason to mutate state in place.
getInitialState()
getInitialState(): Readonly<T>Returns the exact initial value captured by the constructor.
const initial = store.getInitialState();getInitialState() does not reset the store. To reset, pass the initial value back through setState() yourself:
store.setState(store.getInitialState());If the current state is already the same reference as the initial state, Object.is prevents notification. If you need subscribers to react to a reset of object state, pass a fresh object with the same fields.
Public API summary
Store<T>: owns one state value.getState(): reads the current value.getInitialState(): reads the constructor value.setState(): replaces the value after middleware.subscribe(): registers a listener and returns cleanup.pushMiddleware()/unshiftMiddleware(): compose update middleware.