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; type Intercept = (payload: Record<string, any> & { type: string; }) => any;
let intercepts: Intercept[] = []; let intercepts: Intercept[] = [];
const interceptIndex = new Map<Intercept, number>();
let nextInterceptId = 0;
/** /**
* @internal * @internal
@ -33,7 +35,14 @@ export function injectFluxInterceptor() {
(dispatcher._interceptors ??= []).unshift(cb); (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 * nullish -> nothing, falsy -> block, object -> modify
*/ */
export function intercept(cb: Intercept) { export function intercept(cb: Intercept) {
const id = nextInterceptId++;
interceptIndex.set(cb, id);
intercepts.push(cb); intercepts.push(cb);
return () => { 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 SearchTree = Record<string, any>;
export type SearchFilter = (tree: SearchTree) => boolean; export type SearchFilter = (tree: SearchTree) => boolean;
export interface FindInTreeOptions { export interface FindInTreeOptions {
walkable?: string[]; walkable?: string[] | Set<string>;
ignore?: string[]; ignore?: string[] | Set<string>;
maxDepth?: number; maxDepth?: number;
} }
function treeSearch(tree: SearchTree, filter: SearchFilter, opts: Required<FindInTreeOptions>, depth: number): any { function treeSearch(tree: SearchTree, filter: SearchFilter, walkable: Set<string>, ignore: Set<string>, maxDepth: number, depth: number): any {
if (depth > opts.maxDepth) return; if (depth > maxDepth) return;
if (!tree) return; if (!tree) return;
try { try {
@ -22,21 +22,19 @@ function treeSearch(tree: SearchTree, filter: SearchFilter, opts: Required<FindI
if (typeof item !== "object" || item === null) continue; if (typeof item !== "object" || item === null) continue;
try { try {
const found = treeSearch(item, filter, opts, depth + 1); const found = treeSearch(item, filter, walkable, ignore, maxDepth, depth + 1);
if (found) return found; if (found) return found;
} catch { } } catch { }
} }
} else if (typeof tree === "object") { } else if (typeof tree === "object") {
const walkableLen = opts.walkable.length;
const ignoreLen = opts.ignore.length;
for (const key in tree) { for (const key in tree) {
if (Object.prototype.hasOwnProperty.call(tree, key) === false) continue; if (Object.prototype.hasOwnProperty.call(tree, key) === false) continue;
if (typeof tree[key] !== "object" || tree[key] === null) continue; if (typeof tree[key] !== "object" || tree[key] === null) continue;
if (walkableLen && !opts.walkable.includes(key)) continue; if (walkable.size && !walkable.has(key)) continue;
if (ignoreLen && opts.ignore.includes(key)) continue; if (ignore.size && ignore.has(key)) continue;
try { 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; if (found) return found;
} catch { } } catch { }
} }
@ -52,5 +50,5 @@ export default function findInTree(
maxDepth = 100 maxDepth = 100
}: FindInTreeOptions = {}, }: FindInTreeOptions = {},
): any | undefined { ): 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; export const getMetroCache = () => _metroCache;
function buildInitCache() { function buildInitCache() {
let modulesCount = 0; const modulesCount = Object.keys(window.modules).length;
for (const _ in window.modules) modulesCount++;
const cache = { const cache = {
_v: CACHE_VERSION, _v: CACHE_VERSION,
@ -47,8 +46,7 @@ export async function initMetroCache() {
_metroCache = null!; _metroCache = null!;
throw "cache invalidated; version mismatch"; throw "cache invalidated; version mismatch";
} }
let currentCount = 0; let currentCount = Object.keys(window.modules).length;
for (const _ in window.modules) currentCount++;
if (_metroCache._modulesCount !== currentCount) { if (_metroCache._modulesCount !== currentCount) {
_metroCache = null!; _metroCache = null!;
throw "cache invalidated; modules count mismatch"; throw "cache invalidated; modules count mismatch";