| 35 | import { strings } from "@notesnook/intl"; |
| 36 | |
| 37 | export class KV { |
| 38 | storage: MMKVInstance; |
| 39 | constructor(storage: MMKVInstance) { |
| 40 | this.storage = storage; |
| 41 | } |
| 42 | |
| 43 | async read<T>(key: string, isArray?: boolean) { |
| 44 | if (!key) return undefined; |
| 45 | const data = this.storage.getString(key); |
| 46 | if (!data) return undefined; |
| 47 | try { |
| 48 | return JSON.parse(data) as T; |
| 49 | } catch (e) { |
| 50 | return data as T; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | async write<T>(key: string, data: T) { |
| 55 | this.storage.setString( |
| 56 | key, |
| 57 | typeof data === "string" ? data : JSON.stringify(data) |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | async readMulti<T>(keys: string[]) { |
| 62 | if (keys.length <= 0) { |
| 63 | return []; |
| 64 | } else { |
| 65 | try { |
| 66 | const data = await this.storage.getMultipleItemsAsync<any>( |
| 67 | keys.slice(), |
| 68 | "string" |
| 69 | ); |
| 70 | return data.map(([key, value]) => { |
| 71 | let obj; |
| 72 | try { |
| 73 | obj = JSON.parse(value); |
| 74 | } catch (e) { |
| 75 | obj = value; |
| 76 | } |
| 77 | return [key, obj]; |
| 78 | }) as [string, T][]; |
| 79 | } catch (e) { |
| 80 | return []; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | async remove(key: string) { |
| 86 | this.storage.removeItem(key); |
| 87 | } |
| 88 | |
| 89 | async removeMulti(keys: string[]) { |
| 90 | if (!keys) return; |
| 91 | this.storage.removeItems(keys); |
| 92 | } |
| 93 | |
| 94 | async clear() { |
nothing calls this directly
no outgoing calls
no test coverage detected