Skip to content

Commit d27cd6a

Browse files
committed
Convert asyncLoad from callbacks to promise. NFC
This is an internal function that should not have any external users.
1 parent 536da63 commit d27cd6a

File tree

7 files changed

+26
-29
lines changed

7 files changed

+26
-29
lines changed

src/library.js

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2182,25 +2182,20 @@ addToLibrary({
21822182
},
21832183

21842184
$asyncLoad__docs: '/** @param {boolean=} noRunDep */',
2185-
$asyncLoad: (url, onload, onerror, noRunDep) => {
2186-
var dep = !noRunDep ? getUniqueRunDependency(`al ${url}`) : '';
2187-
readAsync(url).then(
2188-
(arrayBuffer) => {
2189-
#if ASSERTIONS
2190-
assert(arrayBuffer, `Loading data file "${url}" failed (no arrayBuffer).`);
2191-
#endif
2192-
onload(new Uint8Array(arrayBuffer));
2193-
if (dep) removeRunDependency(dep);
2194-
},
2195-
(err) => {
2196-
if (onerror) {
2197-
onerror();
2198-
} else {
2199-
throw `Loading data file "${url}" failed.`;
2200-
}
2201-
}
2202-
);
2203-
if (dep) addRunDependency(dep);
2185+
$asyncLoad: (url, noRunDep) => {
2186+
return new Promise((resolve, reject) => {
2187+
var dep = !noRunDep ? getUniqueRunDependency(`al ${url}`) : '';
2188+
if (dep) addRunDependency(dep);
2189+
readAsync(url).then(
2190+
(arrayBuffer) => {
2191+
#if ASSERTIONS
2192+
assert(arrayBuffer, `Loading data file "${url}" failed (no arrayBuffer).`);
2193+
#endif
2194+
resolve(new Uint8Array(arrayBuffer));
2195+
if (dep) removeRunDependency(dep);
2196+
}, reject
2197+
);
2198+
});
22042199
},
22052200

22062201
$alignMemory: (size, alignment) => {

src/library_async.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -470,18 +470,19 @@ addToLibrary({
470470
emscripten_wget_data__async: true,
471471
emscripten_wget_data: (url, pbuffer, pnum, perror) => {
472472
return Asyncify.handleSleep((wakeUp) => {
473-
asyncLoad(UTF8ToString(url), (byteArray) => {
473+
/* no need for run dependency, this is async but will not do any prepare etc. step */
474+
asyncLoad(UTF8ToString(url), /*noRunDep=*/true).then((byteArray) => {
474475
// can only allocate the buffer after the wakeUp, not during an asyncing
475476
var buffer = _malloc(byteArray.length); // must be freed by caller!
476477
HEAPU8.set(byteArray, buffer);
477478
{{{ makeSetValue('pbuffer', 0, 'buffer', '*') }}};
478479
{{{ makeSetValue('pnum', 0, 'byteArray.length', 'i32') }}};
479480
{{{ makeSetValue('perror', 0, '0', 'i32') }}};
480481
wakeUp();
481-
}, () => {
482+
}).catch(() => {
482483
{{{ makeSetValue('perror', 0, '1', 'i32') }}};
483484
wakeUp();
484-
}, true /* no need for run dependency, this is async but will not do any prepare etc. step */ );
485+
});
485486
});
486487
},
487488

src/library_dylink.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ var LibraryDylink = {
10241024

10251025
var libFile = locateFile(libName);
10261026
if (flags.loadAsync) {
1027-
return new Promise((resolve, reject) => asyncLoad(libFile, resolve, reject));
1027+
return asyncLoad(libFile);
10281028
}
10291029

10301030
// load the binary synchronously

src/library_fs_shared.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ addToLibrary({
7676
}
7777
addRunDependency(dep);
7878
if (typeof url == 'string') {
79-
asyncLoad(url, processData, onerror);
79+
asyncLoad(url).then(processData).catch(onerror);
8080
} else {
8181
processData(url);
8282
}

src/library_wget.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,23 @@ var LibraryWget = {
6464
emscripten_async_wget_data__proxy: 'sync',
6565
emscripten_async_wget_data: (url, userdata, onload, onerror) => {
6666
{{{ runtimeKeepalivePush() }}}
67-
asyncLoad(UTF8ToString(url), (byteArray) => {
67+
/* no need for run dependency, this is async but will not do any prepare etc. step */
68+
asyncLoad(UTF8ToString(url), /*noRunDep=*/true).then((byteArray) => {
6869
{{{ runtimeKeepalivePop() }}}
6970
callUserCallback(() => {
7071
var buffer = _malloc(byteArray.length);
7172
HEAPU8.set(byteArray, buffer);
7273
{{{ makeDynCall('vppi', 'onload') }}}(userdata, buffer, byteArray.length);
7374
_free(buffer);
7475
});
75-
}, () => {
76+
}).catch(() => {
7677
if (onerror) {
7778
{{{ runtimeKeepalivePop() }}}
7879
callUserCallback(() => {
7980
{{{ makeDynCall('vp', 'onerror') }}}(userdata);
8081
});
8182
}
82-
}, true /* no need for run dependency, this is async but will not do any prepare etc. step */ );
83+
});
8384
},
8485

8586
emscripten_async_wget2__deps: ['$PATH_FS', '$wget', '$stackRestore', '$stringToUTF8OnStack'],
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6339
1+
6298
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
13936
1+
13867

0 commit comments

Comments
 (0)