From 7d4fc5e8e1f7858650e9a909a5706c2a00e1dcb7 Mon Sep 17 00:00:00 2001 From: Nuckyz <61953774+Nuckyz@users.noreply.github.com> Date: Fri, 23 Aug 2024 05:11:07 -0300 Subject: [PATCH] more cleanie --- src/plugins/consoleShortcuts/index.ts | 10 +++++----- src/webpack/api.tsx | 14 ++++++++------ 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/plugins/consoleShortcuts/index.ts b/src/plugins/consoleShortcuts/index.ts index db4a7ee44..711436636 100644 --- a/src/plugins/consoleShortcuts/index.ts +++ b/src/plugins/consoleShortcuts/index.ts @@ -25,7 +25,7 @@ import { canonicalizeMatch, canonicalizeReplace, canonicalizeReplacement } from import { SYM_PROXY_INNER_GET, SYM_PROXY_INNER_VALUE } from "@utils/proxyInner"; import definePlugin, { PluginNative, StartAt } from "@utils/types"; import * as Webpack from "@webpack"; -import { _cacheFindAll, cacheFindAll, cacheFindModuleId, extract, filters, searchFactories } from "@webpack"; +import { cacheFindAll, cacheFindModuleId, extract, filters, searchFactories } from "@webpack"; import * as Common from "@webpack/common"; import { loadLazyChunks } from "debug/loadLazyChunks"; import type { ComponentType } from "react"; @@ -56,14 +56,14 @@ function makeShortcuts() { const cacheKey = String(filterProps); if (cache.has(cacheKey)) return cache.get(cacheKey); - const matches = _cacheFindAll(filterFactory(...filterProps)); + const matches = cacheFindAll(filterFactory(...filterProps), shouldReturnFactory); const result = (() => { switch (matches.length) { case 0: return null; - case 1: return shouldReturnFactory ? matches[0].factory : matches[0].result; + case 1: return matches[0]; default: - const uniqueMatches = [...new Set(shouldReturnFactory ? matches.map(m => m.factory) : matches.map(m => m.result))]; + const uniqueMatches = [...new Set(matches)]; if (uniqueMatches.length > 1) { console.warn(`Warning: This filter matches ${matches.length} modules. Make it more specific!\n`, uniqueMatches); } @@ -113,7 +113,7 @@ function makeShortcuts() { findByFactoryCode: newFindWrapper(filters.byFactoryCode), findAllByFactoryCode: (...code: Webpack.CodeFilter) => cacheFindAll(filters.byFactoryCode(...code)), findModuleFactory: newFindWrapper(filters.byFactoryCode, true), - findAllModuleFactories: (...code: Webpack.CodeFilter) => _cacheFindAll(filters.byFactoryCode(...code)).map(m => m.factory), + findAllModuleFactories: (...code: Webpack.CodeFilter) => cacheFindAll(filters.byFactoryCode(...code), true), plugins: { getter: () => Vencord.Plugins.plugins }, PluginsApi: { getter: () => Vencord.Plugins }, diff --git a/src/webpack/api.tsx b/src/webpack/api.tsx index 8273c7afe..e822a4f14 100644 --- a/src/webpack/api.tsx +++ b/src/webpack/api.tsx @@ -716,10 +716,11 @@ export const _cacheFind = traceFunction("cacheFind", function _cacheFind(filter: * Find the first export or module exports from an already required module that matches the filter. * * @param filter A function that takes an export or module exports and returns a boolean - * @returns The found export or module exports, or undefined + * @returns The found export, module exports, module factory, or undefined */ -export function cacheFind(filter: FilterFn) { - return _cacheFind(filter).result; +export function cacheFind(filter: FilterFn, shouldReturnFactory: boolean = false) { + const { result, factory } = _cacheFind(filter); + return shouldReturnFactory ? factory : result; } /** @@ -777,10 +778,11 @@ export function _cacheFindAll(filter: FilterFn): Required[] { * Find the the export or module exports from an all the required modules that match the filter. * * @param filter A function that takes an export or module exports and returns a boolean - * @returns An array of found exports or module exports + * @returns An array of found exports, module exports, or module factories */ -export function cacheFindAll(filter: FilterFn) { - return _cacheFindAll(filter).map(({ result }) => result); +export function cacheFindAll(filter: FilterFn, shouldReturnFactories: boolean = false) { + const cacheFindAllResults = _cacheFindAll(filter); + return cacheFindAllResults.map(({ result, factory }) => shouldReturnFactories ? factory : result); } /**