|
| 1 | +import type { AsyncStorage } from "./AsyncStorage"; |
| 2 | +import NativeAsyncStorage, { |
| 3 | + type Spec as NativeAsyncStorageSpec, |
| 4 | +} from "./native-module/NativeAsyncStorage"; |
| 5 | +import { AsyncStorageError } from "./AsyncStorageError"; |
| 6 | + |
| 7 | +class AsyncStorageImpl implements AsyncStorage { |
| 8 | + constructor(private readonly dbName: string) {} |
| 9 | + |
| 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 | + |
| 21 | + getItem = async (key: string): Promise<string | null> => { |
| 22 | + try { |
| 23 | + const result = await this.db.getValues(this.dbName, [key]); |
| 24 | + const value = result[0] ?? null; |
| 25 | + return value?.value ?? null; |
| 26 | + } catch (e) { |
| 27 | + throw AsyncStorageError.nativeError(e); |
| 28 | + } |
| 29 | + }; |
| 30 | + |
| 31 | + setItem = async (key: string, value: string): Promise<void> => { |
| 32 | + try { |
| 33 | + await this.db.setValues(this.dbName, [{ key, value }]); |
| 34 | + } catch (e) { |
| 35 | + throw AsyncStorageError.nativeError(e); |
| 36 | + } |
| 37 | + }; |
| 38 | + |
| 39 | + removeItem = async (key: string): Promise<void> => { |
| 40 | + try { |
| 41 | + await this.db.removeValues(this.dbName, [key]); |
| 42 | + } catch (e) { |
| 43 | + throw AsyncStorageError.nativeError(e); |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + getMany = async (keys: string[]): Promise<Record<string, string | null>> => { |
| 48 | + 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 | + ); |
| 55 | + } catch (e) { |
| 56 | + throw AsyncStorageError.nativeError(e); |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + setMany = async (entries: Record<string, string>): Promise<void> => { |
| 61 | + try { |
| 62 | + await this.db.setValues( |
| 63 | + this.dbName, |
| 64 | + Object.entries(entries).map(([key, value]) => ({ key, value })) |
| 65 | + ); |
| 66 | + } catch (e) { |
| 67 | + throw AsyncStorageError.nativeError(e); |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + removeMany = async (keys: string[]): Promise<void> => { |
| 72 | + try { |
| 73 | + await this.db.removeValues(this.dbName, keys); |
| 74 | + } catch (e) { |
| 75 | + throw AsyncStorageError.nativeError(e); |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + getAllKeys = async (): Promise<string[]> => { |
| 80 | + try { |
| 81 | + return await this.db.getKeys(this.dbName); |
| 82 | + } catch (e) { |
| 83 | + throw AsyncStorageError.nativeError(e); |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + clear = async (): Promise<void> => { |
| 88 | + try { |
| 89 | + return await this.db.clearStorage(this.dbName); |
| 90 | + } catch (e) { |
| 91 | + throw AsyncStorageError.nativeError(e); |
| 92 | + } |
| 93 | + }; |
| 94 | +} |
| 95 | + |
| 96 | +export function createAsyncStorage(databaseName: string): AsyncStorage { |
| 97 | + return new AsyncStorageImpl(databaseName); |
| 98 | +} |
0 commit comments