Skip to content

Commit

Permalink
update selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
SKarolFolio committed Dec 11, 2024
1 parent 0c2aec4 commit 7c27031
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/store/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { createSelectors } from './utils/selectors';
import { useStatusStore } from './stores/status';
import { useLoadingStateStore } from './stores/loadingState';
import { useMarcPreviewStore } from './stores/marcPreview';
Expand All @@ -8,11 +7,13 @@ import { useConfigStore } from './stores/config';
import { useUIStore } from './stores/ui';
import { useSearchStore } from './stores/search';

export const useStatusState = () => createSelectors(useStatusStore).use;
export const useLoadingState = () => createSelectors(useLoadingStateStore).use;
export const useMarcPreviewState = () => createSelectors(useMarcPreviewStore).use;
export const useProfileState = () => createSelectors(useProfileStore).use;
export const useInputsState = () => createSelectors(useInputsStore).use;
export const useConfigState = () => createSelectors(useConfigStore).use;
export const useUIState = () => createSelectors(useUIStore).use;
export const useSearchState = () => createSelectors(useSearchStore).use;
// "createSelectors" can be used here for generation of the memoized selectors.
// Now this function is non-optimized and its usage causes memory leaks.
export const useStatusState = useStatusStore;
export const useLoadingState = useLoadingStateStore;
export const useMarcPreviewState = useMarcPreviewStore;
export const useProfileState = useProfileStore;
export const useInputsState = useInputsStore;
export const useConfigState = useConfigStore;
export const useUIState = useUIStore;
export const useSearchState = useSearchStore;
2 changes: 2 additions & 0 deletions src/store/utils/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ type WithSelectors<S> = S extends {
? S & { use: { [K in keyof T]: T[K] } }
: never;

// Use this function to generate all available selectors.
// Now it causes memory leaks if invoke it after each re-render, so it should be memoized.
export const createSelectors = <S extends UseBoundStore<StoreApi<object>>>(_store: S) => {
const store = _store as WithSelectors<typeof _store>;
store.use = {};
Expand Down

0 comments on commit 7c27031

Please sign in to comment.