atoms-state v0.8.1
git clone https://git.pyrossh.dev/atoms-state
Simple State management for react
22fa1eb
— pyros2097
2021-04-03T09:20:27+05:30
fix lint
Generated files not shown: yarn.lock
- package.json +5 -1
- src/index.ts +30 -21
- test/index.test.ts +11 -15
package.json
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"start": "tsdx watch",
|
|
15
15
|
"build": "tsdx build",
|
|
16
16
|
"test": "tsdx test",
|
|
17
|
-
"lint": "tsdx lint",
|
|
17
|
+
"lint": "tsdx lint --fix",
|
|
18
18
|
"prepare": "tsdx build",
|
|
19
19
|
"size": "size-limit",
|
|
20
20
|
"analyze": "size-limit --why",
|
|
@@ -63,5 +63,9 @@
|
|
|
63
63
|
"tsdx": "^0.14.1",
|
|
64
64
|
"tslib": "^2.1.0",
|
|
65
65
|
"typescript": "^4.2.3"
|
|
66
|
+
},
|
|
67
|
+
"resolutions": {
|
|
68
|
+
"**/@typescript-eslint/eslint-plugin": "^4.1.1",
|
|
69
|
+
"**/@typescript-eslint/parser": "^4.1.1"
|
|
66
70
|
}
|
|
67
71
|
}
|
src/index.ts
CHANGED
|
@@ -18,7 +18,7 @@ export const atom = <S>(initial: S | DerivedAtomReader<S>): Atom<S> => {
|
|
|
18
18
|
};
|
|
19
19
|
const compute = () => {
|
|
20
20
|
value = (initial as DerivedAtomReader<S>)(get);
|
|
21
|
-
subs.forEach(
|
|
21
|
+
subs.forEach(sub => {
|
|
22
22
|
sub(value);
|
|
23
23
|
});
|
|
24
24
|
};
|
|
@@ -37,7 +37,7 @@ export const atom = <S>(initial: S | DerivedAtomReader<S>): Atom<S> => {
|
|
|
37
37
|
},
|
|
38
38
|
update(fn: (oldValue: S) => S) {
|
|
39
39
|
value = fn(value);
|
|
40
|
-
subs.forEach(
|
|
40
|
+
subs.forEach(sub => {
|
|
41
41
|
sub(value);
|
|
42
42
|
});
|
|
43
43
|
},
|
|
@@ -47,7 +47,7 @@ export const atom = <S>(initial: S | DerivedAtomReader<S>): Atom<S> => {
|
|
|
47
47
|
export const useAtom = <S>(atom: Atom<S>): S => {
|
|
48
48
|
const [data, setData] = useState<S>(atom.getValue());
|
|
49
49
|
useEffect(() => {
|
|
50
|
-
return atom.subscribe(
|
|
50
|
+
return atom.subscribe(value => {
|
|
51
51
|
setData(value);
|
|
52
52
|
});
|
|
53
53
|
}, []);
|
|
@@ -71,10 +71,10 @@ export const asyncAtom = <P, S>(fn: (p: P) => Promise<S>): AsyncAtom<P, S> => {
|
|
|
71
71
|
params = p;
|
|
72
72
|
value = fn(p);
|
|
73
73
|
value
|
|
74
|
-
.then(
|
|
74
|
+
.then(res => {
|
|
75
75
|
value = res;
|
|
76
76
|
})
|
|
77
|
-
.catch(
|
|
77
|
+
.catch(err => {
|
|
78
78
|
value = err;
|
|
79
79
|
});
|
|
80
80
|
}
|
|
@@ -89,7 +89,7 @@ export const asyncAtom = <P, S>(fn: (p: P) => Promise<S>): AsyncAtom<P, S> => {
|
|
|
89
89
|
},
|
|
90
90
|
update(updater: (oldValue: S) => S) {
|
|
91
91
|
value = updater(value as S);
|
|
92
|
-
subs.forEach(
|
|
92
|
+
subs.forEach(sub => {
|
|
93
93
|
sub(value as S);
|
|
94
94
|
});
|
|
95
95
|
},
|
|
@@ -99,7 +99,7 @@ export const asyncAtom = <P, S>(fn: (p: P) => Promise<S>): AsyncAtom<P, S> => {
|
|
|
99
99
|
export const useAsyncAtom = <P, S>(a: AsyncAtom<P, S>, params: P) => {
|
|
100
100
|
const [, toggle] = useState(false);
|
|
101
101
|
useEffect(() => {
|
|
102
|
-
return a.subscribe(() => toggle(
|
|
102
|
+
return a.subscribe(() => toggle(v => !v));
|
|
103
103
|
}, []);
|
|
104
104
|
const v = a.compute(params);
|
|
105
105
|
if (v instanceof Error) {
|
|
@@ -109,35 +109,44 @@ export const useAsyncAtom = <P, S>(a: AsyncAtom<P, S>, params: P) => {
|
|
|
109
109
|
} else {
|
|
110
110
|
return v;
|
|
111
111
|
}
|
|
112
|
-
}
|
|
112
|
+
};
|
|
113
113
|
|
|
114
|
-
type PromiseFunc<S, P> = (p: P) => Promise<S>
|
|
114
|
+
type PromiseFunc<S, P> = (p: P) => Promise<S>;
|
|
115
115
|
|
|
116
116
|
const usePromiseCache: Map<Function, Map<string, any>> = new Map();
|
|
117
|
+
export const usePromise = <S, P>(
|
|
118
|
+
fn: PromiseFunc<S, P>,
|
|
119
|
+
params: P
|
|
117
|
-
|
|
120
|
+
): [S | null, (v: S) => void] => {
|
|
118
121
|
const [data, setData] = useState<boolean>(false);
|
|
119
122
|
const fnCache = usePromiseCache.get(fn) || new Map();
|
|
120
123
|
if (!usePromiseCache.get(fn)) {
|
|
121
124
|
usePromiseCache.set(fn, fnCache);
|
|
122
125
|
}
|
|
123
126
|
const key = typeof params === 'string' ? params : JSON.stringify(params);
|
|
124
|
-
const value = fnCache.get(key)
|
|
127
|
+
const value = fnCache.get(key);
|
|
125
128
|
if (value) {
|
|
126
129
|
if (value instanceof Promise) {
|
|
127
130
|
throw value;
|
|
128
131
|
} else if (value instanceof Error || value?.errors) {
|
|
129
132
|
throw value;
|
|
130
133
|
}
|
|
134
|
+
return [
|
|
135
|
+
value,
|
|
131
|
-
|
|
136
|
+
v => {
|
|
132
|
-
|
|
137
|
+
fnCache.set(key, v);
|
|
133
|
-
|
|
138
|
+
setData(!data);
|
|
139
|
+
},
|
|
134
|
-
|
|
140
|
+
];
|
|
135
141
|
}
|
|
136
142
|
|
|
137
|
-
fnCache.set(
|
|
143
|
+
fnCache.set(
|
|
144
|
+
key,
|
|
145
|
+
fn(params)
|
|
138
|
-
|
|
146
|
+
.then(res => {
|
|
139
|
-
|
|
147
|
+
fnCache.set(key, res);
|
|
140
|
-
|
|
148
|
+
})
|
|
141
|
-
|
|
149
|
+
.catch(err => fnCache.set(key, err))
|
|
150
|
+
);
|
|
142
151
|
throw fnCache.get(key);
|
|
143
|
-
};
|
|
152
|
+
};
|
test/index.test.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { atom } from '../src';
|
|
2
2
|
|
|
3
3
|
describe('atom', () => {
|
|
4
|
-
|
|
5
4
|
it('getValue', () => {
|
|
6
5
|
const atomOne = atom(10);
|
|
7
6
|
const atomTwo = atom(20);
|
|
8
|
-
const sumAtom = atom(
|
|
7
|
+
const sumAtom = atom(get => get(atomOne) + get(atomTwo));
|
|
9
8
|
expect(atomOne.getValue()).toEqual(10);
|
|
10
9
|
expect(atomTwo.getValue()).toEqual(20);
|
|
11
10
|
expect(sumAtom.getValue()).toEqual(30);
|
|
@@ -13,27 +12,24 @@ describe('atom', () => {
|
|
|
13
12
|
|
|
14
13
|
it('update', () => {
|
|
15
14
|
const atomOne = atom(10);
|
|
16
|
-
const sumAtom = atom(
|
|
15
|
+
const sumAtom = atom(get => get(atomOne) + 5);
|
|
17
|
-
atomOne.update(
|
|
16
|
+
atomOne.update(v => v + 2);
|
|
18
17
|
expect(atomOne.getValue()).toEqual(12);
|
|
19
18
|
expect(sumAtom.getValue()).toEqual(17);
|
|
20
|
-
})
|
|
19
|
+
});
|
|
21
20
|
|
|
22
21
|
it('subscribe', async () => {
|
|
23
22
|
const atomOne = atom(10);
|
|
24
|
-
const sumAtom = atom(
|
|
23
|
+
const sumAtom = atom(get => get(atomOne) + 5);
|
|
25
|
-
atomOne.subscribe(
|
|
24
|
+
atomOne.subscribe(newValue => expect(newValue).toEqual(15));
|
|
26
|
-
sumAtom.subscribe(
|
|
25
|
+
sumAtom.subscribe(newValue => expect(newValue).toEqual(20));
|
|
27
|
-
atomOne.update(
|
|
26
|
+
atomOne.update(v => v + 5);
|
|
28
|
-
})
|
|
27
|
+
});
|
|
29
28
|
|
|
30
29
|
// it('useAtom', async () => {
|
|
31
30
|
// })
|
|
32
|
-
|
|
33
31
|
});
|
|
34
32
|
|
|
35
|
-
describe('asyncAtom', () => {
|
|
33
|
+
describe('asyncAtom', () => {});
|
|
36
|
-
})
|
|
37
34
|
|
|
38
|
-
describe('usePromise', () => {
|
|
35
|
+
describe('usePromise', () => {});
|
|
39
|
-
})
|