more cleanie

This commit is contained in:
Nuckyz 2024-08-23 05:11:07 -03:00
parent 26a71caced
commit 7d4fc5e8e1
No known key found for this signature in database
GPG key ID: 440BF8296E1C4AD9
2 changed files with 13 additions and 11 deletions

View file

@ -25,7 +25,7 @@ import { canonicalizeMatch, canonicalizeReplace, canonicalizeReplacement } from
import { SYM_PROXY_INNER_GET, SYM_PROXY_INNER_VALUE } from "@utils/proxyInner"; import { SYM_PROXY_INNER_GET, SYM_PROXY_INNER_VALUE } from "@utils/proxyInner";
import definePlugin, { PluginNative, StartAt } from "@utils/types"; import definePlugin, { PluginNative, StartAt } from "@utils/types";
import * as Webpack from "@webpack"; 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 * as Common from "@webpack/common";
import { loadLazyChunks } from "debug/loadLazyChunks"; import { loadLazyChunks } from "debug/loadLazyChunks";
import type { ComponentType } from "react"; import type { ComponentType } from "react";
@ -56,14 +56,14 @@ function makeShortcuts() {
const cacheKey = String(filterProps); const cacheKey = String(filterProps);
if (cache.has(cacheKey)) return cache.get(cacheKey); if (cache.has(cacheKey)) return cache.get(cacheKey);
const matches = _cacheFindAll(filterFactory(...filterProps)); const matches = cacheFindAll(filterFactory(...filterProps), shouldReturnFactory);
const result = (() => { const result = (() => {
switch (matches.length) { switch (matches.length) {
case 0: return null; case 0: return null;
case 1: return shouldReturnFactory ? matches[0].factory : matches[0].result; case 1: return matches[0];
default: 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) { if (uniqueMatches.length > 1) {
console.warn(`Warning: This filter matches ${matches.length} modules. Make it more specific!\n`, uniqueMatches); 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), findByFactoryCode: newFindWrapper(filters.byFactoryCode),
findAllByFactoryCode: (...code: Webpack.CodeFilter) => cacheFindAll(filters.byFactoryCode(...code)), findAllByFactoryCode: (...code: Webpack.CodeFilter) => cacheFindAll(filters.byFactoryCode(...code)),
findModuleFactory: newFindWrapper(filters.byFactoryCode, true), 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 }, plugins: { getter: () => Vencord.Plugins.plugins },
PluginsApi: { getter: () => Vencord.Plugins }, PluginsApi: { getter: () => Vencord.Plugins },

View file

@ -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. * 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 * @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) { export function cacheFind(filter: FilterFn, shouldReturnFactory: boolean = false) {
return _cacheFind(filter).result; const { result, factory } = _cacheFind(filter);
return shouldReturnFactory ? factory : result;
} }
/** /**
@ -777,10 +778,11 @@ export function _cacheFindAll(filter: FilterFn): Required<CacheFindResult>[] {
* Find the the export or module exports from an all the required modules that match the filter. * 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 * @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) { export function cacheFindAll(filter: FilterFn, shouldReturnFactories: boolean = false) {
return _cacheFindAll(filter).map(({ result }) => result); const cacheFindAllResults = _cacheFindAll(filter);
return cacheFindAllResults.map(({ result, factory }) => shouldReturnFactories ? factory : result);
} }
/** /**