|
1 | | -import React, {useMemo} from 'react'; |
| 1 | +import React, { useMemo } from 'react'; |
2 | 2 | import { getDefaultStore, useAtom, useAtomValue, useSetAtom } from 'jotai'; |
3 | 3 | import { selectAtom, useHydrateAtoms } from 'jotai/utils'; |
4 | 4 |
|
@@ -473,9 +473,12 @@ const convertScopeShorthand = ( |
473 | 473 | ? { scope: optionsOrScope } |
474 | 474 | : optionsOrScope; |
475 | 475 |
|
476 | | -const useConvertScopeShorthand: typeof convertScopeShorthand = (optionsOrScope) => { |
| 476 | +const useConvertScopeShorthand: typeof convertScopeShorthand = ( |
| 477 | + optionsOrScope |
| 478 | +) => { |
477 | 479 | const convertedOptions = convertScopeShorthand(optionsOrScope); |
478 | 480 | // Works because all values are primitives |
| 481 | + // eslint-disable-next-line react-compiler/react-compiler |
479 | 482 | return useMemo(() => convertedOptions, Object.values(convertedOptions)); |
480 | 483 | }; |
481 | 484 |
|
@@ -764,115 +767,118 @@ Please wrap them with useCallback or configure the deps array correctly.` |
764 | 767 | const convertedOptions = useConvertScopeShorthand(options); |
765 | 768 | const store = useStore(convertedOptions); |
766 | 769 |
|
767 | | - return useMemo(() => ({ |
768 | | - // store.use<Key>Value() |
769 | | - ...(withStoreAndOptions( |
770 | | - atomsOfUseValue, |
771 | | - getUseValueIndex, |
772 | | - store, |
773 | | - convertedOptions |
774 | | - ) as UseKeyValueApis<MyStoreAtoms>), |
775 | | - // store.get<Key>() |
776 | | - ...(withStoreAndOptions( |
777 | | - atomsOfGet, |
778 | | - getGetIndex, |
779 | | - store, |
780 | | - convertedOptions |
781 | | - ) as GetKeyApis<MyStoreAtoms>), |
782 | | - // store.useSet<Key>() |
783 | | - ...(withStoreAndOptions( |
784 | | - atomsOfUseSet, |
785 | | - getUseSetIndex, |
786 | | - store, |
787 | | - convertedOptions |
788 | | - ) as UseSetKeyApis<MyStoreAtoms>), |
789 | | - // store.set<Key>(...args) |
790 | | - ...(withStoreAndOptions( |
791 | | - atomsOfSet, |
792 | | - getSetIndex, |
793 | | - store, |
794 | | - convertedOptions |
795 | | - ) as SetKeyApis<MyStoreAtoms>), |
796 | | - // store.use<Key>State() |
797 | | - ...(withStoreAndOptions( |
798 | | - atomsOfUseState, |
799 | | - getUseStateIndex, |
800 | | - store, |
801 | | - convertedOptions |
802 | | - ) as UseKeyStateApis<MyStoreAtoms>), |
803 | | - // store.subscribe<Key>(callback) |
804 | | - ...(withStoreAndOptions( |
805 | | - atomsOfSubscribe, |
806 | | - getSubscribeIndex, |
807 | | - store, |
808 | | - convertedOptions |
809 | | - ) as SubscribeKeyApis<MyStoreAtoms>), |
810 | | - // store.useValue('key') |
811 | | - useValue: withKeyAndStoreAndOptions( |
812 | | - atomsOfUseValue, |
813 | | - store, |
814 | | - convertedOptions |
815 | | - ) as UseParamKeyValueApi<MyStoreAtoms>, |
816 | | - // store.get('key') |
817 | | - get: withKeyAndStoreAndOptions( |
818 | | - atomsOfGet, |
819 | | - store, |
820 | | - convertedOptions |
821 | | - ) as GetParamKeyApi<MyStoreAtoms>, |
822 | | - // store.useSet('key') |
823 | | - useSet: withKeyAndStoreAndOptions( |
824 | | - atomsOfUseSet, |
825 | | - store, |
826 | | - convertedOptions |
827 | | - ) as UseSetParamKeyApi<MyStoreAtoms>, |
828 | | - // store.set('key', ...args) |
829 | | - set: withKeyAndStoreAndOptions( |
830 | | - atomsOfSet, |
831 | | - store, |
832 | | - convertedOptions |
833 | | - ) as SetParamKeyApi<MyStoreAtoms>, |
834 | | - // store.useState('key') |
835 | | - useState: withKeyAndStoreAndOptions( |
836 | | - atomsOfUseState, |
837 | | - store, |
838 | | - convertedOptions |
839 | | - ) as UseParamKeyStateApi<MyStoreAtoms>, |
840 | | - // store.subscribe('key', callback) |
841 | | - subscribe: withKeyAndStoreAndOptions( |
842 | | - atomsOfSubscribe, |
843 | | - store, |
844 | | - convertedOptions |
845 | | - ) as SubscribeParamKeyApi<MyStoreAtoms>, |
846 | | - // store.useAtomValue(atomConfig) |
847 | | - useAtomValue: ((atomConfig, selector, equalityFnOrDeps, deps) => |
848 | | - // eslint-disable-next-line react-compiler/react-compiler |
849 | | - useAtomValueWithStore( |
850 | | - atomConfig, |
| 770 | + return useMemo( |
| 771 | + () => ({ |
| 772 | + // store.use<Key>Value() |
| 773 | + ...(withStoreAndOptions( |
| 774 | + atomsOfUseValue, |
| 775 | + getUseValueIndex, |
851 | 776 | store, |
852 | | - convertedOptions, |
853 | | - selector, |
854 | | - equalityFnOrDeps, |
855 | | - deps |
856 | | - )) as UseAtomParamValueApi, |
857 | | - // store.getAtom(atomConfig) |
858 | | - getAtom: (atomConfig) => |
859 | | - getAtomWithStore(atomConfig, store, convertedOptions), |
860 | | - // store.useSetAtom(atomConfig) |
861 | | - useSetAtom: (atomConfig) => |
862 | | - // eslint-disable-next-line react-compiler/react-compiler |
863 | | - useSetAtomWithStore(atomConfig, store, convertedOptions), |
864 | | - // store.setAtom(atomConfig, ...args) |
865 | | - setAtom: (atomConfig) => |
866 | | - setAtomWithStore(atomConfig, store, convertedOptions), |
867 | | - // store.useAtomState(atomConfig) |
868 | | - useAtomState: (atomConfig) => |
869 | | - // eslint-disable-next-line react-compiler/react-compiler |
870 | | - useAtomStateWithStore(atomConfig, store, convertedOptions), |
871 | | - // store.subscribeAtom(atomConfig, callback) |
872 | | - subscribeAtom: (atomConfig) => |
873 | | - subscribeAtomWithStore(atomConfig, store, convertedOptions), |
874 | | - store, |
875 | | - }), [store, convertedOptions]); |
| 777 | + convertedOptions |
| 778 | + ) as UseKeyValueApis<MyStoreAtoms>), |
| 779 | + // store.get<Key>() |
| 780 | + ...(withStoreAndOptions( |
| 781 | + atomsOfGet, |
| 782 | + getGetIndex, |
| 783 | + store, |
| 784 | + convertedOptions |
| 785 | + ) as GetKeyApis<MyStoreAtoms>), |
| 786 | + // store.useSet<Key>() |
| 787 | + ...(withStoreAndOptions( |
| 788 | + atomsOfUseSet, |
| 789 | + getUseSetIndex, |
| 790 | + store, |
| 791 | + convertedOptions |
| 792 | + ) as UseSetKeyApis<MyStoreAtoms>), |
| 793 | + // store.set<Key>(...args) |
| 794 | + ...(withStoreAndOptions( |
| 795 | + atomsOfSet, |
| 796 | + getSetIndex, |
| 797 | + store, |
| 798 | + convertedOptions |
| 799 | + ) as SetKeyApis<MyStoreAtoms>), |
| 800 | + // store.use<Key>State() |
| 801 | + ...(withStoreAndOptions( |
| 802 | + atomsOfUseState, |
| 803 | + getUseStateIndex, |
| 804 | + store, |
| 805 | + convertedOptions |
| 806 | + ) as UseKeyStateApis<MyStoreAtoms>), |
| 807 | + // store.subscribe<Key>(callback) |
| 808 | + ...(withStoreAndOptions( |
| 809 | + atomsOfSubscribe, |
| 810 | + getSubscribeIndex, |
| 811 | + store, |
| 812 | + convertedOptions |
| 813 | + ) as SubscribeKeyApis<MyStoreAtoms>), |
| 814 | + // store.useValue('key') |
| 815 | + useValue: withKeyAndStoreAndOptions( |
| 816 | + atomsOfUseValue, |
| 817 | + store, |
| 818 | + convertedOptions |
| 819 | + ) as UseParamKeyValueApi<MyStoreAtoms>, |
| 820 | + // store.get('key') |
| 821 | + get: withKeyAndStoreAndOptions( |
| 822 | + atomsOfGet, |
| 823 | + store, |
| 824 | + convertedOptions |
| 825 | + ) as GetParamKeyApi<MyStoreAtoms>, |
| 826 | + // store.useSet('key') |
| 827 | + useSet: withKeyAndStoreAndOptions( |
| 828 | + atomsOfUseSet, |
| 829 | + store, |
| 830 | + convertedOptions |
| 831 | + ) as UseSetParamKeyApi<MyStoreAtoms>, |
| 832 | + // store.set('key', ...args) |
| 833 | + set: withKeyAndStoreAndOptions( |
| 834 | + atomsOfSet, |
| 835 | + store, |
| 836 | + convertedOptions |
| 837 | + ) as SetParamKeyApi<MyStoreAtoms>, |
| 838 | + // store.useState('key') |
| 839 | + useState: withKeyAndStoreAndOptions( |
| 840 | + atomsOfUseState, |
| 841 | + store, |
| 842 | + convertedOptions |
| 843 | + ) as UseParamKeyStateApi<MyStoreAtoms>, |
| 844 | + // store.subscribe('key', callback) |
| 845 | + subscribe: withKeyAndStoreAndOptions( |
| 846 | + atomsOfSubscribe, |
| 847 | + store, |
| 848 | + convertedOptions |
| 849 | + ) as SubscribeParamKeyApi<MyStoreAtoms>, |
| 850 | + // store.useAtomValue(atomConfig) |
| 851 | + useAtomValue: ((atomConfig, selector, equalityFnOrDeps, deps) => |
| 852 | + // eslint-disable-next-line react-compiler/react-compiler |
| 853 | + useAtomValueWithStore( |
| 854 | + atomConfig, |
| 855 | + store, |
| 856 | + convertedOptions, |
| 857 | + selector, |
| 858 | + equalityFnOrDeps, |
| 859 | + deps |
| 860 | + )) as UseAtomParamValueApi, |
| 861 | + // store.getAtom(atomConfig) |
| 862 | + getAtom: (atomConfig) => |
| 863 | + getAtomWithStore(atomConfig, store, convertedOptions), |
| 864 | + // store.useSetAtom(atomConfig) |
| 865 | + useSetAtom: (atomConfig) => |
| 866 | + // eslint-disable-next-line react-compiler/react-compiler |
| 867 | + useSetAtomWithStore(atomConfig, store, convertedOptions), |
| 868 | + // store.setAtom(atomConfig, ...args) |
| 869 | + setAtom: (atomConfig) => |
| 870 | + setAtomWithStore(atomConfig, store, convertedOptions), |
| 871 | + // store.useAtomState(atomConfig) |
| 872 | + useAtomState: (atomConfig) => |
| 873 | + // eslint-disable-next-line react-compiler/react-compiler |
| 874 | + useAtomStateWithStore(atomConfig, store, convertedOptions), |
| 875 | + // store.subscribeAtom(atomConfig, callback) |
| 876 | + subscribeAtom: (atomConfig) => |
| 877 | + subscribeAtomWithStore(atomConfig, store, convertedOptions), |
| 878 | + store, |
| 879 | + }), |
| 880 | + [store, convertedOptions] |
| 881 | + ); |
876 | 882 | }; |
877 | 883 |
|
878 | 884 | const useNameState = <K extends keyof StoreAtoms<T, E>>( |
|
0 commit comments