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 */
7import { buildUrlForBundle } from './buildUrlForBundle';
8import { fetchThenEvalAsync } from './fetchThenEval';
9// import LoadingView from '../LoadingView';
10
11let pendingRequests = 0;
12
13/**
14 * Load a bundle for a URL using fetch + eval on native and script tag injection on web.
15 *
16 * @param bundlePath Given a statement like `import('./Bacon')` `bundlePath` would be `Bacon.bundle?params=from-metro`.
17 */
18export async function loadBundleAsync(bundlePath: string): Promise<void> {
19  const requestUrl = buildUrlForBundle(bundlePath);
20
21  if (process.env.NODE_ENV === 'production') {
22    return fetchThenEvalAsync(requestUrl);
23  } else {
24    const LoadingView = require('../LoadingView')
25      .default as typeof import('../LoadingView').default;
26
27    // Send a signal to the `expo` package to show the loading indicator.
28    LoadingView.showMessage('Downloading...', 'load');
29
30    pendingRequests++;
31
32    return fetchThenEvalAsync(requestUrl)
33      .then(() => {
34        if (process.env.NODE_ENV !== 'production') {
35          const HMRClient = require('../HMRClient')
36            .default as typeof import('../HMRClient').default;
37          HMRClient.registerBundle(requestUrl);
38        }
39      })
40      .finally(() => {
41        if (!--pendingRequests) {
42          LoadingView.hide();
43        }
44      });
45  }
46}
47