Skip to content

Commit

Permalink
do not change with-slices type, and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Jul 9, 2024
1 parent 79c737c commit 52d9504
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 35 deletions.
39 changes: 21 additions & 18 deletions src/immer/create-slice-with-immer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,34 @@ export type SliceConfig<
};

type ImmerActions<Value, Actions extends SliceActions<Value>> = {
[K in keyof Actions]: (...args: InferArgs<Actions[K]>) => (prev: Value) => Value | void;
[K in keyof Actions]: (
...args: InferArgs<Actions[K]>
) => (prev: Value) => Value;
};

export function createSliceWithImmer<
Name extends string,
Value,
Actions extends SliceActions<Value>,
>(config: SliceConfig<Name, Value, Actions>) {
const immerActions = Object.keys(config.actions).reduce((acc, actionKey) => {
const action = config.actions[actionKey as keyof Actions];

if (action) {
acc[actionKey as keyof Actions] = ((...args: InferArgs<typeof action>) => (prev: Value) => {
// Use produce to handle draft modification or return new state
return produce(prev, (draft: Value) => {
const result = (action as (...args: InferArgs<typeof action>) => (draft: Value) => Value | void)(...args)(draft);
if (result !== undefined) {
return result;
}
});
}) as ImmerActions<Value, Actions>[typeof actionKey];
}

return acc;
}, {} as ImmerActions<Value, Actions>);
const immerActions = Object.keys(config.actions).reduce(
(acc, actionKey) => {
const action = config.actions[actionKey as keyof Actions];

if (action) {
acc[actionKey as keyof Actions] = ((
...args: InferArgs<typeof action>
) =>
(prev: Value) =>
produce(prev, (draft: Value) =>
action(...args)(draft),
)) as ImmerActions<Value, Actions>[typeof actionKey];
}

return acc;
},
{} as ImmerActions<Value, Actions>,
);

return {
...config,
Expand Down
14 changes: 1 addition & 13 deletions src/with-slices.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
type SliceActions<Value> = {
[actionName: string]: (...args: never[]) => (prev: Value) => Value | void;
};

type SliceConfig<
Name extends string,
Value,
Actions extends SliceActions<Value>,
> = {
name: Name;
value: Value;
actions: Actions;
};
import type { SliceConfig } from './create-slice.js';

type ParametersIf<T> = T extends (...args: infer Args) => unknown
? Args
Expand Down
6 changes: 3 additions & 3 deletions tests/01_basic.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect, test } from 'vitest';
import { create } from 'zustand';
import { createSlice, withSlices } from 'zustand-slices';
import { createSliceWithImmer } from 'zustand-slices/immer';
import { createSliceWithImmer } from 'zustand-slices/immer';

test('should export functions', () => {
expect(createSlice).toBeDefined();
Expand All @@ -25,11 +25,11 @@ test('createSliceWithImmer', () => {
const immerSlice = createSliceWithImmer({
name: 'counter',
value: {
count: 0
count: 0,
},
actions: {
increment: () => (prev) => {
prev.count += 1
prev.count += 1;
},
},
});
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"jsx": "react-jsx",
"baseUrl": ".",
"paths": {
"zustand-slices": ["./src/index.js"]
"zustand-slices": ["./src/index.js"],
"zustand-slices/immer": ["./src/immer/index.js"]
}
},
"exclude": ["dist", "examples"]
Expand Down

0 comments on commit 52d9504

Please sign in to comment.