qol improvements

This commit is contained in:
Vendicated 2022-11-03 19:12:50 +01:00
parent c20dc269d2
commit 2c3dee4120
No known key found for this signature in database
GPG key ID: EC781ADFB93EFFA3
3 changed files with 159 additions and 50 deletions

View file

@ -80,7 +80,7 @@ function ReplacementComponent({ module, match, replacement, setReplacementError
const fullMatch = matchResult[0] ? makeCodeblock(matchResult[0], "js") : "";
const groups = matchResult.length > 1
? makeCodeblock(matchResult.slice(1).map((g, i) => `Group ${i}: ${g}`).join("\n"), "yml")
? makeCodeblock(matchResult.slice(1).map((g, i) => `Group ${i + 1}: ${g}`).join("\n"), "yml")
: "";
return (
@ -172,6 +172,22 @@ function ReplacementInput({ replacement, setReplacement, replacementError }) {
onChange={onChange}
error={error ?? replacementError}
/>
{!isFunc && (
<>
<Forms.FormTitle>Cheat Sheet</Forms.FormTitle>
{Object.entries({
"$$": "Insert a $",
"$&": "Insert the entire match",
"$\\`": "Insert the substring before the match",
"$'": "Insert the substring after the match",
"$n": "Insert the nth capturing group ($1, $2...)"
}).map(([placeholder, desc]) => (
<Forms.FormText key={placeholder}>
{Parser.parse("`" + placeholder + "`")}: {desc}
</Forms.FormText>
))}
</>
)}
<Switch
className={Margins.marginTop8}
@ -202,7 +218,7 @@ function PatchHelper() {
find: ${JSON.stringify(find)},
replacement: {
match: /${match.replace(/(?<!\\)\//g, "\\/")}/,
replacement: ${typeof replacement === "function" ? replacement.toString() : JSON.stringify(replacement)}
replace: ${typeof replacement === "function" ? replacement.toString() : JSON.stringify(replacement)}
}
}
`.trim();

View file

@ -19,9 +19,9 @@
import { Promisable } from "type-fest";
export class Queue {
private promise = Promise.resolve();
private promise: Promise<any> = Promise.resolve();
add(func: () => Promisable<void>) {
this.promise = this.promise.then(func);
add<T>(func: (lastValue: unknown) => Promisable<T>): Promise<T> {
return (this.promise = this.promise.then(func));
}
}

View file

@ -21,6 +21,8 @@ import type { WebpackInstance } from "discord-types/other";
import Logger from "../utils/logger";
import { proxyLazy } from "../utils/proxyLazy";
const logger = new Logger("Webpack");
export let _resolveReady: () => void;
/**
* Fired once a gateway connection to Discord has been established.
@ -51,7 +53,6 @@ export const filters = {
},
};
const logger = new Logger("Webpack");
export const subscriptions = new Map<FilterFn, CallbackFn>();
export const listeners = new Set<CallbackFn>();
@ -125,6 +126,107 @@ export function findAll(filter: FilterFn, getDefault = true) {
return ret;
}
/**
* Same as {@link find} but in bulk
* @param filterFns Arry of filters. Please note that this array will be modified in place, so if you still
* need it afterwards, pass a copy.
* @returns Array of results in the same order as the passed filters
*/
export function bulk(...filterFns: FilterFn[]) {
if (!Array.isArray(filterFns))
throw new Error("Invalid filters. Expected function[] got " + typeof filterFns);
const { length } = filterFns;
if (length === 0)
throw new Error("Expected at least two filters.");
if (length === 1) {
if (IS_DEV) {
throw new Error("bulk called with only one filter. Use find");
}
return find(filterFns[0]);
}
const filters = filterFns as Array<FilterFn | undefined>;
let found = 0;
const results = Array(length);
outer:
for (const key in cache) {
const mod = cache[key];
if (!mod?.exports) continue;
for (let j = 0; j < length; j++) {
const filter = filters[j];
// Already done
if (filter === undefined) continue;
if (filter(mod.exports)) {
results[j] = mod.exports;
filters[j] = undefined;
if (++found === length) break outer;
break;
}
if (typeof mod.exports !== "object")
continue;
if (mod.exports.default && filter(mod.exports.default)) {
results[j] = mod.exports.default;
filters[j] = undefined;
if (++found === length) break outer;
break;
}
for (const nestedMod in mod.exports)
if (nestedMod.length <= 3) {
const nested = mod.exports[nestedMod];
if (nested && filter(nested)) {
results[j] = nested;
filters[j] = undefined;
if (++found === length) break outer;
continue outer;
}
}
}
}
if (found !== length) {
const err = new Error(`Got ${length} filters, but only found ${found} modules!`);
if (IS_DEV) {
// Strict behaviour in DevBuilds to fail early and make sure the issue is found
throw err;
}
logger.warn(err);
}
return results;
}
/**
* Find the id of a module by its code
* @param code Code
* @returns number or null
*/
export function findModuleId(code: string) {
for (const id in wreq.m) {
if (wreq.m[id].toString().includes(code)) {
return Number(id);
}
}
const err = new Error("Didn't find module with code:\n" + code);
if (IS_DEV) {
// Strict behaviour in DevBuilds to fail early and make sure the issue is found
throw err;
}
logger.warn(err);
return null;
}
/**
* Finds a mangled module by the provided code "code" (must be unique and can be anywhere in the module)
* then maps it into an easily usable module via the specified mappers
@ -140,11 +242,11 @@ export function findAll(filter: FilterFn, getDefault = true) {
export function mapMangledModule<S extends string>(code: string, mappers: Record<S, FilterFn>): Record<S, any> {
const exports = {} as Record<S, any>;
// search every factory function
for (const id in wreq.m) {
const src = wreq.m[id].toString() as string;
if (src.includes(code)) {
const mod = wreq(id as any as number);
const id = findModuleId(code);
if (id === null)
return exports;
const mod = wreq(id);
outer:
for (const key in mod) {
const member = mod[key];
@ -158,15 +260,6 @@ export function mapMangledModule<S extends string>(code: string, mappers: Record
}
return exports;
}
}
const err = new Error("Didn't find module matching this code:\n" + code);
if (IS_DEV)
throw err;
logger.warn(err);
return exports;
}
/**
* Same as {@link mapMangledModule} but lazy