Skip to content

Commit abcf083

Browse files
committed
refactor: improve types and code style
1 parent 975d4e4 commit abcf083

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

.changeset/flat-beers-drum.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'atomsphere': patch
3+
---
4+
5+
Improve types

src/__tests__/use-atom.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ describe('useAtom', () => {
2121

2222
// Act.
2323
const { result } = renderHook(() => useAtom(countAtom));
24+
2425
result.current[1](1);
2526

2627
// Assert.
@@ -35,6 +36,7 @@ describe('useAtom', () => {
3536

3637
// Act.
3738
const { result } = renderHook(() => useAtom(countAtom));
39+
3840
result.current[1]((prev) => prev + 1);
3941

4042
// Assert.

src/atom.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ export type WritableAtom<T> = ReadableAtom<T> & {
77
set: (newValue: T | ((prev: T) => T)) => void;
88
};
99

10-
type DerivedGetter = <T>(atom: ReadableAtom<T>) => T;
10+
type DeriveFn<T> = (get: <P>(atom: ReadableAtom<P>) => P) => T;
1111

12-
export function atom<T>(derive: (get: DerivedGetter) => T): ReadableAtom<T>;
12+
export function atom<T>(initialValue: DeriveFn<T>): ReadableAtom<T>;
1313
export function atom<T>(initialValue: T): WritableAtom<T>;
14-
export function atom<T>(initialValue: T | ((get: DerivedGetter) => T)) {
14+
export function atom<T>(initialValue: T | DeriveFn<T>) {
1515
return typeof initialValue === 'function'
16-
? createDerivedAtom(initialValue as (get: DerivedGetter) => T)
16+
? createDerivedAtom(initialValue as DeriveFn<T>)
1717
: createAtom(initialValue);
1818
}
1919

@@ -40,8 +40,8 @@ function createAtom<T>(initialValue: T): WritableAtom<T> {
4040
};
4141

4242
const notify = () => {
43-
subscribers.forEach((cb) => {
44-
cb();
43+
subscribers.forEach((subscriber) => {
44+
subscriber();
4545
});
4646
};
4747

@@ -52,10 +52,8 @@ function createAtom<T>(initialValue: T): WritableAtom<T> {
5252
};
5353
}
5454

55-
function createDerivedAtom<T>(
56-
derive: (get: DerivedGetter) => T,
57-
): ReadableAtom<T> {
58-
const getAndSubscribe = <T>(atom: ReadableAtom<T>) => {
55+
function createDerivedAtom<T>(derive: DeriveFn<T>): ReadableAtom<T> {
56+
const getAndSubscribe = <P>(atom: ReadableAtom<P>) => {
5957
atom.subscribe(reCalculate);
6058

6159
return atom.get();

src/use-atom.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,9 @@ export function useAtom<T>(
77

88
export function useAtom<T>(atom: ReadableAtom<T>): ReadableAtom<T>['get'];
99

10-
export function useAtom<T>(atom: WritableAtom<T> | ReadableAtom<T>): any {
11-
const isDerivedAtom = !('set' in atom);
10+
export function useAtom<T>(atom: WritableAtom<T> | ReadableAtom<T>) {
11+
const isWriteableAtom = 'set' in atom;
1212
const value = useSyncExternalStore(atom.subscribe, atom.get);
1313

14-
if (isDerivedAtom) {
15-
return value;
16-
}
17-
18-
return [value, atom.set] as const;
14+
return isWriteableAtom ? [value, atom.set] : value;
1915
}

0 commit comments

Comments
 (0)