~repos /atoms-state

#js#react#flux

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

Simple State management for react


95fe2b3e Peter John

3 years ago
fix stuff
Files changed (4) hide show
  1. index.d.ts +1 -1
  2. index.js +6 -5
  3. index.test.js +7 -7
  4. package.json +1 -1
index.d.ts CHANGED
@@ -43,7 +43,7 @@ export function useForm<T>(
43
43
  onSubmit: (d: T) => Promise<void>;
44
44
  };
45
45
 
46
- declare module 'Storage' {
46
+ declare namespace Storage {
47
47
  export function setItem<T>(key: string, value: T): boolean;
48
48
  export function getItem<T>(key: string): T;
49
49
  export function removeItem(key: string): void;
index.js CHANGED
@@ -139,11 +139,7 @@ export const Storage = {
139
139
  return item ? JSON.parse(item) : null;
140
140
  },
141
141
  async setItem(key, value) {
142
- if (value) {
143
- await AsyncStorage.setItem(key, JSON.stringify(value));
142
+ await AsyncStorage.setItem(key, JSON.stringify(value));
144
- } else {
145
- await AsyncStorage.removeItem(key);
146
- }
147
143
  if (this.listeners[key]) {
148
144
  for (const cb of this.listeners[key]) {
149
145
  cb(value);
@@ -152,6 +148,11 @@ export const Storage = {
152
148
  },
153
149
  async removeItem(key) {
154
150
  await AsyncStorage.removeItem(key);
151
+ if (this.listeners[key]) {
152
+ for (const cb of this.listeners[key]) {
153
+ cb(null);
154
+ }
155
+ }
155
156
  },
156
157
  async clear() {
157
158
  await AsyncStorage.clear();
index.test.js CHANGED
@@ -4,7 +4,7 @@ describe('atom', () => {
4
4
  it('getValue', () => {
5
5
  const atomOne = atom(10);
6
6
  const atomTwo = atom(20);
7
- const sumAtom = atom(get => get(atomOne) + get(atomTwo));
7
+ const sumAtom = atom((get) => get(atomOne) + get(atomTwo));
8
8
  expect(atomOne.getValue()).toEqual(10);
9
9
  expect(atomTwo.getValue()).toEqual(20);
10
10
  expect(sumAtom.getValue()).toEqual(30);
@@ -12,18 +12,18 @@ describe('atom', () => {
12
12
 
13
13
  it('update', () => {
14
14
  const atomOne = atom(10);
15
- const sumAtom = atom(get => get(atomOne) + 5);
15
+ const sumAtom = atom((get) => get(atomOne) + 5);
16
- atomOne.update(v => v + 2);
16
+ atomOne.update((v) => v + 2);
17
17
  expect(atomOne.getValue()).toEqual(12);
18
18
  expect(sumAtom.getValue()).toEqual(17);
19
19
  });
20
20
 
21
21
  it('subscribe', async () => {
22
22
  const atomOne = atom(10);
23
- const sumAtom = atom(get => get(atomOne) + 5);
23
+ const sumAtom = atom((get) => get(atomOne) + 5);
24
- atomOne.subscribe(newValue => expect(newValue).toEqual(15));
24
+ atomOne.subscribe((newValue) => expect(newValue).toEqual(15));
25
- sumAtom.subscribe(newValue => expect(newValue).toEqual(20));
25
+ sumAtom.subscribe((newValue) => expect(newValue).toEqual(20));
26
- atomOne.update(v => v + 5);
26
+ atomOne.update((v) => v + 5);
27
27
  });
28
28
 
29
29
  // it('useAtom', async () => {
package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "atoms-state",
3
3
  "author": "pyros.sh",
4
4
  "description": "State management and common hooks",
5
- "version": "0.7.0",
5
+ "version": "0.7.1",
6
6
  "license": "MIT",
7
7
  "type": "module",
8
8
  "main": "index.js",