|
| 1 | +import type { AsyncStorage } from "./AsyncStorage"; |
| 2 | +import { AsyncStorageError } from "./AsyncStorageError"; |
| 3 | +import { TurboModuleRegistry, type TurboModule } from "react-native"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Because of missing support for Windows native target by Room KMP, |
| 7 | + * Windows target uses v2 implementation of AsyncStorage. |
| 8 | + */ |
| 9 | +export function createAsyncStorage(_: string): AsyncStorage { |
| 10 | + return getLegacyStorage(); |
| 11 | +} |
| 12 | + |
| 13 | +export function getLegacyStorage(): AsyncStorage { |
| 14 | + return LegacyAsyncStorageImpl.instance; |
| 15 | +} |
| 16 | + |
| 17 | +class LegacyAsyncStorageImpl implements AsyncStorage { |
| 18 | + private constructor() {} |
| 19 | + |
| 20 | + static instance = new LegacyAsyncStorageImpl(); |
| 21 | + |
| 22 | + private get db(): NativeAsyncStorageSpec { |
| 23 | + const mod = RNCAsyncStorage; |
| 24 | + if (!mod) { |
| 25 | + throw AsyncStorageError.jsError( |
| 26 | + `Native module is null, cannot access storage.`, |
| 27 | + AsyncStorageError.Type.NativeModuleError |
| 28 | + ); |
| 29 | + } |
| 30 | + return mod; |
| 31 | + } |
| 32 | + |
| 33 | + getItem = async (key: string): Promise<string | null> => { |
| 34 | + try { |
| 35 | + return await new Promise((resolve, reject) => { |
| 36 | + this.db.multiGet([key], (errors, result) => { |
| 37 | + if (errors?.length) { |
| 38 | + const error = errors[0]!; |
| 39 | + reject( |
| 40 | + AsyncStorageError.jsError( |
| 41 | + error.message, |
| 42 | + AsyncStorageError.Type.OtherStorageError |
| 43 | + ) |
| 44 | + ); |
| 45 | + } else { |
| 46 | + resolve(result?.[0]?.[1] ?? null); |
| 47 | + } |
| 48 | + }); |
| 49 | + }); |
| 50 | + } catch (e) { |
| 51 | + throw AsyncStorageError.nativeError(e); |
| 52 | + } |
| 53 | + }; |
| 54 | + |
| 55 | + setItem = async (key: string, value: string): Promise<void> => { |
| 56 | + try { |
| 57 | + throw new Error("todo"); |
| 58 | + } catch (e) { |
| 59 | + throw AsyncStorageError.nativeError(e); |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + removeItem = async (key: string): Promise<void> => { |
| 64 | + try { |
| 65 | + throw new Error("todo"); |
| 66 | + } catch (e) { |
| 67 | + throw AsyncStorageError.nativeError(e); |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + getMany = async (keys: string[]): Promise<Record<string, string | null>> => { |
| 72 | + try { |
| 73 | + throw new Error("todo"); |
| 74 | + } catch (e) { |
| 75 | + throw AsyncStorageError.nativeError(e); |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + setMany = async (entries: Record<string, string>): Promise<void> => { |
| 80 | + try { |
| 81 | + throw new Error("todo"); |
| 82 | + } catch (e) { |
| 83 | + throw AsyncStorageError.nativeError(e); |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + removeMany = async (keys: string[]): Promise<void> => { |
| 88 | + try { |
| 89 | + throw new Error("todo"); |
| 90 | + } catch (e) { |
| 91 | + throw AsyncStorageError.nativeError(e); |
| 92 | + } |
| 93 | + }; |
| 94 | + |
| 95 | + getAllKeys = async (): Promise<string[]> => { |
| 96 | + try { |
| 97 | + throw new Error("todo"); |
| 98 | + } catch (e) { |
| 99 | + throw AsyncStorageError.nativeError(e); |
| 100 | + } |
| 101 | + }; |
| 102 | + |
| 103 | + clear = async (): Promise<void> => { |
| 104 | + try { |
| 105 | + throw new Error("todo"); |
| 106 | + } catch (e) { |
| 107 | + throw AsyncStorageError.nativeError(e); |
| 108 | + } |
| 109 | + }; |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * |
| 114 | + * This is a backward compatibility layer, to support Windows target in v3. |
| 115 | + * Room KMP does not support Windows native target currently, so shared-storage cannot be used. |
| 116 | + * https://issuetracker.google.com/issues/363195546 |
| 117 | + */ |
| 118 | +const RNCAsyncStorage = |
| 119 | + TurboModuleRegistry.get<NativeAsyncStorageSpec>("RNCAsyncStorage"); |
| 120 | + |
| 121 | +type ErrorLike = { |
| 122 | + message: string; |
| 123 | + key?: string; |
| 124 | +}; |
| 125 | + |
| 126 | +interface NativeAsyncStorageSpec extends TurboModule { |
| 127 | + multiGet: ( |
| 128 | + keys: string[], |
| 129 | + callback: (error?: ErrorLike[], result?: [string, string][]) => void |
| 130 | + ) => void; |
| 131 | + multiSet: ( |
| 132 | + kvPairs: [string, string][], |
| 133 | + callback: (error?: ErrorLike[]) => void |
| 134 | + ) => void; |
| 135 | + multiRemove: ( |
| 136 | + keys: readonly string[], |
| 137 | + callback: (error?: ErrorLike[]) => void |
| 138 | + ) => void; |
| 139 | + multiMerge: ( |
| 140 | + kvPairs: [string, string][], |
| 141 | + callback: (error?: ErrorLike[]) => void |
| 142 | + ) => void; |
| 143 | + getAllKeys: ( |
| 144 | + callback: (error?: ErrorLike[], result?: [string, string][]) => void |
| 145 | + ) => void; |
| 146 | + clear: (callback: (error?: ErrorLike[]) => void) => void; |
| 147 | +} |
0 commit comments