[Storage] Move to MMKVManager

This commit is contained in:
Beef 2023-01-16 22:52:23 +00:00
parent af936940a0
commit 818573a228
7 changed files with 25 additions and 46 deletions

View file

@ -18,8 +18,5 @@ export const Flux = findByProps("connectStores");
export const FluxDispatcher = findByProps("_currentDispatchActionType");
// React
export const React = findByProps("createElement") as typeof import("react");
export const ReactNative = findByProps("Text", "Image") as typeof import("react-native");
// AsyncStorage
export const AsyncStorage = findByProps("setItem") as typeof import("@react-native-async-storage/async-storage").default;
export const React = window.React as typeof import("react");
export { ReactNative } from "@metro/hoist";

View file

@ -1,4 +1,5 @@
import { MetroModules, PropsFinder, PropsFinderAll } from "@types";
import { moment } from "@metro/hoist";
// Metro require
declare const __r: (moduleId: number) => any;
@ -24,15 +25,6 @@ for (const key in window.modules) {
}
}
// Early find logic
const basicFind = (prop: string) => Object.values(window.modules).find(m => m.publicModule.exports?.[prop]).publicModule.exports;
// Hoist React
window.React = basicFind("createElement");
// Find moment
let moment = basicFind("isMoment");
// Function to filter through modules
export const filterModules = (modules: MetroModules, single = false) => (filter: (m: any) => boolean) => {
const found = [];

14
src/lib/metro/hoist.ts Normal file
View file

@ -0,0 +1,14 @@
// Hoist required modules
// This used to be in filters.ts, but things became convoluted
// Early find logic
const basicFind = (prop: string) => Object.values(window.modules).find(m => m.publicModule.exports?.[prop]).publicModule.exports;
// Hoist React on window
window.React = basicFind("createElement") as typeof import("react");;
// Export ReactNative
export const ReactNative = basicFind("Text") as typeof import("react-native");
// Export moment
export const moment = basicFind("isMoment");

View file

@ -1,5 +1,9 @@
import { Indexable } from "@types";
import { AsyncStorage } from "@metro/common";
import { ReactNative as RN } from "@metro/hoist";
// Discord's custom special storage sauce
// TODO: Type this
const MMKVManager = RN.NativeModules.MMKVManager;
// TODO: React hook?
// TODO: Clean up types, if necessary
@ -19,18 +23,18 @@ export default function createStorage<T>(storeName: string, onRestore?: (parsed:
set(target: object, key: string | symbol, value: any) {
Reflect.set(target, key, value);
AsyncStorage.setItem(storeName, JSON.stringify(internalStore));
MMKVManager.setItem(storeName, JSON.stringify(internalStore));
return true;
},
deleteProperty(target: object, key: string | symbol) {
Reflect.deleteProperty(target, key);
AsyncStorage.setItem(storeName, JSON.stringify(internalStore));
MMKVManager.setItem(storeName, JSON.stringify(internalStore));
return true;
}
}
AsyncStorage.getItem(storeName).then(async function (v) {
MMKVManager.getItem(storeName).then(async function (v: any) {
if (!v) return;
const parsed: T & Indexable<any> = JSON.parse(v);