Skip to content

Commit 7e48e2e

Browse files
committed
Use Scheduler.postTask when implementing EM_TIMING_SETIMMEDIATE
IIUC this API is better than `setTimeout(0)` or `postMessage()` in terms of being a polyfill for the old/deprecated setImmediate API. Its not clear if/when safari will implement this API so it will likely need to be feature detected for the foreseeable.
1 parent f573345 commit 7e48e2e

3 files changed

Lines changed: 14 additions & 4 deletions

File tree

src/closure-externs/closure-externs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,5 @@ Navigator.prototype.webkitGetUserMedia = function(
234234
var os = {};
235235

236236
AudioWorkletProcessor.parameterDescriptors;
237+
238+
var scheduler = {};

src/lib/libeventloop.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,11 +341,19 @@ LibraryJSEventLoop = {
341341
assert(mode == {{{ cDefs.EM_TIMING_SETIMMEDIATE}}});
342342
#endif
343343
if (!MainLoop.setImmediate) {
344-
if (globalThis.setImmediate) {
344+
if (globalThis.scheduler) {
345+
// Some modern browsers implement scheduler.postTask, but not all.
346+
#if RUNTIME_DEBUG
347+
dbg('setImmediate: using scheduler.postTask');
348+
#endif
349+
MainLoop.setImmediate = scheduler.postTask.bind(scheduler);
350+
#if ENVIRONMENT_MAY_BE_NODE
351+
} else if (globalThis.setImmediate) {
345352
MainLoop.setImmediate = setImmediate;
353+
#endif
346354
} else {
347355
#if RUNTIME_DEBUG
348-
dbg('using polyfill for setImmediate');
356+
dbg('setImmediate: using polyfill');
349357
#endif
350358
// Emulate setImmediate. (note: not a complete polyfill, we don't emulate clearImmediate() to keep code size to minimum, since not needed)
351359
var setImmediates = [];

test/codesize/test_codesize_hello_dylink_all.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"a.out.js": 268108,
2+
"a.out.js": 268175,
33
"a.out.nodebug.wasm": 587563,
4-
"total": 855671,
4+
"total": 855738,
55
"sent": [
66
"IMG_Init",
77
"IMG_Load",

0 commit comments

Comments
 (0)