Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions src/utils/storeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,35 @@ export function hasStateChanged<T>(prevState: T, updates: Partial<T>): boolean {
});
}

interface StoreSetStateJob<T> {
partial: Partial<T> | ((state: T) => Partial<T>);
force?: boolean;
}

/**
* A custom store creation utility
*/
export function createStore<T extends object>(initialState: T): Store<T> {
let state = { ...initialState };
const queue: StoreSetStateJob<T>[] = [];
const listeners = new Set<() => void>();
let isUpdating = false;

const setState = (partial: Partial<T> | ((state: T) => Partial<T>), force?: boolean) => {
// Prevent nested updates
if (isUpdating) return;

try {
isUpdating = true;
const nextState = typeof partial === 'function' ? partial(state) : partial;
const hasChanged = hasStateChanged(state, nextState);
const processQueue = () => {
const job = queue.shift();
if (!job) return;

if (force || hasChanged) {
state = { ...state, ...nextState };
listeners.forEach((listener) => listener());
}
} finally {
isUpdating = false;
const { partial, force } = job;
const nextState = typeof partial === 'function' ? partial(state) : partial;
const hasChanged = hasStateChanged(state, nextState);
if (force || hasChanged) {
state = { ...state, ...nextState };
listeners.forEach((listener) => listener());
}
};
const setState = (partial: Partial<T> | ((state: T) => Partial<T>), force?: boolean) => {
queue.push({ partial, force });
processQueue();
};

return {
getState: () => state,
Expand Down