Skip to content

Commit

Permalink
support bail-out
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Sep 29, 2024
1 parent 49fde1d commit 92a2159
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/with-slices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,14 @@ export function withSlices<
config.actions,
)) {
state[actionName] = (...args: unknown[]) => {
set(((prevState: Record<string, unknown>) => ({
[config.name]: actionFn(...args)(prevState[config.name]),
})) as never);
set(((prevState: Record<string, unknown>) => {
const prevSlice = prevState[config.name];
const nextSlice = actionFn(...args)(prevSlice);
if (Object.is(prevSlice, nextSlice)) {
return prevState;
}
return { [config.name]: nextSlice };
}) as never);
};
}
}
Expand Down

0 comments on commit 92a2159

Please sign in to comment.