From 15b7982228f4d52f5b56c8aeab9b16ea1575152c Mon Sep 17 00:00:00 2001 From: Nuckyz <61953774+Nuckyz@users.noreply.github.com> Date: Wed, 18 Sep 2024 13:57:33 -0300 Subject: [PATCH] yes --- src/globals.d.ts | 1 + src/plugins/decor/ui/components/index.ts | 2 +- src/utils/lazy.ts | 23 ++++++++--------------- src/utils/lazyReact.tsx | 6 +++--- src/utils/proxyInner.ts | 16 ++++++---------- src/webpack/api.tsx | 20 ++++++++++---------- 6 files changed, 29 insertions(+), 39 deletions(-) diff --git a/src/globals.d.ts b/src/globals.d.ts index f1a5a84bf..70843b5b8 100644 --- a/src/globals.d.ts +++ b/src/globals.d.ts @@ -20,6 +20,7 @@ import { LoDashStatic } from "lodash"; declare global { type AnyRecord = Record; + type AnyComponentType

= React.ComponentType

& AnyRecord; /** * This exists only at build time, so references to it in patches should insert it diff --git a/src/plugins/decor/ui/components/index.ts b/src/plugins/decor/ui/components/index.ts index fbcd3c8b6..a0c05cd69 100644 --- a/src/plugins/decor/ui/components/index.ts +++ b/src/plugins/decor/ui/components/index.ts @@ -19,7 +19,7 @@ type DecorationGridItemComponent = ComponentType DecorationGridItem = v; -export const AvatarDecorationModalPreview = findComponentByCode(".shopPreviewBanner", component => React.memo(component)); +export const AvatarDecorationModalPreview = findComponentByCode(".shopPreviewBanner", component => React.memo(component)); type DecorationGridDecorationComponent = React.ComponentType & { avatarDecoration: AvatarDecoration; diff --git a/src/utils/lazy.ts b/src/utils/lazy.ts index e1e8f06f1..69d6961f7 100644 --- a/src/utils/lazy.ts +++ b/src/utils/lazy.ts @@ -17,7 +17,7 @@ export function makeLazy(factory: () => T, attempts = 5, { isIndirect = false let tries = 0; let cache: T; - const getter = () => { + const getter: LazyFunction = function () { if (!cache && attempts > tries) { tries++; cache = factory(); @@ -30,7 +30,6 @@ export function makeLazy(factory: () => T, attempts = 5, { isIndirect = false }; getter.$$vencordLazyFailed = () => tries === attempts; - return getter; } @@ -69,17 +68,11 @@ const handler: ProxyHandler = { * * @param factory Factory returning the result * @param attempts How many times to try to evaluate the factory before giving up - * @param errMsg The error message to throw when the factory fails - * @param primitiveErrMsg The error message to throw when factory result is a primitive + * @param err The error message to throw when the factory fails + * @param primitiveErr The error message to throw when factory result is a primitive * @returns Result of factory function */ -export function proxyLazy( - factory: () => T, - attempts = 5, - errMsg: string | (() => string) = `proxyLazy factory failed:\n${factory}`, - primitiveErrMsg = "proxyLazy called on a primitive value.", - isChild = false -): T { +export function proxyLazy(factory: () => T, attempts = 5, err: string | (() => string) = `proxyLazy factory failed:\n${factory}`, primitiveErr = "proxyLazy called on a primitive value.", isChild = false): T { const get = makeLazy(factory, attempts, { isIndirect: true }); let isSameTick = true; @@ -93,7 +86,7 @@ export function proxyLazy( } if (!proxyDummy[SYM_LAZY_CACHED]) { - throw new Error(typeof errMsg === "string" ? errMsg : errMsg()); + throw new Error(typeof err === "string" ? err : err()); } else { if (typeof proxyDummy[SYM_LAZY_CACHED] === "function") { proxy.toString = proxyDummy[SYM_LAZY_CACHED].toString.bind(proxyDummy[SYM_LAZY_CACHED]); @@ -129,8 +122,8 @@ export function proxyLazy( return Reflect.get(lazyTarget, p, lazyTarget); }, attempts, - errMsg, - primitiveErrMsg, + err, + primitiveErr, true ); } @@ -140,7 +133,7 @@ export function proxyLazy( return Reflect.get(lazyTarget, p, lazyTarget); } - throw new Error(primitiveErrMsg); + throw new Error(primitiveErr); } }); diff --git a/src/utils/lazyReact.tsx b/src/utils/lazyReact.tsx index 88cce9de3..637f15256 100644 --- a/src/utils/lazyReact.tsx +++ b/src/utils/lazyReact.tsx @@ -9,7 +9,7 @@ import { makeLazy } from "./lazy"; export const SYM_LAZY_COMPONENT_INNER = Symbol.for("vencord.lazyComponent.inner"); export type LazyComponentType

