|
1 | 1 | import type { AsyncStorage } from "./AsyncStorage"; |
2 | 2 | import { AsyncStorageError } from "./AsyncStorageError"; |
| 3 | +import IndexedDBStorage from "./web-module/IndexedDBStorage"; |
3 | 4 |
|
4 | 5 | class AsyncStorageWebImpl implements AsyncStorage { |
5 | | - constructor(private readonly dbName: string) {} |
| 6 | + private db: IndexedDBStorage; |
| 7 | + |
| 8 | + constructor(dbName: string) { |
| 9 | + this.db = new IndexedDBStorage(dbName); |
| 10 | + } |
6 | 11 |
|
7 | 12 | getItem = async (key: string): Promise<string | null> => { |
8 | 13 | try { |
9 | | - // todo: |
10 | | - return null; |
| 14 | + const result = await this.db.getValues([key]); |
| 15 | + const value = result?.[0] ?? null; |
| 16 | + return value?.value ?? null; |
11 | 17 | } catch (e) { |
12 | | - throw AsyncStorageError.nativeError(e); |
| 18 | + throw this.createError(e); |
13 | 19 | } |
14 | 20 | }; |
15 | 21 |
|
16 | 22 | setItem = async (key: string, value: string): Promise<void> => { |
17 | 23 | try { |
18 | | - // todo |
| 24 | + await this.db.setValues([{ key, value }]); |
19 | 25 | } catch (e) { |
20 | | - throw AsyncStorageError.nativeError(e); |
| 26 | + throw this.createError(e); |
21 | 27 | } |
22 | 28 | }; |
23 | 29 |
|
24 | 30 | removeItem = async (key: string): Promise<void> => { |
25 | 31 | try { |
26 | | - // todo |
| 32 | + await this.db.removeValues([key]); |
27 | 33 | } catch (e) { |
28 | | - throw AsyncStorageError.nativeError(e); |
| 34 | + throw this.createError(e); |
29 | 35 | } |
30 | 36 | }; |
31 | 37 |
|
32 | 38 | getMany = async (keys: string[]): Promise<Record<string, string | null>> => { |
33 | 39 | try { |
34 | | - // todo |
35 | | - return {}; |
| 40 | + return await this.db.getValues(keys).then((entries) => |
| 41 | + entries.reduce<Record<string, string | null>>((values, current) => { |
| 42 | + values[current.key] = current.value; |
| 43 | + return values; |
| 44 | + }, {}) |
| 45 | + ); |
36 | 46 | } catch (e) { |
37 | | - throw AsyncStorageError.nativeError(e); |
| 47 | + throw this.createError(e); |
38 | 48 | } |
39 | 49 | }; |
40 | 50 |
|
41 | 51 | setMany = async (entries: Record<string, string>): Promise<void> => { |
42 | 52 | try { |
43 | | - // todo |
| 53 | + await this.db.setValues( |
| 54 | + Object.entries(entries).map(([key, value]) => ({ key, value })) |
| 55 | + ); |
44 | 56 | } catch (e) { |
45 | | - throw AsyncStorageError.nativeError(e); |
| 57 | + throw this.createError(e); |
46 | 58 | } |
47 | 59 | }; |
48 | 60 |
|
49 | 61 | removeMany = async (keys: string[]): Promise<void> => { |
50 | 62 | try { |
51 | | - // todo |
| 63 | + await this.db.removeValues(keys); |
52 | 64 | } catch (e) { |
53 | | - throw AsyncStorageError.nativeError(e); |
| 65 | + throw this.createError(e); |
54 | 66 | } |
55 | 67 | }; |
56 | 68 |
|
57 | 69 | getAllKeys = async (): Promise<string[]> => { |
58 | 70 | try { |
59 | | - // todo |
60 | | - return []; |
| 71 | + return await this.db.getKeys(); |
61 | 72 | } catch (e) { |
62 | | - throw AsyncStorageError.nativeError(e); |
| 73 | + throw this.createError(e); |
63 | 74 | } |
64 | 75 | }; |
65 | 76 |
|
66 | 77 | clear = async (): Promise<void> => { |
67 | 78 | try { |
68 | | - // return await this.db.clearStorage(this.dbName); |
69 | | - // todo |
| 79 | + return await this.db.clearStorage(); |
70 | 80 | } catch (e) { |
71 | | - throw AsyncStorageError.nativeError(e); |
| 81 | + throw this.createError(e); |
72 | 82 | } |
73 | 83 | }; |
| 84 | + |
| 85 | + private createError(e: any): AsyncStorageError { |
| 86 | + if (e instanceof AsyncStorageError) { |
| 87 | + return e; |
| 88 | + } |
| 89 | + return AsyncStorageError.jsError( |
| 90 | + e?.message ?? `Web storage error: ${e}`, |
| 91 | + AsyncStorageError.Type.WebStorageError |
| 92 | + ); |
| 93 | + } |
74 | 94 | } |
75 | 95 |
|
76 | 96 | export function createAsyncStorage(databaseName: string): AsyncStorage { |
|
0 commit comments