Files
healer-man/scripts/arena-pipeline/runtime-store.ts
T
2026-09-11 14:26:15 -04:00

19 lines
875 B
TypeScript

// Same vanilla store contract used by Zustand, without a React dependency on
// the dedicated server. Each arena operation restores its own saved state.
export function create<T>(initialize: (set: any, get: () => T, api: any) => T) {
let state: T;
const listeners = new Set<(state: T, previous: T) => void>();
const api = {
getState: () => state,
setState(update: any, replace = false) {
const previous = state;
const patch = typeof update === "function" ? update(state) : update;
state = replace ? patch : Object.assign({}, state, patch);
if (state !== previous) for (const listener of listeners) listener(state, previous);
},
subscribe(listener: (state: T, previous: T) => void) { listeners.add(listener); return () => listeners.delete(listener); },
};
state = initialize(api.setState, api.getState, api);
return api;
}