Skip to content

Commit 62c737f

Browse files
committed
Simplify callUserCallback. NFC
This moves the fetch-specific behaviour into the fetch library.
1 parent 529028e commit 62c737f

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
@@ -3492,8 +3492,7 @@ mergeInto(LibraryManager.library, {
34923492
'$maybeExit',
34933493
#endif
34943494
],
3495-
$callUserCallback__docs: '/** @param {boolean=} synchronous */',
3496-
$callUserCallback: function(func, synchronous) {
3495+
$callUserCallback: function(func) {
34973496
#if EXIT_RUNTIME
34983497
if (runtimeExited || ABORT) {
34993498
#else
@@ -3504,11 +3503,6 @@ mergeInto(LibraryManager.library, {
35043503
#endif
35053504
return;
35063505
}
3507-
// For synchronous calls, let any exceptions propagate, and don't let the runtime exit.
3508-
if (synchronous) {
3509-
func();
3510-
return;
3511-
}
35123506
try {
35133507
func();
35143508
#if EXIT_RUNTIME || USE_PTHREADS

0 commit comments

Comments
 (0)