= React.FunctionComponent

& AnyRecord & { - [SYM_LAZY_COMPONENT_INNER]: () => LazyComponentType

| null; + [SYM_LAZY_COMPONENT_INNER]: () => AnyComponentType

| null; }; /** @@ -19,10 +19,10 @@ export type LazyComponentType

= React.FunctionComponent

* @param attempts How many times to try to get the component before giving up * @returns Result of factory function */ -export function LazyComponent

(factory: () => any, attempts = 5, err: string | (() => string) = `LazyComponent factory failed:\n${factory}`): LazyComponentType

{ +export function LazyComponent

(factory: () => AnyComponentType

, attempts = 5, err: string | (() => string) = `LazyComponent factory failed:\n${factory}`): LazyComponentType

{ const get = makeLazy(factory, attempts, { isIndirect: true }); - let InnerComponent = null as LazyComponentType

| null; + let InnerComponent = null as AnyComponentType

| null; let lazyFailedLogged = false; const LazyComponent: LazyComponentType

= function (props) { diff --git a/src/utils/proxyInner.ts b/src/utils/proxyInner.ts index 0e1be40ed..fabec64c4 100644 --- a/src/utils/proxyInner.ts +++ b/src/utils/proxyInner.ts @@ -42,22 +42,18 @@ const handler: ProxyHandler = { * IMPORTANT: * Destructuring at top level is not supported for proxyInner. * - * @param errMsg The error message to throw when the inner value is not set - * @param primitiveErrMsg The error message to throw when the inner value is a primitive + * @param err The error message to throw when the inner value is not set + * @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( - errMsg: string | (() => string) = "Proxy inner value is undefined, setInnerValue was never called.", - primitiveErrMsg = "proxyInner called on a primitive value. This can happen if you try to destructure a primitive at the same tick as the proxy was created.", - isChild = false -): [proxy: T, setInnerValue: (innerValue: T) => void] { +export function proxyInner(err: string | (() => string) = "Proxy inner value is undefined, setInnerValue was never called.", primitiveErr = "proxyInner called on a primitive value. This can happen if you try to destructure a primitive at the same tick as the proxy was created.", isChild = false): [proxy: T, setInnerValue: (innerValue: T) => void] { let isSameTick = true; if (!isChild) setTimeout(() => isSameTick = false, 0); const proxyDummy = Object.assign(function () { }, { [SYM_PROXY_INNER_GET]: function () { if (proxyDummy[SYM_PROXY_INNER_VALUE] == null) { - throw new Error(typeof errMsg === "string" ? errMsg : errMsg()); + throw new Error(typeof err === "string" ? err : err()); } return proxyDummy[SYM_PROXY_INNER_VALUE]; @@ -85,7 +81,7 @@ export function proxyInner( "\nConsider not destructuring, using findProp or if you really need to destructure, using mapMangledModule instead." ); - const [recursiveProxy, recursiveSetInnerValue] = proxyInner(errMsg, primitiveErrMsg, true); + const [recursiveProxy, recursiveSetInnerValue] = proxyInner(err, primitiveErr, true); recursiveSetInnerValues.push((innerValue: T) => { // Set the inner value of the destructured value as the prop value p of the parent @@ -100,7 +96,7 @@ export function proxyInner( return Reflect.get(innerTarget, p, innerTarget); } - throw new Error(primitiveErrMsg); + throw new Error(primitiveErr); } }); diff --git a/src/webpack/api.tsx b/src/webpack/api.tsx index c76f8bcd4..28fe5e66b 100644 --- a/src/webpack/api.tsx +++ b/src/webpack/api.tsx @@ -171,8 +171,8 @@ function printFilter(filter: FilterFn) { return String(filter); } -function wrapWebpackComponent

(err: string | (() => string)): [WrapperComponent: LazyComponentType

, setInnerComponent: (rawComponent: any, parsedComponent: LazyComponentType

) => void] { - let InnerComponent = null as LazyComponentType

| null; +function wrapWebpackComponent

(err: string | (() => string)): [WrapperComponent: LazyComponentType

, setInnerComponent: (rawComponent: any, parsedComponent: AnyComponentType

) => void] { + let InnerComponent = null as AnyComponentType

| null; let findFailedLogged = false; const WrapperComponent: LazyComponentType

= function (props) { @@ -186,7 +186,7 @@ function wrapWebpackComponent

(err: string | (() => string)) WrapperComponent[SYM_LAZY_COMPONENT_INNER] = () => InnerComponent; - function setInnerComponent(RawComponent: any, ParsedComponent: LazyComponentType

) { + function setInnerComponent(RawComponent: any, ParsedComponent: AnyComponentType

) { InnerComponent = ParsedComponent; Object.assign(WrapperComponent, RawComponent); } @@ -281,7 +281,7 @@ export function find(filter: FilterFn, parse: (module: ModuleExports) = * @param parse A function that takes the found component as its first argument and returns a component. Useful if you want to wrap the found component in something. Defaults to the original component * @returns The component if found, or a noop component */ -export function findComponent

(filter: FilterFn, parse: (component: ModuleExports) => LazyComponentType

= m => m, { isIndirect = false }: { isIndirect?: boolean; } = {}) { +export function findComponent

(filter: FilterFn, parse: (component: ModuleExports) => AnyComponentType

= m => m, { isIndirect = false }: { isIndirect?: boolean; } = {}) { if (typeof filter !== "function") { throw new Error("Invalid filter. Expected a function got " + typeof filter); } @@ -311,8 +311,8 @@ export function findComponent

(filter: FilterFn, parse: (com * @param parse A function that takes the found component as its first argument and returns a component. Useful if you want to wrap the found component in something. Defaults to the original component * @returns The component if found, or a noop component */ -export function findExportedComponent

(...props: PropsFilter | [...PropsFilter, (component: ModuleExports) => LazyComponentType

]) { - const parse = (typeof props.at(-1) === "function" ? props.pop() : m => m) as (component: ModuleExports) => LazyComponentType

; +export function findExportedComponent

(...props: PropsFilter | [...PropsFilter, (component: ModuleExports) => AnyComponentType

]) { + const parse = (typeof props.at(-1) === "function" ? props.pop() : m => m) as (component: ModuleExports) => AnyComponentType

; const newProps = props as PropsFilter; const filter = filters.byProps(...newProps); @@ -339,8 +339,8 @@ export function findExportedComponent

(...props: PropsFilter * @param parse A function that takes the found component as its first argument and returns a component. Useful if you want to wrap the found component in something. Defaults to the original component * @returns The component if found, or a noop component */ -export function findComponentByCode

(...code: CodeFilter | [...CodeFilter, (component: ModuleExports) => LazyComponentType

]) { - const parse = (typeof code.at(-1) === "function" ? code.pop() : m => m) as (component: ModuleExports) => LazyComponentType

; +export function findComponentByCode

(...code: CodeFilter | [...CodeFilter, (component: ModuleExports) => AnyComponentType

]) { + const parse = (typeof code.at(-1) === "function" ? code.pop() : m => m) as (component: ModuleExports) => AnyComponentType

; const newCode = code as CodeFilter; const ComponentResult = findComponent

(filters.componentByCode(...newCode), parse, { isIndirect: true }); @@ -362,8 +362,8 @@ export function findComponentByCode

(...code: CodeFilter | [ * @param parse A function that takes the found component as its first argument and returns a component. Useful if you want to wrap the found component in something. Defaults to the original component * @returns The component if found, or a noop component */ -export function findComponentByFields

(...fields: PropsFilter | [...PropsFilter, (component: ModuleExports) => LazyComponentType

]) { - const parse = (typeof fields.at(-1) === "function" ? fields.pop() : m => m) as (component: ModuleExports) => LazyComponentType

; +export function findComponentByFields

(...fields: PropsFilter | [...PropsFilter, (component: ModuleExports) => AnyComponentType

]) { + const parse = (typeof fields.at(-1) === "function" ? fields.pop() : m => m) as (component: ModuleExports) => AnyComponentType

; const newFields = fields as PropsFilter; const ComponentResult = findComponent

(filters.componentByFields(...newFields), parse, { isIndirect: true });