Avoid checking for default twice
This commit is contained in:
parent
172c372e40
commit
8602ec2611
2 changed files with 10 additions and 35 deletions
|
@ -12,7 +12,7 @@ import { PatchReplacement } from "@utils/types";
|
|||
|
||||
import { traceFunctionWithResults } from "../debug/Tracer";
|
||||
import { patches } from "../plugins";
|
||||
import { _initWebpack, AnyModuleFactory, AnyWebpackRequire, factoryListeners, findModuleId, ModuleExports, moduleListeners, subscriptions, WebpackRequire, WrappedModuleFactory, wreq } from ".";
|
||||
import { _initWebpack, AnyModuleFactory, AnyWebpackRequire, factoryListeners, findModuleId, ModuleExports, moduleListeners, waitForSubscriptions, WebpackRequire, WrappedModuleFactory, wreq } from ".";
|
||||
|
||||
const logger = new Logger("WebpackInterceptor", "#8caaee");
|
||||
|
||||
|
@ -345,11 +345,6 @@ function wrapAndPatchFactory(id: PropertyKey, originalFactory: AnyModuleFactory)
|
|||
break nonEnumerableChecking;
|
||||
}
|
||||
|
||||
if (exports.default === window || exports.default?.[Symbol.toStringTag] === "DOMTokenList") {
|
||||
shouldMakeNonEnumerable = true;
|
||||
break nonEnumerableChecking;
|
||||
}
|
||||
|
||||
for (const exportKey in exports) {
|
||||
if (exports[exportKey] === window || exports[exportKey]?.[Symbol.toStringTag] === "DOMTokenList") {
|
||||
shouldMakeNonEnumerable = true;
|
||||
|
@ -380,11 +375,11 @@ function wrapAndPatchFactory(id: PropertyKey, originalFactory: AnyModuleFactory)
|
|||
}
|
||||
}
|
||||
|
||||
for (const [filter, callback] of subscriptions) {
|
||||
for (const [filter, callback] of waitForSubscriptions) {
|
||||
try {
|
||||
|
||||
if (filter(exports)) {
|
||||
subscriptions.delete(filter);
|
||||
waitForSubscriptions.delete(filter);
|
||||
callback(exports, id);
|
||||
continue;
|
||||
}
|
||||
|
@ -393,17 +388,11 @@ function wrapAndPatchFactory(id: PropertyKey, originalFactory: AnyModuleFactory)
|
|||
continue;
|
||||
}
|
||||
|
||||
if (exports.default != null && filter(exports.default)) {
|
||||
subscriptions.delete(filter);
|
||||
callback(exports.default, id);
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const exportKey in exports) {
|
||||
const exportValue = exports[exportKey];
|
||||
|
||||
if (exportValue != null && filter(exportValue)) {
|
||||
subscriptions.delete(filter);
|
||||
waitForSubscriptions.delete(filter);
|
||||
callback(exportValue, id);
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -86,7 +86,7 @@ export const filters = {
|
|||
export type CallbackFn = (module: ModuleExports, id: PropertyKey) => void;
|
||||
export type FactoryListernFn = (factory: AnyModuleFactory) => void;
|
||||
|
||||
export const subscriptions = new Map<FilterFn, CallbackFn>();
|
||||
export const waitForSubscriptions = new Map<FilterFn, CallbackFn>();
|
||||
export const moduleListeners = new Set<CallbackFn>();
|
||||
export const factoryListeners = new Set<FactoryListernFn>();
|
||||
|
||||
|
@ -130,7 +130,7 @@ export const find = traceFunction("find", function find(filter: FilterFn, { isIn
|
|||
|
||||
for (const key in cache) {
|
||||
const mod = cache[key];
|
||||
if (!mod.loaded || !mod?.exports) continue;
|
||||
if (!mod?.loaded || mod.exports == null) continue;
|
||||
|
||||
if (filter(mod.exports)) {
|
||||
return isWaitFor ? [mod.exports, key] : mod.exports;
|
||||
|
@ -138,11 +138,6 @@ export const find = traceFunction("find", function find(filter: FilterFn, { isIn
|
|||
|
||||
if (typeof mod.exports !== "object") continue;
|
||||
|
||||
if (mod.exports.default && filter(mod.exports.default)) {
|
||||
const found = mod.exports.default;
|
||||
return isWaitFor ? [found, key] : found;
|
||||
}
|
||||
|
||||
for (const nestedMod in mod.exports) {
|
||||
const nested = mod.exports[nestedMod];
|
||||
if (nested && filter(nested)) {
|
||||
|
@ -165,16 +160,14 @@ export function findAll(filter: FilterFn) {
|
|||
const ret = [] as any[];
|
||||
for (const key in cache) {
|
||||
const mod = cache[key];
|
||||
if (!mod.loaded || !mod?.exports) continue;
|
||||
if (!mod?.loaded || mod.exports == null) continue;
|
||||
|
||||
if (filter(mod.exports))
|
||||
ret.push(mod.exports);
|
||||
else if (typeof mod.exports !== "object")
|
||||
continue;
|
||||
|
||||
if (mod.exports.default && filter(mod.exports.default))
|
||||
ret.push(mod.exports.default);
|
||||
else for (const nestedMod in mod.exports) {
|
||||
for (const nestedMod in mod.exports) {
|
||||
const nested = mod.exports[nestedMod];
|
||||
if (nested && filter(nested)) ret.push(nested);
|
||||
}
|
||||
|
@ -213,7 +206,7 @@ export const findBulk = traceFunction("findBulk", function findBulk(...filterFns
|
|||
outer:
|
||||
for (const key in cache) {
|
||||
const mod = cache[key];
|
||||
if (!mod.loaded || !mod?.exports) continue;
|
||||
if (!mod?.loaded || mod.exports == null) continue;
|
||||
|
||||
for (let j = 0; j < length; j++) {
|
||||
const filter = filters[j];
|
||||
|
@ -230,13 +223,6 @@ export const findBulk = traceFunction("findBulk", function findBulk(...filterFns
|
|||
if (typeof mod.exports !== "object")
|
||||
continue;
|
||||
|
||||
if (mod.exports.default && filter(mod.exports.default)) {
|
||||
results[j] = mod.exports.default;
|
||||
filters[j] = undefined;
|
||||
if (++found === length) break outer;
|
||||
break;
|
||||
}
|
||||
|
||||
for (const nestedMod in mod.exports) {
|
||||
const nested = mod.exports[nestedMod];
|
||||
if (nested && filter(nested)) {
|
||||
|
@ -605,7 +591,7 @@ export function waitFor(filter: string | PropsFilter | FilterFn, callback: Callb
|
|||
if (existing) return void callback(existing, id);
|
||||
}
|
||||
|
||||
subscriptions.set(filter, callback);
|
||||
waitForSubscriptions.set(filter, callback);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Reference in a new issue