Skip to content

Commit 697c9e6

Browse files
committed
explicite promise resolving
1 parent a25caf1 commit 697c9e6

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

tools/file_packager.py

Lines changed: 29 additions & 17 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 = '''
@@ -713,6 +716,8 @@ def generate_js(data_target, data_files, metadata):
713716
create_data = '''// canOwn this data in the filesystem, it is a slice into the heap that will never change
714717
Module['FS_createDataFile'](this.name, null, byteArray, true, true, true);
715718
Module['removeRunDependency'](`fp ${that.name}`);'''
719+
ready_promise = '''
720+
loadDataResolve();'''
716721

717722
if not options.lz4:
718723
# Data requests - for getting a block of data out of the big archive - have
@@ -739,14 +744,14 @@ def generate_js(data_target, data_files, metadata):
739744
finish: async function(byteArray) {
740745
var that = this;
741746
%s
742-
this.requests[this.name] = null;
747+
this.requests[this.name] = null;%s
743748
}
744749
};
745750
746751
var files = metadata['files'];
747752
for (var i = 0; i < files.length; ++i) {
748753
new DataRequest(files[i]['start'], files[i]['end'], files[i]['audio'] || 0).open('GET', files[i]['filename']);
749-
}\n''' % (create_preloaded if options.use_preload_plugins else create_data)
754+
}\n''' % (create_preloaded if options.use_preload_plugins else create_data, ready_promise if options.export_es6 else '')
750755

751756
if options.has_embedded and not options.obj_output:
752757
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')
@@ -1049,6 +1054,10 @@ def generate_js(data_target, data_files, metadata):
10491054

10501055
code += '''
10511056
Module['preloadResults'] ??= {};\n'''
1057+
catch_case = '''
1058+
.catch((error) => {
1059+
loadDataReject(error);
1060+
})'''
10521061

10531062
if options.use_preload_cache:
10541063
code += '''
@@ -1075,10 +1084,10 @@ def generate_js(data_target, data_files, metadata):
10751084
}
10761085
}
10771086
} catch(e) {
1078-
await preloadFallback(e);
1087+
await preloadFallback(e)%s;
10791088
}
10801089
1081-
Module['setStatus']?.('Downloading...');\n'''
1090+
Module['setStatus']?.('Downloading...');\n''' % (catch_case if options.export_es6 else '')
10821091
else:
10831092
# Not using preload cache, so we might as well start the xhr ASAP,
10841093
# potentially before JS parsing of the main codebase if it's after us.
@@ -1091,14 +1100,16 @@ def generate_js(data_target, data_files, metadata):
10911100
if (!fetched) {
10921101
// Note that we don't use await here because we want to execute the
10931102
// the rest of this function immediately.
1094-
let packageData = await fetchRemotePackage(REMOTE_PACKAGE_NAME, REMOTE_PACKAGE_SIZE);
1095-
if (fetchedCallback) {
1096-
fetchedCallback(packageData);
1097-
fetchedCallback = null;
1098-
} else {
1099-
fetched = packageData;
1100-
}
1101-
}\n'''
1103+
fetchRemotePackage(REMOTE_PACKAGE_NAME, REMOTE_PACKAGE_SIZE)
1104+
.then((data) => {
1105+
if (fetchedCallback) {
1106+
fetchedCallback(data);
1107+
fetchedCallback = null;
1108+
} else {
1109+
fetched = data;
1110+
}
1111+
})%s;
1112+
}\n''' % (catch_case if options.export_es6 else '')
11021113

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

11231134
if options.separate_metadata:
11241135
node_support_code = ''
@@ -1154,10 +1165,11 @@ def generate_js(data_target, data_files, metadata):
11541165
else:
11551166
ret += '''
11561167
}
1157-
await loadPackage(%s);\n''' % json.dumps(metadata)
1168+
loadPackage(%s);\n''' % json.dumps(metadata)
11581169

11591170
if options.export_es6:
11601171
ret += '''
1172+
});
11611173
}
11621174
// END the loadDataFile function
11631175
'''

0 commit comments

Comments
 (0)