MCPcopy Create free account
hub / github.com/Snapchat/Valdi / makeWaitForNextTask

Function makeWaitForNextTask

valdi/vscode_debugger/src/cdp/connection.ts:382–413  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

380// implementation taken from playwright: https://github.com/microsoft/playwright/blob/59d0f8728d4809b39785d68d7a146f06f0dbe2e6/src/helper.ts#L233
381// See https://joel.tools/microtasks/
382function makeWaitForNextTask() {
383 if (parseInt(process.versions.node, 10) >= 11) return setImmediate;
384
385 // Unlike Node 11, Node 10 and less have a bug with Task and MicroTask execution order:
386 // - https://github.com/nodejs/node/issues/22257
387 //
388 // So we can't simply run setImmediate to dispatch code in a following task.
389 // However, we can run setImmediate from-inside setImmediate to make sure we're getting
390 // in the following task.
391
392 let spinning = false;
393 const callbacks: (() => void)[] = [];
394 const loop = () => {
395 const callback = callbacks.shift();
396 if (!callback) {
397 spinning = false;
398 return;
399 }
400 setImmediate(loop);
401 // Make sure to call callback() as the last thing since it's
402 // untrusted code that might throw.
403 callback();
404 };
405
406 return (callback: () => void) => {
407 callbacks.push(callback);
408 if (!spinning) {
409 spinning = true;
410 setImmediate(loop);
411 }
412 };
413}

Callers 1

ConnectionClass · 0.85

Calls 2

setImmediateFunction · 0.50
pushMethod · 0.45

Tested by

no test coverage detected