Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
imarabinda committed Jan 5, 2025
1 parent c47ff8c commit d045469
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
20 changes: 15 additions & 5 deletions packages/zustand-x/src/createStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,21 @@ import { storeFactory } from './utils/storeFactory';

import type { StateCreator, StoreMutatorIdentifier } from 'zustand';

/**
* Creates zustand store with additional selectors and actions.
*
* @param {StateType | StateCreator<StateType, Mps, Mcs>} initializer - A function or object that initializes the state.
* @param {TBaseStoreOptions<StateType>} options - store create options.
*/

export const createStore = <
StateType extends TState,
Mps extends [StoreMutatorIdentifier, unknown][] = [],
Mcs extends [StoreMutatorIdentifier, unknown][] = [],
CreateStoreOptions extends
TBaseStoreOptions<StateType> = TBaseStoreOptions<StateType>,
>(
initialState: StateType | StateCreator<StateType, Mps, Mcs>,
initializer: StateType | StateCreator<StateType, Mps, Mcs>,
options: CreateStoreOptions
) => {
type Mutators = [...DefaultMutators<StateType, CreateStoreOptions>, ...Mcs];
Expand All @@ -35,6 +42,7 @@ export const createStore = <
immer: immerOptions,
mutative: mutativeOptions,
persist: persistOptions,
isMutativeState,
} = options;

//current middlewares order devTools(persist(immer(initiator)))
Expand Down Expand Up @@ -82,9 +90,9 @@ export const createStore = <
.reverse()
.reduce(
(y, fn) => fn(y),
(typeof initialState === 'function'
? initialState
: () => initialState) as StateCreator<StateType>
(typeof initializer === 'function'
? initializer
: () => initializer) as StateCreator<StateType>
) as StateCreator<StateType, [], Mutators>;

const store = createStoreZustand(stateMutators);
Expand All @@ -94,7 +102,9 @@ export const createStore = <
const stateActions = generateStateActions(
store,
name,
_immerOptionsInternal.enabled || _mutativeOptionsInternal.enabled
isMutativeState ||
_immerOptionsInternal.enabled ||
_mutativeOptionsInternal.enabled
);

const hookSelectors = generateStateHookSelectors(store);
Expand Down
26 changes: 26 additions & 0 deletions packages/zustand-x/src/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,34 @@ import { TState } from './utils';

export type TBaseStoreOptions<StateType extends TState> = {
name: string;

/**
* Devtools middleware options.
* https://zustand.docs.pmnd.rs/middlewares/devtools
*/
devtools?: DevtoolsOptions;

/**
* Immer middleware options.
* https://zustand.docs.pmnd.rs/middlewares/immer
*/
immer?: ImmerOptions;

/**
* Mutative middleware options.
* https://www.npmjs.com/package/mutative
*/
mutative?: MutativeOptions;

/**
* Persist middleware options.
* https://zustand.docs.pmnd.rs/middlewares/persist
*/
persist?: PersistOptions<StateType>;

/**
* If you're using custom middleware like `immer` and `mutative`
* and no need to return new state then set this value to `true`.
*/
isMutativeState?: boolean;
};
2 changes: 1 addition & 1 deletion packages/zustand-x/src/types/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type TGetStoreEqualityRecord<O> = {
[K in keyof O]: (equalityFn?: TEqualityChecker<O[K]>) => O[K];
};
export type TSetStoreRecord<O> = {
[K in keyof O]: (value: O[K], mutative?: boolean) => void;
[K in keyof O]: (value: O[K]) => void;
};
export type TUseStoreSelectorType<
StateType extends TState,
Expand Down
6 changes: 3 additions & 3 deletions packages/zustand-x/src/utils/generateStateActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ export const generateStateActions = <

Object.keys(store.getState() || {}).forEach((key) => {
const typedKey = key as keyof StateType;
actions[typedKey] = (value, mutative) => {
actions[typedKey] = (value) => {
const prevValue = store.getState()[typedKey];
if (prevValue === value) return;
const actionKey = key.replace(/^\S/, (s) => s.toUpperCase());
const debugLog = storeName ? `@@${storeName}/set${actionKey}` : undefined;
const _isMutative = mutative || isMutative;

//@ts-ignore
store.setState?.(
_isMutative
isMutative
? (draft: StateType) => {
draft[typedKey] = value;
}
Expand Down

0 comments on commit d045469

Please sign in to comment.