UNPKG

@mantine/store

Version:

A library to sync external React state updates

37 lines (36 loc) 897 B
"use client"; import { useSyncExternalStore } from "react"; //#region packages/@mantine/store/src/store.ts function createStore(initialState) { let state = initialState; let initialized = false; const listeners = /* @__PURE__ */ new Set(); return { getState() { return state; }, updateState(value) { state = typeof value === "function" ? value(state) : value; }, setState(value) { this.updateState(value); listeners.forEach((listener) => listener(state)); }, initialize(value) { if (!initialized) { state = value; initialized = true; } }, subscribe(callback) { listeners.add(callback); return () => listeners.delete(callback); } }; } function useStore(store) { return useSyncExternalStore(store.subscribe, () => store.getState(), () => store.getState()); } //#endregion export { createStore, useStore }; //# sourceMappingURL=store.mjs.map