diff --git a/src/metro/caches.ts b/src/metro/caches.ts index 848850e..9f933b9 100644 --- a/src/metro/caches.ts +++ b/src/metro/caches.ts @@ -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(); } }; } diff --git a/src/metro/modules.ts b/src/metro/modules.ts index a3caf8a..869f731 100644 --- a/src/metro/modules.ts +++ b/src/metro/modules.ts @@ -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]; + } } } diff --git a/src/metro/polyfills/redesign.ts b/src/metro/polyfills/redesign.ts index a2ca6fd..c119232 100644 --- a/src/metro/polyfills/redesign.ts +++ b/src/metro/polyfills/redesign.ts @@ -102,46 +102,37 @@ type Keys = LiteralUnion ? U : string, const cacher = getPolyfillModuleCacher("redesign_module"); -const _cache = {} as Record; +const _module = {} as Record; +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();