Merge branch 'immediate-finds' into immediate-finds-modules-proxy

This commit is contained in:
Nuckyz 2024-06-27 19:00:23 -03:00
commit eecc21db91
No known key found for this signature in database
GPG key ID: 440BF8296E1C4AD9
4 changed files with 13 additions and 30 deletions

View file

@ -5,12 +5,6 @@
*/ */
import { UNCONFIGURABLE_PROPERTIES } from "./misc"; import { UNCONFIGURABLE_PROPERTIES } from "./misc";
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_GET = Symbol.for("vencord.lazy.get");
export const SYM_LAZY_CACHED = Symbol.for("vencord.lazy.cached"); export const SYM_LAZY_CACHED = Symbol.for("vencord.lazy.cached");
@ -77,7 +71,7 @@ const handler: ProxyHandler<any> = {
* @param attempts How many times to try to evaluate the factory before giving up * @param attempts How many times to try to evaluate the factory before giving up
* @returns Result of factory function * @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 get = makeLazy(factory, attempts, { isIndirect: true });
const proxyDummy = Object.assign(function () { }, { const proxyDummy = Object.assign(function () { }, {

View file

@ -5,12 +5,6 @@
*/ */
import { UNCONFIGURABLE_PROPERTIES } from "./misc"; import { UNCONFIGURABLE_PROPERTIES } from "./misc";
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_GET = Symbol.for("vencord.proxyInner.get");
export const SYM_PROXY_INNER_VALUE = Symbol.for("vencord.proxyInner.innerValue"); export const SYM_PROXY_INNER_VALUE = Symbol.for("vencord.proxyInner.innerValue");
@ -52,10 +46,10 @@ const handler: ProxyHandler<any> = {
* @param primitiveErr The error message to throw when the inner value is a primitive * @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 * @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.", errMsg = "Proxy inner value is undefined, setInnerValue was never called.",
primitiveErrMsg = "proxyInner called on a primitive value." 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 () { }, { const proxyDummy = Object.assign(function () { }, {
[SYM_PROXY_INNER_GET]: function () { [SYM_PROXY_INNER_GET]: function () {
if (proxyDummy[SYM_PROXY_INNER_VALUE] == null) { 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> ? (...args: Args) => Return extends Promise<any> ? Return : Promise<Return>
: never; : 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 { LazyComponent, LazyComponentType, SYM_LAZY_COMPONENT_INNER } from "@utils/lazyReact";
import { Logger } from "@utils/Logger"; import { Logger } from "@utils/Logger";
import { canonicalizeMatch } from "@utils/patches"; import { canonicalizeMatch } from "@utils/patches";
import { ProxyInner, proxyInner, SYM_PROXY_INNER_VALUE } from "@utils/proxyInner"; import { proxyInner, SYM_PROXY_INNER_VALUE } from "@utils/proxyInner";
import { AnyObject } from "@utils/types";
import { traceFunction } from "../debug/Tracer"; import { traceFunction } from "../debug/Tracer";
import { GenericStore } from "./common"; import { GenericStore } from "./common";
@ -200,7 +199,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 * @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. * @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: (module: ModuleExports) => ModuleExports = m => m, { isIndirect = false }: { isIndirect?: boolean; } = {}) { export function find<T = any>(filter: FilterFn, parse: (module: ModuleExports) => ModuleExports = m => m, { isIndirect = false }: { isIndirect?: boolean; } = {}) {
if (typeof filter !== "function") if (typeof filter !== "function")
throw new Error("Invalid filter. Expected a function got " + typeof filter); throw new Error("Invalid filter. Expected a function got " + typeof filter);
if (typeof parse !== "function") if (typeof parse !== "function")
@ -213,7 +212,7 @@ export function find<T = AnyObject>(filter: FilterFn, parse: (module: ModuleExpo
webpackSearchHistory.push(["find", [proxy, filter]]); 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; return proxy;
} }
@ -334,7 +333,7 @@ export function findComponentByCode<T extends object = any>(...code: string[] |
* @param props A list of props to search the module or exports for * @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 * @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[], (module: ModuleExports) => T]) { export function findByProps<T = any>(...props: string[] | [...string[], (module: ModuleExports) => T]) {
const parse = (typeof props.at(-1) === "function" ? props.pop() : m => m) as (module: ModuleExports) => T; const parse = (typeof props.at(-1) === "function" ? props.pop() : m => m) as (module: ModuleExports) => T;
const newProps = props as string[]; const newProps = props as string[];
@ -355,7 +354,7 @@ export function findByProps<T = AnyObject>(...props: string[] | [...string[], (m
* @param props A list of props to search the module or exports for * @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 * @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[], (module: ModuleExports) => T]) { export function findByPropsAndExtract<T = any>(...props: string[] | [...string[], (module: ModuleExports) => T]) {
const parse = (typeof props.at(-1) === "function" ? props.pop() : m => m) as (module: ModuleExports) => T; const parse = (typeof props.at(-1) === "function" ? props.pop() : m => m) as (module: ModuleExports) => T;
const newProps = props as string[]; const newProps = props as string[];
@ -374,7 +373,7 @@ export function findByPropsAndExtract<T = AnyObject>(...props: string[] | [...st
* @param code A list of code to search each export for * @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 * @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[], (module: ModuleExports) => T]) { export function findByCode<T = any>(...code: string[] | [...string[], (module: ModuleExports) => T]) {
const parse = (typeof code.at(-1) === "function" ? code.pop() : m => m) as (module: ModuleExports) => T; const parse = (typeof code.at(-1) === "function" ? code.pop() : m => m) as (module: ModuleExports) => T;
const newCode = code as string[]; const newCode = code as string[];
@ -408,7 +407,7 @@ export function findStore<T = GenericStore>(name: string) {
* @param code A list of code to search each factory for * @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 * @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[], (module: ModuleExports) => T]) { export function findByFactoryCode<T = any>(...code: string[] | [...string[], (module: ModuleExports) => T]) {
const parse = (typeof code.at(-1) === "function" ? code.pop() : m => m) as (module: ModuleExports) => T; const parse = (typeof code.at(-1) === "function" ? code.pop() : m => m) as (module: ModuleExports) => T;
const newCode = code as string[]; const newCode = code as string[];
@ -436,7 +435,7 @@ export function findByFactoryCode<T = AnyObject>(...code: string[] | [...string[
* @returns Unmangled exports as specified in mappers * @returns Unmangled exports as specified in mappers
*/ */
export function mapMangledModule<S extends PropertyKey>(code: string | string[], mappers: Record<S, FilterFn>) { 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: ModuleExports) => void>; const setters = {} as Record<S, (innerValue: ModuleExports) => void>;
for (const newName in mappers) { for (const newName in mappers) {
@ -494,7 +493,7 @@ export function findModuleFactory(...code: string[]) {
const [proxy, setInnerValue] = proxyInner<AnyModuleFactory>(`Webpack module factory find matched no module. Filter: ${printFilter(filter)}`, "Webpack find with proxy called on a primitive value."); const [proxy, setInnerValue] = proxyInner<AnyModuleFactory>(`Webpack module factory find matched no module. Filter: ${printFilter(filter)}`, "Webpack find with proxy called on a primitive value.");
waitFor(filter, (_, { factory }) => setInnerValue(factory)); waitFor(filter, (_, { factory }) => setInnerValue(factory));
if (proxy[SYM_PROXY_INNER_VALUE] != null) return proxy[SYM_PROXY_INNER_VALUE] as ProxyInner<AnyModuleFactory>; if (proxy[SYM_PROXY_INNER_VALUE] != null) return proxy[SYM_PROXY_INNER_VALUE] as AnyModuleFactory;
return proxy; return proxy;
} }
@ -509,7 +508,7 @@ export function findModuleFactory(...code: string[]) {
* @param attempts How many times to try to evaluate the factory before giving up * @param attempts How many times to try to evaluate the factory before giving up
* @returns Result of factory function * @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]]); if (IS_REPORTER) webpackSearchHistory.push(["webpackDependantLazy", [factory]]);
return proxyLazy<T>(factory, attempts); return proxyLazy<T>(factory, attempts);