~repos /atoms-state

#js#react#flux

git clone https://pyrossh.dev/repos/atoms-state.git

Simple State management for react


22fa1eb0 pyros2097

4 years ago
fix lint
Files changed (4) hide show
  1. package.json +5 -1
  2. src/index.ts +30 -21
  3. test/index.test.ts +11 -15
  4. yarn.lock +58 -31
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((sub) => {
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((sub) => {
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((value) => {
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((res) => {
74
+ .then(res => {
75
75
  value = res;
76
76
  })
77
- .catch((err) => {
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((sub) => {
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((v) => !v))
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
- export const usePromise = <S, P>(fn: PromiseFunc<S, P>, params: P): [S | null, (v: S) => void] => {
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
- return [value, (v) => {
136
+ v => {
132
- fnCache.set(key, v)
137
+ fnCache.set(key, v);
133
- setData(!data);
138
+ setData(!data);
139
+ },
134
- }];
140
+ ];
135
141
  }
136
142
 
137
- fnCache.set(key, fn(params)
143
+ fnCache.set(
144
+ key,
145
+ fn(params)
138
- .then((res) => {
146
+ .then(res => {
139
- fnCache.set(key, res)
147
+ fnCache.set(key, res);
140
- })
148
+ })
141
- .catch((err) => fnCache.set(key, err)))
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((get) => get(atomOne) + get(atomTwo));
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((get) => get(atomOne) + 5);
15
+ const sumAtom = atom(get => get(atomOne) + 5);
17
- atomOne.update((v) => v + 2)
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((get) => get(atomOne) + 5);
23
+ const sumAtom = atom(get => get(atomOne) + 5);
25
- atomOne.subscribe((newValue) => expect(newValue).toEqual(15));
24
+ atomOne.subscribe(newValue => expect(newValue).toEqual(15));
26
- sumAtom.subscribe((newValue) => expect(newValue).toEqual(20));
25
+ sumAtom.subscribe(newValue => expect(newValue).toEqual(20));
27
- atomOne.update((v) => v + 5)
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
- })
yarn.lock CHANGED
@@ -1265,11 +1265,6 @@
1265
1265
  "@types/node" "*"
1266
1266
  "@types/responselike" "*"
1267
1267
 
1268
- "@types/eslint-visitor-keys@^1.0.0":
1269
- version "1.0.0"
1270
- resolved "https://registry.yarnpkg.com/@types/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#1ee30d79544ca84d68d4b3cdb0af4f205663dd2d"
1271
- integrity sha512-OCutwjDZ4aFS6PB1UZ988C4YgwlBHJd6wCeQqaLdmadZ/7e+w79+hbMUFC1QXDNCmdyoRfAFdm0RypzwR+Qpag==
1272
-
1273
1268
  "@types/estree@*":
1274
1269
  version "0.0.47"
1275
1270
  resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.47.tgz#d7a51db20f0650efec24cd04994f523d93172ed4"
@@ -1411,49 +1406,76 @@
1411
1406
  dependencies:
1412
1407
  "@types/yargs-parser" "*"
1413
1408
 
1414
- "@typescript-eslint/eslint-plugin@^2.12.0":
1409
+ "@typescript-eslint/eslint-plugin@^2.12.0", "@typescript-eslint/eslint-plugin@^4.1.1":
1415
- version "2.34.0"
1410
+ version "4.20.0"
1416
- resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-2.34.0.tgz#6f8ce8a46c7dea4a6f1d171d2bb8fbae6dac2be9"
1411
+ resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-4.20.0.tgz#9d8794bd99aad9153092ad13c96164e3082e9a92"
1417
- integrity sha512-4zY3Z88rEE99+CNvTbXSyovv2z9PNOVffTWD2W8QF5s2prBQtwN2zadqERcrHpcR7O/+KMI3fcTAmUUhK/iQcQ==
1412
+ integrity sha512-sw+3HO5aehYqn5w177z2D82ZQlqHCwcKSMboueo7oE4KU9QiC0SAgfS/D4z9xXvpTc8Bt41Raa9fBR8T2tIhoQ==
1418
1413
  dependencies:
1419
- "@typescript-eslint/experimental-utils" "2.34.0"
1414
+ "@typescript-eslint/experimental-utils" "4.20.0"
1415
+ "@typescript-eslint/scope-manager" "4.20.0"
1416
+ debug "^4.1.1"
1420
1417
  functional-red-black-tree "^1.0.1"
1418
+ lodash "^4.17.15"
1421
1419
  regexpp "^3.0.0"
1420
+ semver "^7.3.2"
1422
1421
  tsutils "^3.17.1"
1423
1422
 
1424
- "@typescript-eslint/experimental-utils@2.34.0":
1423
+ "@typescript-eslint/experimental-utils@4.20.0":
1425
- version "2.34.0"
1424
+ version "4.20.0"
1426
- resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-2.34.0.tgz#d3524b644cdb40eebceca67f8cf3e4cc9c8f980f"
1425
+ resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-4.20.0.tgz#a8ab2d7b61924f99042b7d77372996d5f41dc44b"
1427
- integrity sha512-eS6FTkq+wuMJ+sgtuNTtcqavWXqsflWcfBnlYhg/nS4aZ1leewkXGbvBhaapn1q6qf4M71bsR1tez5JTRMuqwA==
1426
+ integrity sha512-sQNlf6rjLq2yB5lELl3gOE7OuoA/6IVXJUJ+Vs7emrQMva14CkOwyQwD7CW+TkmOJ4Q/YGmoDLmbfFrpGmbKng==
1428
1427
  dependencies:
1429
1428
  "@types/json-schema" "^7.0.3"
1429
+ "@typescript-eslint/scope-manager" "4.20.0"
1430
+ "@typescript-eslint/types" "4.20.0"
1430
- "@typescript-eslint/typescript-estree" "2.34.0"
1431
+ "@typescript-eslint/typescript-estree" "4.20.0"
1431
1432
  eslint-scope "^5.0.0"
1432
1433
  eslint-utils "^2.0.0"
1433
1434
 
1434
- "@typescript-eslint/parser@^2.12.0":
1435
+ "@typescript-eslint/parser@^2.12.0", "@typescript-eslint/parser@^4.1.1":
1435
- version "2.34.0"
1436
+ version "4.20.0"
1436
- resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-2.34.0.tgz#50252630ca319685420e9a39ca05fe185a256bc8"
1437
+ resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-4.20.0.tgz#8dd403c8b4258b99194972d9799e201b8d083bdd"
1437
- integrity sha512-03ilO0ucSD0EPTw2X4PntSIRFtDPWjrVq7C3/Z3VQHRC7+13YB55rcJI3Jt+YgeHbjUdJPcPa7b23rXCBokuyA==
1438
+ integrity sha512-m6vDtgL9EABdjMtKVw5rr6DdeMCH3OA1vFb0dAyuZSa3e5yw1YRzlwFnm9knma9Lz6b2GPvoNSa8vOXrqsaglA==
1438
1439
  dependencies:
1439
- "@types/eslint-visitor-keys" "^1.0.0"
1440
- "@typescript-eslint/experimental-utils" "2.34.0"
1440
+ "@typescript-eslint/scope-manager" "4.20.0"
1441
+ "@typescript-eslint/types" "4.20.0"
1441
- "@typescript-eslint/typescript-estree" "2.34.0"
1442
+ "@typescript-eslint/typescript-estree" "4.20.0"
1442
- eslint-visitor-keys "^1.1.0"
1443
+ debug "^4.1.1"
1443
1444
 
1444
- "@typescript-eslint/typescript-estree@2.34.0":
1445
+ "@typescript-eslint/scope-manager@4.20.0":
1445
- version "2.34.0"
1446
+ version "4.20.0"
1446
- resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-2.34.0.tgz#14aeb6353b39ef0732cc7f1b8285294937cf37d5"
1447
+ resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-4.20.0.tgz#953ecbf3b00845ece7be66246608be9d126d05ca"
1447
- integrity sha512-OMAr+nJWKdlVM9LOqCqh3pQQPwxHAN7Du8DR6dmwCrAmxtiXQnhHJ6tBNtf+cggqfo51SG/FCwnKhXCIM7hnVg==
1448
+ integrity sha512-/zm6WR6iclD5HhGpcwl/GOYDTzrTHmvf8LLLkwKqqPKG6+KZt/CfSgPCiybshmck66M2L5fWSF/MKNuCwtKQSQ==
1448
1449
  dependencies:
1450
+ "@typescript-eslint/types" "4.20.0"
1451
+ "@typescript-eslint/visitor-keys" "4.20.0"
1452
+
1453
+ "@typescript-eslint/types@4.20.0":
1454
+ version "4.20.0"
1455
+ resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.20.0.tgz#c6cf5ef3c9b1c8f699a9bbdafb7a1da1ca781225"
1456
+ integrity sha512-cYY+1PIjei1nk49JAPnH1VEnu7OYdWRdJhYI5wiKOUMhLTG1qsx5cQxCUTuwWCmQoyriadz3Ni8HZmGSofeC+w==
1457
+
1458
+ "@typescript-eslint/typescript-estree@4.20.0":
1459
+ version "4.20.0"
1460
+ resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-4.20.0.tgz#8b3b08f85f18a8da5d88f65cb400f013e88ab7be"
1461
+ integrity sha512-Knpp0reOd4ZsyoEJdW8i/sK3mtZ47Ls7ZHvD8WVABNx5Xnn7KhenMTRGegoyMTx6TiXlOVgMz9r0pDgXTEEIHA==
1462
+ dependencies:
1463
+ "@typescript-eslint/types" "4.20.0"
1464
+ "@typescript-eslint/visitor-keys" "4.20.0"
1449
1465
  debug "^4.1.1"
1450
- eslint-visitor-keys "^1.1.0"
1451
- glob "^7.1.6"
1466
+ globby "^11.0.1"
1452
1467
  is-glob "^4.0.1"
1453
- lodash "^4.17.15"
1454
1468
  semver "^7.3.2"
1455
1469
  tsutils "^3.17.1"
1456
1470
 
1471
+ "@typescript-eslint/visitor-keys@4.20.0":
1472
+ version "4.20.0"
1473
+ resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-4.20.0.tgz#1e84db034da13f208325e6bfc995c3b75f7dbd62"
1474
+ integrity sha512-NXKRM3oOVQL8yNFDNCZuieRIwZ5UtjNLYtmMx2PacEAGmbaEYtGgVHUHVyZvU/0rYZcizdrWjDo+WBtRPSgq+A==
1475
+ dependencies:
1476
+ "@typescript-eslint/types" "4.20.0"
1477
+ eslint-visitor-keys "^2.0.0"
1478
+
1457
1479
  "@webassemblyjs/ast@1.9.0":
1458
1480
  version "1.9.0"
1459
1481
  resolved "https://registry.yarnpkg.com/@webassemblyjs/ast/-/ast-1.9.0.tgz#bd850604b4042459a5a41cd7d338cbed695ed964"
@@ -3755,6 +3777,11 @@ eslint-visitor-keys@^1.0.0, eslint-visitor-keys@^1.1.0:
3755
3777
  resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-1.3.0.tgz#30ebd1ef7c2fdff01c3a4f151044af25fab0523e"
3756
3778
  integrity sha512-6J72N8UNa462wa/KFODt/PJ3IU60SDpC3QXC1Hjc1BXXpfL2C9R5+AU7jhe0F6GREqVMh4Juu+NY7xn+6dipUQ==
3757
3779
 
3780
+ eslint-visitor-keys@^2.0.0:
3781
+ version "2.0.0"
3782
+ resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-2.0.0.tgz#21fdc8fbcd9c795cc0321f0563702095751511a8"
3783
+ integrity sha512-QudtT6av5WXels9WjIM7qz1XD1cWGvX4gGXvp/zBn9nXG02D0utdU3Em2m/QjTnrsk6bBjmCygl3rmj118msQQ==
3784
+
3758
3785
  eslint@^6.1.0:
3759
3786
  version "6.8.0"
3760
3787
  resolved "https://registry.yarnpkg.com/eslint/-/eslint-6.8.0.tgz#62262d6729739f9275723824302fb227c8c93ffb"
@@ -4362,7 +4389,7 @@ glob-parent@^5.0.0, glob-parent@^5.1.0, glob-parent@~5.1.0:
4362
4389
  dependencies:
4363
4390
  is-glob "^4.0.1"
4364
4391
 
4365
- glob@^7.0.0, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4, glob@^7.1.6:
4392
+ glob@^7.0.0, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4:
4366
4393
  version "7.1.6"
4367
4394
  resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.6.tgz#141f33b81a7c2492e125594307480c46679278a6"
4368
4395
  integrity sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==