[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

@ -14,7 +14,6 @@
"author": "Beef", "author": "Beef",
"license": "BSD-3-Clause", "license": "BSD-3-Clause",
"devDependencies": { "devDependencies": {
"@react-native-async-storage/async-storage": "^1.17.11",
"@types/react": "18.0.26", "@types/react": "18.0.26",
"@types/react-native": "0.68.1", "@types/react-native": "0.68.1",
"esbuild": "^0.16.17", "esbuild": "^0.16.17",

View file

@ -4,7 +4,6 @@ overrides:
'@types/react': 18.0.26 '@types/react': 18.0.26
specifiers: specifiers:
'@react-native-async-storage/async-storage': ^1.17.11
'@types/react': 18.0.26 '@types/react': 18.0.26
'@types/react-native': 0.68.1 '@types/react-native': 0.68.1
esbuild: ^0.16.17 esbuild: ^0.16.17
@ -18,7 +17,6 @@ dependencies:
spitroast: 1.4.2 spitroast: 1.4.2
devDependencies: devDependencies:
'@react-native-async-storage/async-storage': 1.17.11
'@types/react': 18.0.26 '@types/react': 18.0.26
'@types/react-native': 0.68.1 '@types/react-native': 0.68.1
esbuild: 0.16.17 esbuild: 0.16.17
@ -227,17 +225,6 @@ packages:
dev: true dev: true
optional: true optional: true
/@react-native-async-storage/async-storage/1.17.11:
resolution: {integrity: sha512-bzs45n5HNcDq6mxXnSsOHysZWn1SbbebNxldBXCQs8dSvF8Aor9KCdpm+TpnnGweK3R6diqsT8lFhX77VX0NFw==}
peerDependencies:
react-native: ^0.0.0-0 || 0.60 - 0.71 || 1000.0.0
peerDependenciesMeta:
react-native:
optional: true
dependencies:
merge-options: 3.0.4
dev: true
/@types/prop-types/15.7.5: /@types/prop-types/15.7.5:
resolution: {integrity: sha512-JCB8C6SnDoQf0cNycqd/35A7MjcnK+ZTqE7judS6o7utxUCg6imJg3QK2qzHKszlTjcj2cn+NwMB2i96ubpj7w==} resolution: {integrity: sha512-JCB8C6SnDoQf0cNycqd/35A7MjcnK+ZTqE7judS6o7utxUCg6imJg3QK2qzHKszlTjcj2cn+NwMB2i96ubpj7w==}
dev: true dev: true
@ -308,24 +295,12 @@ packages:
'@esbuild/win32-x64': 0.16.17 '@esbuild/win32-x64': 0.16.17
dev: true dev: true
/is-plain-obj/2.1.0:
resolution: {integrity: sha512-YWnfyRwxL/+SsrWYfOpUtz5b3YD+nyfkHvjbcanzk8zgyO4ASD67uVMRt8k5bM4lLMDnXfriRhOpemw+NfT1eA==}
engines: {node: '>=8'}
dev: true
/magic-string/0.25.9: /magic-string/0.25.9:
resolution: {integrity: sha512-RmF0AsMzgt25qzqqLc1+MbHmhdx0ojF2Fvs4XnOqz2ZOBXzzkEwc/dJQZCYHAn7v1jbVOjAZfK8msRn4BxO4VQ==} resolution: {integrity: sha512-RmF0AsMzgt25qzqqLc1+MbHmhdx0ojF2Fvs4XnOqz2ZOBXzzkEwc/dJQZCYHAn7v1jbVOjAZfK8msRn4BxO4VQ==}
dependencies: dependencies:
sourcemap-codec: 1.4.8 sourcemap-codec: 1.4.8
dev: true dev: true
/merge-options/3.0.4:
resolution: {integrity: sha512-2Sug1+knBjkaMsMgf1ctR1Ujx+Ayku4EdJN4Z+C2+JzoeF7A3OZ9KM2GY0CpQS51NR61LTurMJrRKPhSs3ZRTQ==}
engines: {node: '>=10'}
dependencies:
is-plain-obj: 2.1.0
dev: true
/sourcemap-codec/1.4.8: /sourcemap-codec/1.4.8:
resolution: {integrity: sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA==} resolution: {integrity: sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA==}
deprecated: Please use @jridgewell/sourcemap-codec instead deprecated: Please use @jridgewell/sourcemap-codec instead

2
src/def.d.ts vendored
View file

@ -1,7 +1,6 @@
import * as _spitroast from "spitroast"; import * as _spitroast from "spitroast";
import _React from "react"; import _React from "react";
import _RN from "react-native"; import _RN from "react-native";
import _AsyncStorage from "@react-native-async-storage/async-storage";
type MetroModules = { [id: number]: any }; type MetroModules = { [id: number]: any };
@ -104,7 +103,6 @@ interface VendettaObject {
toasts: PropIntellisense<"open" | "close">; toasts: PropIntellisense<"open" | "close">;
React: typeof _React; React: typeof _React;
ReactNative: typeof _RN; ReactNative: typeof _RN;
AsyncStorage: typeof _AsyncStorage;
}; };
}; };
constants: { constants: {

View file

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

View file

@ -1,4 +1,5 @@
import { MetroModules, PropsFinder, PropsFinderAll } from "@types"; import { MetroModules, PropsFinder, PropsFinderAll } from "@types";
import { moment } from "@metro/hoist";
// Metro require // Metro require
declare const __r: (moduleId: number) => any; 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 // Function to filter through modules
export const filterModules = (modules: MetroModules, single = false) => (filter: (m: any) => boolean) => { export const filterModules = (modules: MetroModules, single = false) => (filter: (m: any) => boolean) => {
const found = []; 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 { 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: React hook?
// TODO: Clean up types, if necessary // 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) { set(target: object, key: string | symbol, value: any) {
Reflect.set(target, key, value); Reflect.set(target, key, value);
AsyncStorage.setItem(storeName, JSON.stringify(internalStore)); MMKVManager.setItem(storeName, JSON.stringify(internalStore));
return true; return true;
}, },
deleteProperty(target: object, key: string | symbol) { deleteProperty(target: object, key: string | symbol) {
Reflect.deleteProperty(target, key); Reflect.deleteProperty(target, key);
AsyncStorage.setItem(storeName, JSON.stringify(internalStore)); MMKVManager.setItem(storeName, JSON.stringify(internalStore));
return true; return true;
} }
} }
AsyncStorage.getItem(storeName).then(async function (v) { MMKVManager.getItem(storeName).then(async function (v: any) {
if (!v) return; if (!v) return;
const parsed: T & Indexable<any> = JSON.parse(v); const parsed: T & Indexable<any> = JSON.parse(v);