[Types] Type MMKVManager

This commit is contained in:
redstonekasi 2023-01-17 00:52:20 +01:00 committed by Beef
parent 3c95bb1024
commit 71f687e345
2 changed files with 38 additions and 4 deletions

34
src/def.d.ts vendored
View file

@ -81,6 +81,40 @@ interface RNConstants extends _RN.PlatformConstants {
systemName: string; systemName: string;
} }
/**
* A key-value storage based upon `SharedPreferences` on Android.
*
* These types are based on Android though everything should be the same between
* platforms.
*/
interface MMKVManager {
/**
* Get the value for the given `key`, or null
* @param key The key to fetch
*/
getItem: (key: string) => Promise<string | null>;
/**
* Deletes the value for the given `key`
* @param key The key to delete
*/
removeItem: (key: string) => void;
/**
* Sets the value of `key` to `value`
*/
setItem: (key: string, value: string) => void;
/**
* Goes through every item in storage and returns it, excluding the
* keys specified in `exclude`.
* @param exclude A list of items to exclude from result
*/
refresh: (exclude: string[]) => Promise<Record<string, string>>;
/**
* You will be murdered if you use this function.
* Clears ALL of Discord's settings.
*/
clear: () => void;
}
type Indexable<Type> = { [index: string]: Type } type Indexable<Type> = { [index: string]: Type }
interface VendettaObject { interface VendettaObject {

View file

@ -1,9 +1,9 @@
import { Indexable } from "@types"; import { Indexable, MMKVManager } from "@types";
import { ReactNative as RN } from "@metro/hoist"; import { ReactNative as RN } from "@metro/hoist";
// Discord's custom special storage sauce // Discord's custom special storage sauce
// TODO: Type this // TODO: Type this
const MMKVManager = RN.NativeModules.MMKVManager; const MMKVManager = RN.NativeModules.MMKVManager as MMKVManager;
// TODO: React hook? // TODO: React hook?
// TODO: Clean up types, if necessary // TODO: Clean up types, if necessary
@ -34,7 +34,7 @@ export default function createStorage<T>(storeName: string, onRestore?: (parsed:
} }
} }
MMKVManager.getItem(storeName).then(async function (v: any) { MMKVManager.getItem(storeName).then(async function (v) {
if (!v) return; if (!v) return;
const parsed: T & Indexable<any> = JSON.parse(v); const parsed: T & Indexable<any> = JSON.parse(v);
@ -46,4 +46,4 @@ export default function createStorage<T>(storeName: string, onRestore?: (parsed:
}) })
return new Proxy(internalStore, proxyValidator) as T; return new Proxy(internalStore, proxyValidator) as T;
} }