Skip to content

Commit 2486865

Browse files
committed
feat(async-storage): web storage implementation
1 parent d4145fe commit 2486865

File tree

1 file changed

+14
-34
lines changed

1 file changed

+14
-34
lines changed
Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,78 @@
11
import type { AsyncStorage } from "./AsyncStorage";
2-
import NativeAsyncStorage, {
3-
type Spec as NativeAsyncStorageSpec,
4-
} from "./native-module/NativeAsyncStorage";
52
import { AsyncStorageError } from "./AsyncStorageError";
63

7-
class AsyncStorageImpl implements AsyncStorage {
4+
class AsyncStorageWebImpl implements AsyncStorage {
85
constructor(private readonly dbName: string) {}
96

10-
private get db(): NativeAsyncStorageSpec {
11-
const mod = NativeAsyncStorage;
12-
if (!mod) {
13-
throw AsyncStorageError.jsError(
14-
`Native module is null, cannot create db`,
15-
AsyncStorageError.Type.NativeModuleError
16-
);
17-
}
18-
return mod;
19-
}
20-
217
getItem = async (key: string): Promise<string | null> => {
228
try {
23-
const result = await this.db.getValues(this.dbName, [key]);
24-
const value = result[0] ?? null;
25-
return value?.value ?? null;
9+
// todo:
10+
return null;
2611
} catch (e) {
2712
throw AsyncStorageError.nativeError(e);
2813
}
2914
};
3015

3116
setItem = async (key: string, value: string): Promise<void> => {
3217
try {
33-
await this.db.setValues(this.dbName, [{ key, value }]);
18+
// todo
3419
} catch (e) {
3520
throw AsyncStorageError.nativeError(e);
3621
}
3722
};
3823

3924
removeItem = async (key: string): Promise<void> => {
4025
try {
41-
await this.db.removeValues(this.dbName, [key]);
26+
// todo
4227
} catch (e) {
4328
throw AsyncStorageError.nativeError(e);
4429
}
4530
};
4631

4732
getMany = async (keys: string[]): Promise<Record<string, string | null>> => {
4833
try {
49-
return await this.db.getValues(this.dbName, keys).then((entries) =>
50-
entries.reduce<Record<string, string | null>>((values, current) => {
51-
values[current.key] = current.value;
52-
return values;
53-
}, {})
54-
);
34+
// todo
35+
return {};
5536
} catch (e) {
5637
throw AsyncStorageError.nativeError(e);
5738
}
5839
};
5940

6041
setMany = async (entries: Record<string, string>): Promise<void> => {
6142
try {
62-
await this.db.setValues(
63-
this.dbName,
64-
Object.entries(entries).map(([key, value]) => ({ key, value }))
65-
);
43+
// todo
6644
} catch (e) {
6745
throw AsyncStorageError.nativeError(e);
6846
}
6947
};
7048

7149
removeMany = async (keys: string[]): Promise<void> => {
7250
try {
73-
await this.db.removeValues(this.dbName, keys);
51+
// todo
7452
} catch (e) {
7553
throw AsyncStorageError.nativeError(e);
7654
}
7755
};
7856

7957
getAllKeys = async (): Promise<string[]> => {
8058
try {
81-
return await this.db.getKeys(this.dbName);
59+
// todo
60+
return [];
8261
} catch (e) {
8362
throw AsyncStorageError.nativeError(e);
8463
}
8564
};
8665

8766
clear = async (): Promise<void> => {
8867
try {
89-
return await this.db.clearStorage(this.dbName);
68+
// return await this.db.clearStorage(this.dbName);
69+
// todo
9070
} catch (e) {
9171
throw AsyncStorageError.nativeError(e);
9272
}
9373
};
9474
}
9575

9676
export function createAsyncStorage(databaseName: string): AsyncStorage {
97-
return new AsyncStorageImpl(databaseName);
77+
return new AsyncStorageWebImpl(databaseName);
9878
}

0 commit comments

Comments
 (0)