[Build] SWC-ify, allow classes and async arrow functions (#24)

* [Build] SWC-ify, allow classes and async arrow functions

* [Build] The SWC-ening is upon us.

* [Build] Tidy

* [Global] Make use of our newfound powers

* [Build] Call bundle.close when done

---------

Co-authored-by: Beef <beefers@riseup.net>
This commit is contained in:
redstonekasi 2023-02-26 01:34:16 +01:00 committed by GitHub
parent 6ef8c97b86
commit 8354b3806c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 86 additions and 79 deletions

View file

@ -10,7 +10,7 @@ import logger from "@lib/logger";
// This logs in the native logging implementation, e.g. logcat
console.log("Hello from Vendetta!");
async function init() {
(async () => {
try {
// Load everything in parallel
const unloads = await Promise.all([
@ -32,6 +32,4 @@ async function init() {
} catch (e: any) {
alert(`Vendetta failed to initialize... ${e.stack || e.toString()}`);
}
};
init();
})();

View file

@ -5,26 +5,23 @@ const MMKVManager = RN.NativeModules.MMKVManager as MMKVManager;
const DCDFileManager = RN.NativeModules.DCDFileManager as DCDFileManager;
const filePathFixer: (file: string) => string = RN.Platform.select({
default: (f) => f,
ios: (f) => `Documents/${f}`,
default: (f) => f,
ios: (f) => `Documents/${f}`,
});
export const createMMKVBackend = (store: string): StorageBackend => ({
get: async function() {
return JSON.parse((await MMKVManager.getItem(store)) ?? "{}");
},
set: (data) => MMKVManager.setItem(store, JSON.stringify(data)),
get: async () => JSON.parse((await MMKVManager.getItem(store)) ?? "{}"),
set: (data) => MMKVManager.setItem(store, JSON.stringify(data)),
});
export const createFileBackend = (file: string): StorageBackend => {
let created: boolean;
return {
get: async function() {
const path = `${DCDFileManager.getConstants().DocumentsDirPath}/${file}`;
if (!created && !await DCDFileManager.fileExists(path))
return (created = true, DCDFileManager.writeFile("documents", filePathFixer(file), "{}", "utf8"));
return JSON.parse((await DCDFileManager.readFile(path, "utf8")));
},
set: (data) => void DCDFileManager.writeFile("documents", filePathFixer(file), JSON.stringify(data), "utf8"),
};
};
let created: boolean;
return {
get: async () => {
const path = `${DCDFileManager.getConstants().DocumentsDirPath}/${file}`;
if (!created && !(await DCDFileManager.fileExists(path))) return (created = true), DCDFileManager.writeFile("documents", filePathFixer(file), "{}", "utf8");
return JSON.parse(await DCDFileManager.readFile(path, "utf8"));
},
set: (data) => void DCDFileManager.writeFile("documents", filePathFixer(file), JSON.stringify(data), "utf8"),
};
};

View file

@ -21,34 +21,31 @@ function without<T extends Record<string, any>>(object: T, ...keys: string[]) {
return cloned;
}
// I wish Hermes let me do async arrow functions
export default async function windowObject(unloads: any[]): Promise<VendettaObject> {
return {
patcher: without(patcher, "unpatchAll"),
metro: { ...metro, common: { ...common } },
constants,
utils,
debug: without(debug, "versionHash", "patchLogHook"),
ui: {
components,
toasts,
assets,
...color,
},
plugins: without(plugins, "initPlugins"),
commands: without(commands, "patchCommands"),
storage,
settings,
loader: {
identity: window.__vendetta_loader,
config: loaderConfig,
},
logger,
version: debug.versionHash,
unload: () => {
unloads.filter(i => typeof i === "function").forEach(p => p());
// @ts-expect-error explode
delete window.vendetta;
}
}
}
export default async (unloads: any[]): Promise<VendettaObject> => ({
patcher: without(patcher, "unpatchAll"),
metro: { ...metro, common: { ...common } },
constants,
utils,
debug: without(debug, "versionHash", "patchLogHook"),
ui: {
components,
toasts,
assets,
...color,
},
plugins: without(plugins, "initPlugins"),
commands: without(commands, "patchCommands"),
storage,
settings,
loader: {
identity: window.__vendetta_loader,
config: loaderConfig,
},
logger,
version: debug.versionHash,
unload: () => {
unloads.filter(i => typeof i === "function").forEach(p => p());
// @ts-expect-error explode
delete window.vendetta;
}
});