Skip to content

Commit

Permalink
refactor: generating selectors only once
Browse files Browse the repository at this point in the history
improved perfomance. previously selector hooks were re-generated on every call to store
  • Loading branch information
kmindubaev committed Jun 6, 2024
1 parent bf1cb1c commit a8635c4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
8 changes: 3 additions & 5 deletions src/createSelectorFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ export function createSelectorFunctions<StateType extends object>(

Object.keys(storeIn.getState()).forEach((key) => {
const selector = (state: StateType) => state[key as keyof StateType];
storeIn.use[key] = () => {
return typeof storeIn === 'function'
? storeIn(selector)
: useStore(storeIn, selector as any);
};
storeIn.use[key] = typeof storeIn === 'function'
? () => storeIn(selector)
: () => useStore(storeIn, selector as any);
});

return store as UseBoundStore<StoreApi<StateType>> &
Expand Down
8 changes: 3 additions & 5 deletions src/createSelectorHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ export function createSelectorHooks<StateType extends object>(

Object.keys(storeIn.getState()).forEach((key) => {
const selector = (state: StateType) => state[key as keyof StateType];
storeIn[`use${capitalize(key)}`] = () => {
return typeof storeIn === 'function'
? storeIn(selector)
: useStore(storeIn, selector as any);
};
storeIn[`use${capitalize(key)}`] = typeof storeIn === 'function'
? () => storeIn(selector)
: () => useStore(storeIn, selector as any);
});

return storeIn as UseBoundStore<StoreApi<StateType>> &
Expand Down

0 comments on commit a8635c4

Please sign in to comment.