refactor(metro): more senseful naming

This commit is contained in:
amsyarasyiq 2024-05-21 17:15:51 +08:00
parent af6ba01079
commit 5c7d819264
4 changed files with 27 additions and 30 deletions

View file

@ -7,24 +7,21 @@ type ModulesMap = {
};
interface MetroCacheStore {
_v: 6;
_v: 10;
_buildNumber: number;
findIndex: Record<string, ModulesMap | undefined>;
polyfillIndex: Record<string, ModulesMap | undefined>;
assetsIndex: Record<string, number>;
}
const BUNNY_METRO_CACHE_KEY = "__bunny_metro_cache_key_v5__";
const BUNNY_METRO_CACHE_KEY = "__bunny_metro_cache_key_v10__";
let _metroCache = null as unknown as MetroCacheStore;
export function getMetroCache() {
return _metroCache;
}
window.getMetroCache = getMetroCache;
export const getMetroCache = window.__getMetroCache = () => _metroCache;
function buildInitCache() {
_metroCache = {
_v: 6,
_v: 10,
_buildNumber: ClientInfoManager.Build,
findIndex: {},
polyfillIndex: {},
@ -62,12 +59,12 @@ export function getCacherForUniq(uniq: string, allFind: boolean) {
return {
cacheId(moduleId: number) {
indexObject = _metroCache.findIndex[uniq] ??= {};
indexObject ??= _metroCache.findIndex[uniq] ??= {};
indexObject[moduleId] = 1;
saveCache();
},
finish() {
indexObject = _metroCache.findIndex[uniq] ??= {};
indexObject ??= _metroCache.findIndex[uniq] ??= {};
if (allFind) indexObject._ = 1;
saveCache();
}
@ -82,7 +79,7 @@ export function getPolyfillModuleCacher(name: string) {
return require("@metro/modules").getCachedPolyfillModules(name);
},
cacheId(moduleId: number) {
indexObject = _metroCache.polyfillIndex[name] ??= {};
indexObject ??= _metroCache.polyfillIndex[name] ??= {};
indexObject[moduleId] = 1;
saveCache();
}

View file

@ -4,10 +4,10 @@ import { FilterFn } from "./types";
function testExports<A extends unknown[]>(moduleExports: any, moduleId: number, filter: FilterFn<A>) {
if (moduleExports.default && moduleExports.__esModule && filter(moduleExports.default, moduleId, true)) {
return [filter.defaultFilter ? moduleExports : moduleExports.default, !filter.defaultFilter];
return [filter.raw ? moduleExports : moduleExports.default, !filter.raw];
}
if (!filter.defaultFilter && filter(moduleExports, moduleId, false)) {
if (!filter.raw && filter(moduleExports, moduleId, false)) {
return [moduleExports, false];
}
@ -47,7 +47,7 @@ export function findModuleId<A extends unknown[]>(filter: FilterFn<A>) {
*/
export function findExports<A extends unknown[]>(filter: FilterFn<A>) {
const [id, defaultExp] = findModule(filter);
if (defaultExp == null) return;
if (id == null) return;
return defaultExp ? requireModule(id).default : requireModule(id);
}
@ -84,9 +84,9 @@ export function findAllModuleId<A extends unknown[]>(filter: FilterFn<A>) {
* @param filter findAll calls filter once for each enumerable module's exports, adding the exports to the returned array when filter returns a thruthy value.
*/
export function findAllExports<A extends unknown[]>(filter: FilterFn<A>) {
return findAllModule(filter).map(e => {
const [id, defaultExp] = e;
if (defaultExp == null) return;
return findAllModule(filter).map(ret => {
if (!ret.length) return;
const [id, defaultExp] = ret;
return defaultExp ? requireModule(id).default : requireModule(id);
});
}

View file

@ -139,13 +139,13 @@ export interface FilterFn<A extends unknown[]> {
(m: any, id: number, defaultCheck: boolean): boolean;
filter: FilterCheckDef<A>;
args: A;
defaultFilter: boolean;
raw: boolean;
uniq: string;
}
export interface FilterDefinition<A extends unknown[]> {
(...args: A): FilterFn<A>;
byDefault(...args: A): FilterFn<A>;
byRaw(...args: A): FilterFn<A>;
uniqMaker(args: A): string;
}

View file

@ -8,26 +8,26 @@ export function createFilterDefinition<A extends unknown[]>(
fn: FilterCheckDef<A>,
uniqMaker: (args: A) => string
): FilterDefinition<A> {
function createHolder<T extends Function>(func: T, args: A, defaultFilter: boolean) {
function createHolder<T extends Function>(func: T, args: A, raw: boolean) {
return Object.assign(func, {
filter: fn,
defaultFilter,
raw,
args,
uniq: [
defaultFilter && "default::",
raw && "raw::",
uniqMaker(args)
].filter(Boolean).join("")
});
}
const curry = (defaultFilter: boolean) => (...args: A) => {
const curry = (raw: boolean) => (...args: A) => {
return createHolder((m: ModuleExports, id: number, defaultCheck: boolean) => {
return fn(args, m, id, defaultCheck);
}, args, defaultFilter);
}, args, raw);
};
return Object.assign(curry(false), {
byDefault: curry(true),
byRaw: curry(true),
uniqMaker
});
}
@ -46,17 +46,17 @@ export const findByProps = (...props: string[]) => findExports(byProps(...props)
export const findByPropsLazy = (...props: string[]) => proxyLazy(() => findByProps(...props));
export const findByPropsAll = (...props: string[]) => findAllExports(byProps(...props));
export const findByName = (name: string, expDefault = true) => findExports(expDefault ? byName(name) : byName.byDefault(name));
export const findByName = (name: string, expDefault = true) => findExports(expDefault ? byName(name) : byName.byRaw(name));
export const findByNameLazy = (name: string, expDefault = true) => proxyLazy(() => findByName(name, expDefault));
export const findByNameAll = (name: string, expDefault = true) => findAllExports(expDefault ? byName(name) : byName.byDefault(name));
export const findByNameAll = (name: string, expDefault = true) => findAllExports(expDefault ? byName(name) : byName.byRaw(name));
export const findByDisplayName = (name: string, expDefault = true) => findExports(expDefault ? byDisplayName(name) : byDisplayName.byDefault(name));
export const findByDisplayName = (name: string, expDefault = true) => findExports(expDefault ? byDisplayName(name) : byDisplayName.byRaw(name));
export const findByDisplayNameLazy = (name: string, expDefault = true) => proxyLazy(() => findByDisplayName(name, expDefault));
export const findByDisplayNameAll = (name: string, expDefault = true) => findAllExports(expDefault ? byDisplayName(name) : byDisplayName.byDefault(name));
export const findByDisplayNameAll = (name: string, expDefault = true) => findAllExports(expDefault ? byDisplayName(name) : byDisplayName.byRaw(name));
export const findByTypeName = (name: string, expDefault = true) => findExports(expDefault ? byTypeName(name) : byTypeName.byDefault(name));
export const findByTypeName = (name: string, expDefault = true) => findExports(expDefault ? byTypeName(name) : byTypeName.byRaw(name));
export const findByTypeNameLazy = (name: string, expDefault = true) => proxyLazy(() => findByTypeName(name, expDefault));
export const findByTypeNameAll = (name: string, expDefault = true) => findAllExports(expDefault ? byTypeName(name) : byTypeName.byDefault(name));
export const findByTypeNameAll = (name: string, expDefault = true) => findAllExports(expDefault ? byTypeName(name) : byTypeName.byRaw(name));
export const findByStoreName = (name: string) => findExports(byStoreName(name));
export const findByStoreNameLazy = (name: string) => proxyLazy(() => findByStoreName(name));