Skip to content

Commit

Permalink
Move {getAtom,setAtom,useAtom} to {get,set,use}.atom
Browse files Browse the repository at this point in the history
  • Loading branch information
12joan committed Dec 21, 2023
1 parent e0ff6cc commit 898d26f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 32 deletions.
2 changes: 1 addition & 1 deletion packages/jotai-x/src/createAtomStore.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ describe('createAtomStore', () => {
const derivedAtom = atom((get) => `My name is ${get(userStore.atom.name)}`);

const DerivedAtomConsumer = () => {
const message = useUserStore().getAtom(derivedAtom);
const message = useUserStore().get.atom(derivedAtom);

return <div>{message}</div>;
};
Expand Down
69 changes: 38 additions & 31 deletions packages/jotai-x/src/createAtomStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,17 @@ type UseAtomOptionsOrScope = UseAtomOptions | string;
export type GetRecord<O> = {
[K in keyof O]: (options?: UseAtomOptionsOrScope) => O[K];
};

export type ExtendedGetRecord<O, E> = GetRecord<
O & {
[key in keyof E]: E[key] extends Atom<infer U> ? U : never;
}
>;

export type SetRecord<O> = {
[K in keyof O]: (options?: UseAtomOptionsOrScope) => (value: O[K]) => void;
};

export type UseRecord<O> = {
[K in keyof O]: (
options?: UseAtomOptionsOrScope
Expand Down Expand Up @@ -75,16 +83,9 @@ type UseAtomFn = <V, A extends unknown[], R>(
) => [V, (...args: A) => R];

export type UseStoreApi<T, E> = (options?: UseAtomOptionsOrScope) => {
get: GetRecord<
T & {
[key in keyof E]: E[key] extends Atom<infer U> ? U : never;
}
>;
set: SetRecord<T>;
use: UseRecord<T>;
getAtom: GetAtomFn;
setAtom: SetAtomFn;
useAtom: UseAtomFn;
get: ExtendedGetRecord<T, E> & { atom: GetAtomFn };
set: SetRecord<T> & { atom: SetAtomFn };
use: UseRecord<T> & { atom: UseAtomFn };
store: (options?: UseAtomOptionsOrScope) => JotaiStore | undefined;
};

Expand Down Expand Up @@ -172,9 +173,9 @@ export const createAtomStore = <
const useStoreIndex = getUseStoreIndex(name) as UseNameStore<N>;
const storeIndex = getStoreIndex(name) as NameStore<N>;

const getAtoms = {} as ReturnType<UseStoreApi<T, E>>['get'];
const setAtoms = {} as ReturnType<UseStoreApi<T, E>>['set'];
const useAtoms = {} as ReturnType<UseStoreApi<T, E>>['use'];
const getAtoms = {} as ExtendedGetRecord<T, E>;
const setAtoms = {} as SetRecord<T>;
const useAtoms = {} as UseRecord<T>;
const primitiveAtoms = {} as WritableAtomRecord<T>;

const useStore = (optionsOrScope: UseAtomOptionsOrScope = {}) => {
Expand Down Expand Up @@ -236,24 +237,30 @@ export const createAtomStore = <
};

const useStoreApi: UseStoreApi<T, E> = (defaultOptions = {}) => ({
get: withDefaultOptions(getAtoms, convertScopeShorthand(defaultOptions)),
set: withDefaultOptions(setAtoms, convertScopeShorthand(defaultOptions)),
use: withDefaultOptions(useAtoms, convertScopeShorthand(defaultOptions)),
getAtom: (atomConfig, options) =>
useAtomValueWithStore(atomConfig, {
...convertScopeShorthand(defaultOptions),
...convertScopeShorthand(options),
}),
setAtom: (atomConfig, options) =>
useSetAtomWithStore(atomConfig, {
...convertScopeShorthand(defaultOptions),
...convertScopeShorthand(options),
}),
useAtom: (atomConfig, options) =>
useAtomWithStore(atomConfig, {
...convertScopeShorthand(defaultOptions),
...convertScopeShorthand(options),
}),
get: {
...withDefaultOptions(getAtoms, convertScopeShorthand(defaultOptions)),
atom: (atomConfig, options) =>
useAtomValueWithStore(atomConfig, {
...convertScopeShorthand(defaultOptions),
...convertScopeShorthand(options),
}),
},
set: {
...withDefaultOptions(setAtoms, convertScopeShorthand(defaultOptions)),
atom: (atomConfig, options) =>
useSetAtomWithStore(atomConfig, {
...convertScopeShorthand(defaultOptions),
...convertScopeShorthand(options),
}),
},
use: {
...withDefaultOptions(useAtoms, convertScopeShorthand(defaultOptions)),
atom: (atomConfig, options) =>
useAtomWithStore(atomConfig, {
...convertScopeShorthand(defaultOptions),
...convertScopeShorthand(options),
}),
},
store: (options) =>
useStore({
...convertScopeShorthand(defaultOptions),
Expand Down

0 comments on commit 898d26f

Please sign in to comment.