Skip to content

Commit

Permalink
Refactor of selector generation (#18)
Browse files Browse the repository at this point in the history
* refactor: generating selectors only once

improved perfomance. previously selector hooks were re-generated on every call to store

* v2.0.4

---------

Co-authored-by: kmindubaev <[email protected]>
  • Loading branch information
Kamilcat and kmindubaev authored Jun 6, 2024
1 parent bf1cb1c commit 70d26bb
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "auto-zustand-selectors-hook",
"version": "2.0.3",
"version": "2.0.4",
"keywords": [],
"description": "",
"main": "dist/index.min.js",
Expand Down Expand Up @@ -72,4 +72,4 @@
"zustand": "^4.5.2"
},
"packageManager": "[email protected]+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72"
}
}
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 70d26bb

Please sign in to comment.