1/** 2 * Copyright © 2022 650 Industries. 3 * 4 * This source code is licensed under the MIT license found in the 5 * LICENSE file in the root directory of this source tree. 6 */ 7 8import { loadBundleAsync } from './loadBundle'; 9 10/** 11 * Must satisfy the requirements of the Metro bundler. 12 * https://github.com/react-native-community/discussions-and-proposals/blob/main/proposals/0605-lazy-bundling.md#__loadbundleasync-in-metro 13 */ 14type AsyncRequire = (path: string) => Promise<void>; 15 16/** Create an `loadBundleAsync` function in the expected shape for Metro bundler. */ 17export function buildAsyncRequire(): AsyncRequire { 18 const cache = new Map<string, Promise<void>>(); 19 20 return async function universal_loadBundleAsync(path: string): Promise<void> { 21 if (cache.has(path)) { 22 return cache.get(path)!; 23 } 24 25 const promise = loadBundleAsync(path).catch((error) => { 26 cache.delete(path); 27 throw error; 28 }); 29 30 cache.set(path, promise); 31 32 return promise; 33 }; 34} 35