Skip to content

Commit

Permalink
use Maps to only iterate through slices that actually have a given ac…
Browse files Browse the repository at this point in the history
…tion (#2)

* use Maps to only iterate through slices that actually have a matching action

* use Object.entries instead of Object.keys

* only call set once per action

---------

Co-authored-by: Daishi Kato <[email protected]>
  • Loading branch information
EskiMojo14 and dai-shi authored Apr 21, 2024
1 parent 5c4b769 commit 077a757
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,26 +91,28 @@ export function withSlices<Configs extends SliceConfig<string, unknown, any>[]>(
) => void,
) => {
const state: any = {};
const actionNameSet = new Set<string>();
const sliceMapsByAction = new Map<string, Map<string, any>>();
for (const config of configs) {
state[config.name] = config.value;
for (const actionName of Object.keys(config.actions)) {
actionNameSet.add(actionName);
for (const [actionName, actionFn] of Object.entries(config.actions)) {
let actionsBySlice = sliceMapsByAction.get(actionName);
if (!actionsBySlice) {
sliceMapsByAction.set(actionName, (actionsBySlice = new Map()));
}
actionsBySlice.set(config.name, actionFn);
}
}
for (const actionName of actionNameSet) {
for (const [actionName, actionsBySlice] of sliceMapsByAction) {
state[actionName] = (...args: any[]) => {
// FIXME not very efficient to do this every time
for (const config of configs) {
const actionFn = config.actions[actionName];
if (actionFn) {
set((prevState: any) => {
const prevSlice = prevState[config.name];
const nextSlice = actionFn(...args)(prevSlice);
return { [config.name]: nextSlice } as any;
});
set((prevState: any) => {
const nextState: any = {};
for (const [sliceName, actionFn] of actionsBySlice) {
const prevSlice = prevState[sliceName];
const nextSlice = actionFn(...args)(prevSlice);
nextState[sliceName] = nextSlice;
}
}
return nextState;
});
};
}
return state;
Expand Down

0 comments on commit 077a757

Please sign in to comment.