Skip to content

Commit d48bc81

Browse files
authored
Simplify callUserCallback. NFC (#17440)
This moves the fetch-specific behaviour into the fetch library.
1 parent a741786 commit d48bc81

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

src/Fetch.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -454,43 +454,51 @@ function startFetch(fetch, successcb, errorcb, progresscb, readystatechangecb) {
454454
var fetchAttrReplace = !!(fetchAttributes & {{{ cDefine('EMSCRIPTEN_FETCH_REPLACE') }}});
455455
var fetchAttrSynchronous = !!(fetchAttributes & {{{ cDefine('EMSCRIPTEN_FETCH_SYNCHRONOUS') }}});
456456

457+
function doCallback(f) {
458+
if (fetchAttrSynchronous) {
459+
f();
460+
} else {
461+
callUserCallback(f);
462+
}
463+
}
464+
457465
var reportSuccess = (fetch, xhr, e) => {
458466
#if FETCH_DEBUG
459467
console.log('fetch: operation success. e: ' + e);
460468
#endif
461469
{{{ runtimeKeepalivePop() }}}
462-
callUserCallback(() => {
470+
doCallback(() => {
463471
if (onsuccess) {{{ makeDynCall('vp', 'onsuccess') }}}(fetch);
464472
else if (successcb) successcb(fetch);
465-
}, fetchAttrSynchronous);
473+
});
466474
};
467475

468476
var reportProgress = (fetch, xhr, e) => {
469-
callUserCallback(() => {
477+
doCallback(() => {
470478
if (onprogress) {{{ makeDynCall('vp', 'onprogress') }}}(fetch);
471479
else if (progresscb) progresscb(fetch);
472-
}, fetchAttrSynchronous);
480+
});
473481
};
474482

475483
var reportError = (fetch, xhr, e) => {
476484
#if FETCH_DEBUG
477485
console.error('fetch: operation failed: ' + e);
478486
#endif
479487
{{{ runtimeKeepalivePop() }}}
480-
callUserCallback(() => {
488+
doCallback(() => {
481489
if (onerror) {{{ makeDynCall('vp', 'onerror') }}}(fetch);
482490
else if (errorcb) errorcb(fetch);
483-
}, fetchAttrSynchronous);
491+
});
484492
};
485493

486494
var reportReadyStateChange = (fetch, xhr, e) => {
487495
#if FETCH_DEBUG
488496
console.log('fetch: ready state change. e: ' + e);
489497
#endif
490-
callUserCallback(() => {
498+
doCallback(() => {
491499
if (onreadystatechange) {{{ makeDynCall('vp', 'onreadystatechange') }}}(fetch);
492500
else if (readystatechangecb) readystatechangecb(fetch);
493-
}, fetchAttrSynchronous);
501+
});
494502
};
495503

496504
var performUncachedXhr = (fetch, xhr, e) => {
@@ -510,20 +518,20 @@ function startFetch(fetch, successcb, errorcb, progresscb, readystatechangecb) {
510518
console.log('fetch: IndexedDB store succeeded.');
511519
#endif
512520
{{{ runtimeKeepalivePop() }}}
513-
callUserCallback(() => {
521+
doCallback(() => {
514522
if (onsuccess) {{{ makeDynCall('vp', 'onsuccess') }}}(fetch);
515523
else if (successcb) successcb(fetch);
516-
}, fetchAttrSynchronous);
524+
});
517525
};
518526
var storeError = (fetch, xhr, e) => {
519527
#if FETCH_DEBUG
520528
console.error('fetch: IndexedDB store failed.');
521529
#endif
522530
{{{ runtimeKeepalivePop() }}}
523-
callUserCallback(() => {
531+
doCallback(() => {
524532
if (onsuccess) {{{ makeDynCall('vp', 'onsuccess') }}}(fetch);
525533
else if (successcb) successcb(fetch);
526-
}, fetchAttrSynchronous);
534+
});
527535
};
528536
fetchCacheData(Fetch.dbInstance, fetch, xhr.response, storeSuccess, storeError);
529537
};

src/library.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3488,8 +3488,7 @@ mergeInto(LibraryManager.library, {
34883488
'$maybeExit',
34893489
#endif
34903490
],
3491-
$callUserCallback__docs: '/** @param {boolean=} synchronous */',
3492-
$callUserCallback: function(func, synchronous) {
3491+
$callUserCallback: function(func) {
34933492
#if EXIT_RUNTIME
34943493
if (runtimeExited || ABORT) {
34953494
#else
@@ -3500,11 +3499,6 @@ mergeInto(LibraryManager.library, {
35003499
#endif
35013500
return;
35023501
}
3503-
// For synchronous calls, let any exceptions propagate, and don't let the runtime exit.
3504-
if (synchronous) {
3505-
func();
3506-
return;
3507-
}
35083502
try {
35093503
func();
35103504
#if EXIT_RUNTIME || USE_PTHREADS

0 commit comments

Comments
 (0)