Skip to content

Commit c0368f8

Browse files
committed
explicite promise resolving
1 parent ddb448e commit c0368f8

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

tools/file_packager.py

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,8 @@ def main(): # noqa: C901, PLR0912, PLR0915
492492
return 1
493493

494494
if options.from_emcc and options.export_es6:
495-
diagnostics.error('Can\'t use --export-es6 option together with --from-emcc since the code should be embedded within emcc\'s code')
495+
diagnostics.error('Can\'t use --export-es6 option together with --from-emcc since the code should be embedded '
496+
'within emcc\'s code')
496497
return 1
497498

498499
walked.append(__file__)
@@ -643,7 +644,7 @@ def generate_js(data_target, data_files, metadata):
643644

644645
if not options.export_es6:
645646
ret += '''
646-
(async () => {'''
647+
(() => {'''
647648

648649
ret += '''
649650
// Do not attempt to redownload the virtual filesystem data when in a pthread or a Wasm Worker context.
@@ -661,6 +662,8 @@ def generate_js(data_target, data_files, metadata):
661662
var require = createRequire(import.meta.url);
662663
}\n'''
663664

665+
if options.export_es6:
666+
ret += 'return new Promise((loadDataResolve, loadDataReject) => {\n'
664667
ret += ' async function loadPackage(metadata) {\n'
665668

666669
code = '''
@@ -710,6 +713,8 @@ def generate_js(data_target, data_files, metadata):
710713
create_data = '''// canOwn this data in the filesystem, it is a slide into the heap that will never change
711714
Module['FS_createDataFile'](this.name, null, byteArray, true, true, true);
712715
Module['removeRunDependency'](`fp ${that.name}`);'''
716+
ready_promise = '''
717+
loadDataResolve();'''
713718

714719
if not options.lz4:
715720
# Data requests - for getting a block of data out of the big archive - have
@@ -736,14 +741,14 @@ def generate_js(data_target, data_files, metadata):
736741
finish: function(byteArray) {
737742
var that = this;
738743
%s
739-
this.requests[this.name] = null;
744+
this.requests[this.name] = null;%s
740745
}
741746
};
742747
743748
var files = metadata['files'];
744749
for (var i = 0; i < files.length; ++i) {
745750
new DataRequest(files[i]['start'], files[i]['end'], files[i]['audio'] || 0).open('GET', files[i]['filename']);
746-
}\n''' % (create_preloaded if options.use_preload_plugins else create_data)
751+
}\n''' % (create_preloaded if options.use_preload_plugins else create_data, ready_promise if options.export_es6 else '')
747752

748753
if options.has_embedded and not options.obj_output:
749754
diagnostics.warn('--obj-output is recommended when using --embed. This outputs an object file for linking directly into your application is more efficient than JS encoding')
@@ -1046,7 +1051,11 @@ def generate_js(data_target, data_files, metadata):
10461051

10471052
code += '''
10481053
Module['preloadResults'] ??= {};\n'''
1049-
1054+
catch_case = '''
1055+
.catch((error) => {
1056+
loadDataReject(error);
1057+
})'''
1058+
10501059
if options.use_preload_cache:
10511060
code += '''
10521061
async function preloadFallback(error) {
@@ -1072,10 +1081,10 @@ def generate_js(data_target, data_files, metadata):
10721081
}
10731082
}
10741083
} catch(e) {
1075-
await preloadFallback(e);
1084+
await preloadFallback(e)%s;
10761085
}
10771086
1078-
Module['setStatus']?.('Downloading...');\n'''
1087+
Module['setStatus']?.('Downloading...');\n''' % (catch_case if options.export_es6 else '')
10791088
else:
10801089
# Not using preload cache, so we might as well start the xhr ASAP,
10811090
# potentially before JS parsing of the main codebase if it's after us.
@@ -1088,14 +1097,16 @@ def generate_js(data_target, data_files, metadata):
10881097
if (!fetched) {
10891098
// Note that we don't use await here because we want to execute the
10901099
// the rest of this function immediately.
1091-
let packageData = await fetchRemotePackage(REMOTE_PACKAGE_NAME, REMOTE_PACKAGE_SIZE);
1092-
if (fetchedCallback) {
1093-
fetchedCallback(packageData);
1094-
fetchedCallback = null;
1095-
} else {
1096-
fetched = packageData;
1097-
}
1098-
}\n'''
1100+
fetchRemotePackage(REMOTE_PACKAGE_NAME, REMOTE_PACKAGE_SIZE)
1101+
.then((data) => {
1102+
if (fetchedCallback) {
1103+
fetchedCallback(data);
1104+
fetchedCallback = null;
1105+
} else {
1106+
fetched = data;
1107+
}
1108+
})%s;
1109+
}\n''' % (catch_case if options.export_es6 else '')
10991110

11001111
code += '''
11011112
Module['preloadResults'][PACKAGE_NAME] = {fromCache: false};
@@ -1112,10 +1123,10 @@ def generate_js(data_target, data_files, metadata):
11121123
ret += '''
11131124
}
11141125
if (Module['calledRun']) {
1115-
await runWithFS(Module);
1126+
runWithFS(Module)%s;
11161127
} else {
11171128
(Module['preRun'] ??= []).push(runWithFS); // FS is not initialized yet, wait for it
1118-
}\n'''
1129+
}\n''' % (catch_case if options.export_es6 else '')
11191130

11201131
if options.separate_metadata:
11211132
node_support_code = ''
@@ -1151,10 +1162,11 @@ def generate_js(data_target, data_files, metadata):
11511162
else:
11521163
ret += '''
11531164
}
1154-
await loadPackage(%s);\n''' % json.dumps(metadata)
1165+
loadPackage(%s);\n''' % json.dumps(metadata)
11551166

11561167
if options.export_es6:
11571168
ret += '''
1169+
});
11581170
}
11591171
// END the loadDataFile function
11601172
'''

0 commit comments

Comments
 (0)