2017-11-06 11:30:10 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
* @flow
|
|
|
|
* @format
|
|
|
|
* @providesModule BundleSegments
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
let segmentLoaders = new Map();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that a bundle segment is ready for use, for example requiring some of
|
2017-11-08 15:44:43 +00:00
|
|
|
* its module. We cache load promises so as to avoid calling `fetchSegment` twice
|
2017-11-06 11:30:10 +00:00
|
|
|
* for the same bundle. We assume that once a segment is fetched/loaded, it is
|
|
|
|
* never gettting removed during this instance of the JavaScript VM.
|
|
|
|
*/
|
|
|
|
async function loadForModule(moduleID: number): Promise<void> {
|
|
|
|
const {segmentId} = (require: $FlowFixMe).unpackModuleId(moduleID);
|
|
|
|
if (segmentId === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let segmentLoader = segmentLoaders.get(segmentId);
|
|
|
|
if (segmentLoader != null) {
|
|
|
|
return await segmentLoader;
|
|
|
|
}
|
2017-11-08 15:44:43 +00:00
|
|
|
|
|
|
|
const {fetchSegment} = global;
|
|
|
|
if (fetchSegment == null) {
|
2017-11-06 11:30:10 +00:00
|
|
|
throw new Error(
|
2017-11-08 15:44:43 +00:00
|
|
|
'When bundle splitting is enabled, the `global.fetchSegment` function ' +
|
2017-11-06 11:30:10 +00:00
|
|
|
'must be provided to be able to load particular bundle segments.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
segmentLoader = new Promise((resolve, reject) => {
|
2017-11-08 15:44:43 +00:00
|
|
|
fetchSegment(segmentId, error => {
|
2017-11-06 11:30:10 +00:00
|
|
|
if (error != null) {
|
|
|
|
reject(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
segmentLoaders.set(segmentId, segmentLoader);
|
|
|
|
return await segmentLoader;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {loadForModule};
|