this was too hacky and causing issues already

This commit is contained in:
Nuckyz 2024-06-27 18:57:57 -03:00
parent b5a1b3a124
commit 962eaa9df7
No known key found for this signature in database
GPG key ID: 440BF8296E1C4AD9
4 changed files with 13 additions and 32 deletions

View file

@ -4,13 +4,6 @@
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { AnyObject } from "./types";
export type ProxyLazy<T = AnyObject> = T & {
[SYM_LAZY_GET]: () => T;
[SYM_LAZY_CACHED]: T | undefined;
};
export const SYM_LAZY_GET = Symbol.for("vencord.lazy.get");
export const SYM_LAZY_CACHED = Symbol.for("vencord.lazy.cached");
@ -80,7 +73,7 @@ const handler: ProxyHandler<any> = {
* @param attempts How many times to try to evaluate the factory before giving up
* @returns Result of factory function
*/
export function proxyLazy<T = AnyObject>(factory: () => T, attempts = 5): ProxyLazy<T> {
export function proxyLazy<T = any>(factory: () => T, attempts = 5): T {
const get = makeLazy(factory, attempts, { isIndirect: true });
const proxyDummy = Object.assign(function () { }, {

View file

@ -4,13 +4,6 @@
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { AnyObject } from "./types";
export type ProxyInner<T = AnyObject> = T & {
[SYM_PROXY_INNER_GET]?: () => T;
[SYM_PROXY_INNER_VALUE]?: T | undefined;
};
export const SYM_PROXY_INNER_GET = Symbol.for("vencord.proxyInner.get");
export const SYM_PROXY_INNER_VALUE = Symbol.for("vencord.proxyInner.innerValue");
@ -55,10 +48,10 @@ const handler: ProxyHandler<any> = {
* @param primitiveErr The error message to throw when the inner value is a primitive
* @returns A proxy which will act like the inner value when accessed
*/
export function proxyInner<T = AnyObject>(
export function proxyInner<T = any>(
errMsg = "Proxy inner value is undefined, setInnerValue was never called.",
primitiveErrMsg = "proxyInner called on a primitive value."
): [proxy: ProxyInner<T>, setInnerValue: (innerValue: T) => void] {
): [proxy: T, setInnerValue: (innerValue: T) => void] {
const proxyDummy = Object.assign(function () { }, {
[SYM_PROXY_INNER_GET]: function () {
if (proxyDummy[SYM_PROXY_INNER_VALUE] == null) {

View file

@ -356,7 +356,3 @@ export type PluginNative<PluginExports extends Record<string, (event: Electron.I
? (...args: Args) => Return extends Promise<any> ? Return : Promise<Return>
: never;
};
export type AnyObject = Record<PropertyKey, any> & ((...args: any[]) => any) & {
new(...args: any[]): any;
};

View file

@ -8,8 +8,7 @@ import { lazyString, makeLazy, proxyLazy } from "@utils/lazy";
import { LazyComponent, LazyComponentType, SYM_LAZY_COMPONENT_INNER } from "@utils/lazyReact";
import { Logger } from "@utils/Logger";
import { canonicalizeMatch } from "@utils/patches";
import { ProxyInner, proxyInner, SYM_PROXY_INNER_VALUE } from "@utils/proxyInner";
import { AnyObject } from "@utils/types";
import { proxyInner, SYM_PROXY_INNER_VALUE } from "@utils/proxyInner";
import type { WebpackInstance } from "discord-types/other";
import { traceFunction } from "../debug/Tracer";
@ -194,7 +193,7 @@ export function waitFor(filter: FilterFn, callback: ModCallbackFn, { isIndirect
* @param parse A function that takes the find result as its first argument and returns something to use as the proxy inner value. Useful if you want to use a value from the find result, instead of all of it. Defaults to the find result itself
* @returns A proxy that has the parse function return value as its true value, or the plain parse function return value if it was called immediately.
*/
export function find<T = AnyObject>(filter: FilterFn, parse: (mod: any) => any = m => m, { isIndirect = false }: { isIndirect?: boolean; } = {}) {
export function find<T = any>(filter: FilterFn, parse: (mod: any) => any = m => m, { isIndirect = false }: { isIndirect?: boolean; } = {}) {
if (typeof filter !== "function")
throw new Error("Invalid filter. Expected a function got " + typeof filter);
if (typeof parse !== "function")
@ -207,7 +206,7 @@ export function find<T = AnyObject>(filter: FilterFn, parse: (mod: any) => any =
webpackSearchHistory.push(["find", [proxy, filter]]);
}
if (proxy[SYM_PROXY_INNER_VALUE] != null) return proxy[SYM_PROXY_INNER_VALUE] as ProxyInner<T>;
if (proxy[SYM_PROXY_INNER_VALUE] != null) return proxy[SYM_PROXY_INNER_VALUE] as T;
return proxy;
}
@ -328,7 +327,7 @@ export function findComponentByCode<T extends object = any>(...code: string[] |
* @param props A list of props to search the module or exports for
* @param parse A function that takes the find result as its first argument and returns something. Useful if you want to use a value from the find result, instead of all of it. Defaults to the find result itself
*/
export function findByProps<T = AnyObject>(...props: string[] | [...string[], (mod: any) => T]) {
export function findByProps<T = any>(...props: string[] | [...string[], (mod: any) => T]) {
const parse = (typeof props.at(-1) === "function" ? props.pop() : m => m) as (mod: any) => T;
const newProps = props as string[];
@ -349,7 +348,7 @@ export function findByProps<T = AnyObject>(...props: string[] | [...string[], (m
* @param props A list of props to search the module or exports for
* @param parse A function that takes the find result as its first argument and returns something. Useful if you want to use a value from the find result, instead of all of it. Defaults to the find result itself
*/
export function findByPropsAndExtract<T = AnyObject>(...props: string[] | [...string[], (mod: any) => T]) {
export function findByPropsAndExtract<T = any>(...props: string[] | [...string[], (mod: any) => T]) {
const parse = (typeof props.at(-1) === "function" ? props.pop() : m => m) as (mod: any) => T;
const newProps = props as string[];
@ -368,7 +367,7 @@ export function findByPropsAndExtract<T = AnyObject>(...props: string[] | [...st
* @param code A list of code to search each export for
* @param parse A function that takes the find result as its first argument and returns something. Useful if you want to use a value from the find result, instead of all of it. Defaults to the find result itself
*/
export function findByCode<T = AnyObject>(...code: string[] | [...string[], (mod: any) => T]) {
export function findByCode<T = any>(...code: string[] | [...string[], (mod: any) => T]) {
const parse = (typeof code.at(-1) === "function" ? code.pop() : m => m) as (mod: any) => T;
const newCode = code as string[];
@ -402,7 +401,7 @@ export function findStore<T = GenericStore>(name: string) {
* @param code A list of code to search each factory for
* @param parse A function that takes the find result as its first argument and returns something. Useful if you want to use a value from the find result, instead of all of it. Defaults to the find result itself
*/
export function findByFactoryCode<T = AnyObject>(...code: string[] | [...string[], (mod: any) => T]) {
export function findByFactoryCode<T = any>(...code: string[] | [...string[], (mod: any) => T]) {
const parse = (typeof code.at(-1) === "function" ? code.pop() : m => m) as (mod: any) => T;
const newCode = code as string[];
@ -430,7 +429,7 @@ export function findByFactoryCode<T = AnyObject>(...code: string[] | [...string[
* @returns Unmangled exports as specified in mappers
*/
export function mapMangledModule<S extends PropertyKey>(code: string | string[], mappers: Record<S, FilterFn>) {
const mapping = {} as Record<S, ProxyInner<AnyObject>>;
const mapping = {} as Record<S, any>;
const setters = {} as Record<S, (innerValue: any) => void>;
for (const newName in mappers) {
@ -488,7 +487,7 @@ export function findModuleFactory(...code: string[]) {
const [proxy, setInnerValue] = proxyInner<ModuleFactory>(`Webpack module factory find matched no module. Filter: ${printFilter(filter)}`, "Webpack find with proxy called on a primitive value.");
waitFor(filter, (_, { factory }) => setInnerValue(factory));
if (proxy[SYM_PROXY_INNER_VALUE] != null) return proxy[SYM_PROXY_INNER_VALUE] as ProxyInner<ModuleFactory>;
if (proxy[SYM_PROXY_INNER_VALUE] != null) return proxy[SYM_PROXY_INNER_VALUE] as ModuleFactory;
return proxy;
}
@ -503,7 +502,7 @@ export function findModuleFactory(...code: string[]) {
* @param attempts How many times to try to evaluate the factory before giving up
* @returns Result of factory function
*/
export function webpackDependantLazy<T = AnyObject>(factory: () => T, attempts?: number) {
export function webpackDependantLazy<T = any>(factory: () => T, attempts?: number) {
if (IS_REPORTER) webpackSearchHistory.push(["webpackDependantLazy", [factory]]);
return proxyLazy<T>(factory, attempts);