fix(metro/polyfill): unproxy polyfill

This commit is contained in:
amsyarasyiq 2024-06-02 00:09:16 +08:00
parent 1c151745e2
commit e660447143
3 changed files with 42 additions and 45 deletions

View file

@ -1,7 +1,7 @@
import { ClientInfoManager, MMKVManager } from "@lib/api/native/modules";
import { throttle } from "@lib/utils/throttle";
const CACHE_VERSION = 29;
const CACHE_VERSION = 31;
const BUNNY_METRO_CACHE_KEY = `__bunny_metro_cache_key_v${CACHE_VERSION}__`;
export enum ExportsFlags {
@ -115,6 +115,10 @@ export function getPolyfillModuleCacher(name: string) {
cacheId(moduleId: number) {
indexObject[moduleId] = 1;
saveCache();
},
finish() {
indexObject[`_${ModulesMapInternal.FULL_LOOKUP}`] = 1;
saveCache();
}
};
}

View file

@ -212,7 +212,7 @@ export function* getModules(uniq: string, all = false) {
}
export function* getCachedPolyfillModules(name: string) {
const cache = getMetroCache().polyfillIndex[name];
const cache = getMetroCache().polyfillIndex[name]!;
for (const id in cache) {
const exports = requireModule(Number(id));
@ -220,9 +220,11 @@ export function* getCachedPolyfillModules(name: string) {
yield [id, exports];
}
for (const id in metroModules) {
const exports = requireModule(Number(id));
if (isBadExports(exports)) continue;
yield [id, exports];
if (!cache[`_${ModulesMapInternal.FULL_LOOKUP}`]) {
for (const id in metroModules) {
const exports = requireModule(Number(id));
if (isBadExports(exports)) continue;
yield [id, exports];
}
}
}

View file

@ -102,46 +102,37 @@ type Keys = LiteralUnion<typeof redesignProps extends Set<infer U> ? U : string,
const cacher = getPolyfillModuleCacher("redesign_module");
const _cache = {} as Record<Keys, any>;
const _module = {} as Record<Keys, any>;
export default _module;
export default new Proxy({}, {
get(_, p) {
if (typeof p !== "string" || !redesignProps.has(p as any)) return;
if (_cache[p]) return _cache[p];
for (const prop of redesignProps) {
const filter = byProps(prop);
const candidates: any[] = [];
const prop = p as Keys;
const filter = byProps(prop);
const candidates: any[] = [];
for (const [id, moduleExports] of cacher.getModules()) {
if (filter(moduleExports, id, false)) {
cacher.cacheId(id);
candidates.push(moduleExports);
} else if (
moduleExports.__esModule
&& moduleExports.default
&& filter(moduleExports.default, id, false)
) {
cacher.cacheId(id);
candidates.push(moduleExports.default);
}
for (const [id, moduleExports] of cacher.getModules()) {
if (filter(moduleExports, id, false)) {
cacher.cacheId(id);
candidates.push(moduleExports);
} else if (
moduleExports.__esModule
&& moduleExports.default
&& filter(moduleExports.default, id, false)
) {
cacher.cacheId(id);
candidates.push(moduleExports.default);
}
if (candidates.length === 0) return undefined;
const bestCandidate = candidates.reduce(
(c1, c2) =>
(Object.keys(c2).length < Object.keys(c1).length)
? c2
: c1
);
return _cache[prop] = bestCandidate[prop];
},
ownKeys() {
redesignProps.forEach(prop => {
this.get!({}, prop, {});
});
return Reflect.ownKeys(_cache);
}
});
if (candidates.length === 0) continue;
const bestCandidate = candidates.reduce(
(c1, c2) =>
(Object.keys(c2).length < Object.keys(c1).length)
? c2
: c1
);
_module[prop] = bestCandidate[prop];
}
cacher.finish();