Skip to content

Commit

Permalink
remove unnecessary exports
Browse files Browse the repository at this point in the history
  • Loading branch information
imarabinda committed Jan 5, 2025
1 parent d7ec1d4 commit c47ff8c
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 30 deletions.
6 changes: 5 additions & 1 deletion packages/zustand-x/src/createStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ export const createStore = <

const getterSelectors = generateStateGetSelectors(store);

const stateActions = generateStateActions(store, name);
const stateActions = generateStateActions(
store,
name,
_immerOptionsInternal.enabled || _mutativeOptionsInternal.enabled
);

const hookSelectors = generateStateHookSelectors(store);

Expand Down
1 change: 0 additions & 1 deletion packages/zustand-x/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@
export * from './createStore';
export * from './middlewares';
export * from './types';
export * from './utils';
10 changes: 7 additions & 3 deletions packages/zustand-x/src/middlewares/immer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ type StoreImmer<S> = S extends {
| SetStateType<A1>
| Partial<SetStateType<A1>>
| ((state: Draft<Partial<SetStateType<A1>>>) => void),
shouldReplace?: false,
shouldReplace?: true,
...a: SkipTwo<A1>
): Sr1;
setState(
nextStateOrUpdater:
| SetStateType<A2>
| ((state: Draft<Partial<SetStateType<A2>>>) => void),
shouldReplace: true,
shouldReplace: false,
...a: SkipTwo<A2>
): Sr2;
}
Expand All @@ -77,7 +77,11 @@ const immerImpl: ImmerImpl = (initializer) => (set, get, store) => {
typeof updater === 'function' ? produce(updater as any) : updater
) as ((s: T) => T) | T | Partial<T>;

return set(nextState, replace as any, ...a);
return set(
nextState,
typeof replace === 'boolean' ? (replace as any) : true,
...a
);
};

return initializer(store.setState, get, store);
Expand Down
10 changes: 7 additions & 3 deletions packages/zustand-x/src/middlewares/mutative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ type StoreMutative<S> = S extends {
| SetStateType<A1>
| Partial<SetStateType<A1>>
| ((state: Draft<Partial<SetStateType<A1>>>) => void),
shouldReplace?: false,
shouldReplace?: true,
...a: SkipTwo<A1>
): Sr1;
setState(
nextStateOrUpdater:
| SetStateType<A2>
| ((state: Draft<Partial<SetStateType<A2>>>) => void),
shouldReplace: true,
shouldReplace: false,
...a: SkipTwo<A2>
): Sr2;
}
Expand Down Expand Up @@ -81,7 +81,11 @@ const mutativeImpl: MutativeImpl =
: updater
) as ((s: T) => T) | T | Partial<T>;

return set(nextState as any, replace as any, ...a);
return set(
nextState as any,
typeof replace === 'boolean' ? (replace as any) : true,
...a
);
};

return initializer(store.setState, get, store);
Expand Down
1 change: 0 additions & 1 deletion packages/zustand-x/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* @file Automatically generated by barrelsby.
*/

export * from './global';
export * from './middleware';
export * from './mutator';
export * from './options';
Expand Down
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]) => void;
[K in keyof O]: (value: O[K], mutative?: boolean) => void;
};
export type TUseStoreSelectorType<
StateType extends TState,
Expand Down
16 changes: 9 additions & 7 deletions packages/zustand-x/src/utils/generateStateActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,26 @@ export const generateStateActions = <
Mutators extends [StoreMutatorIdentifier, unknown][],
>(
store: TCreatedStoreType<StateType, Mutators>,
storeName?: string
storeName?: string,
isMutative?: boolean
) => {
const actions: TSetStoreRecord<StateType> = {} as TSetStoreRecord<StateType>;

Object.keys(store.getState() || {}).forEach((key) => {
const typedKey = key as keyof StateType;
actions[typedKey] = (value) => {
actions[typedKey] = (value, mutative) => {
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?.(
(draft: StateType) => {
draft[typedKey] = value;
return draft;
},
_isMutative
? (draft: StateType) => {
draft[typedKey] = value;
}
: { [typedKey]: value },
undefined,
debugLog
);
Expand Down
13 changes: 0 additions & 13 deletions packages/zustand-x/src/utils/index.ts

This file was deleted.

0 comments on commit c47ff8c

Please sign in to comment.