feat: some speedups

This commit is contained in:
kmmiio99o 2026-04-05 21:20:30 +02:00
parent 18dccc9e17
commit 676f1a19ba
No known key found for this signature in database
GPG key ID: AC9D7747D423B9BF
3 changed files with 29 additions and 17 deletions

View file

@ -8,6 +8,8 @@ export const dispatcher = FluxDispatcher;
type Intercept = (payload: Record<string, any> & { type: string; }) => any;
let intercepts: Intercept[] = [];
const interceptIndex = new Map<Intercept, number>();
let nextInterceptId = 0;
/**
* @internal
@ -33,7 +35,14 @@ export function injectFluxInterceptor() {
(dispatcher._interceptors ??= []).unshift(cb);
return () => dispatcher._interceptors &&= dispatcher._interceptors.filter(v => v !== cb);
return () => {
const idx = interceptIndex.get(cb);
if (idx !== undefined) {
intercepts.splice(idx, 1);
interceptIndex.delete(cb);
intercepts.forEach((intercept, i) => interceptIndex.set(intercept, i));
}
};
}
/**
@ -41,9 +50,16 @@ export function injectFluxInterceptor() {
* nullish -> nothing, falsy -> block, object -> modify
*/
export function intercept(cb: Intercept) {
const id = nextInterceptId++;
interceptIndex.set(cb, id);
intercepts.push(cb);
return () => {
intercepts = intercepts.filter(i => i !== cb);
const idx = interceptIndex.get(cb);
if (idx !== undefined) {
intercepts.splice(idx, 1);
interceptIndex.delete(cb);
intercepts.forEach((intercept, i) => interceptIndex.set(intercept, i));
}
};
}

View file

@ -4,13 +4,13 @@
export type SearchTree = Record<string, any>;
export type SearchFilter = (tree: SearchTree) => boolean;
export interface FindInTreeOptions {
walkable?: string[];
ignore?: string[];
walkable?: string[] | Set<string>;
ignore?: string[] | Set<string>;
maxDepth?: number;
}
function treeSearch(tree: SearchTree, filter: SearchFilter, opts: Required<FindInTreeOptions>, depth: number): any {
if (depth > opts.maxDepth) return;
function treeSearch(tree: SearchTree, filter: SearchFilter, walkable: Set<string>, ignore: Set<string>, maxDepth: number, depth: number): any {
if (depth > maxDepth) return;
if (!tree) return;
try {
@ -22,21 +22,19 @@ function treeSearch(tree: SearchTree, filter: SearchFilter, opts: Required<FindI
if (typeof item !== "object" || item === null) continue;
try {
const found = treeSearch(item, filter, opts, depth + 1);
const found = treeSearch(item, filter, walkable, ignore, maxDepth, depth + 1);
if (found) return found;
} catch { }
}
} else if (typeof tree === "object") {
const walkableLen = opts.walkable.length;
const ignoreLen = opts.ignore.length;
for (const key in tree) {
if (Object.prototype.hasOwnProperty.call(tree, key) === false) continue;
if (typeof tree[key] !== "object" || tree[key] === null) continue;
if (walkableLen && !opts.walkable.includes(key)) continue;
if (ignoreLen && opts.ignore.includes(key)) continue;
if (walkable.size && !walkable.has(key)) continue;
if (ignore.size && ignore.has(key)) continue;
try {
const found = treeSearch(tree[key], filter, opts, depth + 1);
const found = treeSearch(tree[key], filter, walkable, ignore, maxDepth, depth + 1);
if (found) return found;
} catch { }
}
@ -52,5 +50,5 @@ export default function findInTree(
maxDepth = 100
}: FindInTreeOptions = {},
): any | undefined {
return treeSearch(tree, filter, { walkable, ignore, maxDepth }, 0);
return treeSearch(tree, filter, new Set(walkable), new Set(ignore), maxDepth, 0);
}

View file

@ -16,8 +16,7 @@ let _metroCache = null as unknown as ReturnType<typeof buildInitCache>;
export const getMetroCache = () => _metroCache;
function buildInitCache() {
let modulesCount = 0;
for (const _ in window.modules) modulesCount++;
const modulesCount = Object.keys(window.modules).length;
const cache = {
_v: CACHE_VERSION,
@ -47,8 +46,7 @@ export async function initMetroCache() {
_metroCache = null!;
throw "cache invalidated; version mismatch";
}
let currentCount = 0;
for (const _ in window.modules) currentCount++;
let currentCount = Object.keys(window.modules).length;
if (_metroCache._modulesCount !== currentCount) {
_metroCache = null!;
throw "cache invalidated; modules count mismatch";