diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2023-02-15 19:44:52 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-15 19:44:52 +0100 |
commit | 75209e12f19ca5d4a2a7c9008fba63a487ad8e6a (patch) | |
tree | c122feabbceeef070de4d4eb23667c6153ea7eb1 /ext/node/polyfills/_next_tick.ts | |
parent | c4b9a91e27a32c0949688034c2449936c01a44a9 (diff) |
feat: wire up ext/node to the Node compatibility layer (#17785)
This PR changes Node.js/npm compatibility layer to use polyfills for
built-in Node.js
embedded in the snapshot (that are coming from "ext/node" extension).
As a result loading `std/node`, either from
"https://deno.land/std@<latest>/" or
from "DENO_NODE_COMPAT_URL" env variable were removed. All code that is
imported via "npm:" specifiers now uses code embedded in the snapshot.
Several fixes were applied to various modules in "ext/node" to make
tests pass.
---------
Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
Co-authored-by: Divy Srivastava <dj.srivastava23@gmail.com>
Diffstat (limited to 'ext/node/polyfills/_next_tick.ts')
-rw-r--r-- | ext/node/polyfills/_next_tick.ts | 145 |
1 files changed, 56 insertions, 89 deletions
diff --git a/ext/node/polyfills/_next_tick.ts b/ext/node/polyfills/_next_tick.ts index 7cbff0ea4..d5aa88218 100644 --- a/ext/node/polyfills/_next_tick.ts +++ b/ext/node/polyfills/_next_tick.ts @@ -1,8 +1,6 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // Copyright Joyent, Inc. and other Node contributors. -// deno-lint-ignore-file no-inner-declarations - import { core } from "internal:deno_node/polyfills/_core.ts"; import { validateFunction } from "internal:deno_node/polyfills/internal/validators.mjs"; import { _exiting } from "internal:deno_node/polyfills/_process/exiting.ts"; @@ -15,9 +13,6 @@ interface Tock { const queue = new FixedQueue(); -// deno-lint-ignore no-explicit-any -let _nextTick: any; - export function processTicksAndRejections() { let tock; do { @@ -68,92 +63,21 @@ export function processTicksAndRejections() { // setHasRejectionToWarn(false); } -if (typeof core.setNextTickCallback !== "undefined") { - function runNextTicks() { - // FIXME(bartlomieju): Deno currently doesn't unhandled rejections - // if (!hasTickScheduled() && !hasRejectionToWarn()) - // runMicrotasks(); - // if (!hasTickScheduled() && !hasRejectionToWarn()) - // return; - if (!core.hasTickScheduled()) { - core.runMicrotasks(); - } - if (!core.hasTickScheduled()) { - return true; - } - - processTicksAndRejections(); - return true; - } - - core.setNextTickCallback(processTicksAndRejections); - core.setMacrotaskCallback(runNextTicks); - - function __nextTickNative<T extends Array<unknown>>( - this: unknown, - callback: (...args: T) => void, - ...args: T - ) { - validateFunction(callback, "callback"); - - if (_exiting) { - return; - } - - // TODO(bartlomieju): seems superfluous if we don't depend on `arguments` - let args_; - switch (args.length) { - case 0: - break; - case 1: - args_ = [args[0]]; - break; - case 2: - args_ = [args[0], args[1]]; - break; - case 3: - args_ = [args[0], args[1], args[2]]; - break; - default: - args_ = new Array(args.length); - for (let i = 0; i < args.length; i++) { - args_[i] = args[i]; - } - } - - if (queue.isEmpty()) { - core.setHasTickScheduled(true); - } - // FIXME(bartlomieju): Deno currently doesn't support async hooks - // const asyncId = newAsyncId(); - // const triggerAsyncId = getDefaultTriggerAsyncId(); - const tickObject = { - // FIXME(bartlomieju): Deno currently doesn't support async hooks - // [async_id_symbol]: asyncId, - // [trigger_async_id_symbol]: triggerAsyncId, - callback, - args: args_, - }; - // FIXME(bartlomieju): Deno currently doesn't support async hooks - // if (initHooksExist()) - // emitInit(asyncId, 'TickObject', triggerAsyncId, tickObject); - queue.push(tickObject); +export function runNextTicks() { + // FIXME(bartlomieju): Deno currently doesn't unhandled rejections + // if (!hasTickScheduled() && !hasRejectionToWarn()) + // runMicrotasks(); + // if (!hasTickScheduled() && !hasRejectionToWarn()) + // return; + if (!core.hasTickScheduled()) { + core.runMicrotasks(); } - _nextTick = __nextTickNative; -} else { - function __nextTickQueueMicrotask<T extends Array<unknown>>( - this: unknown, - callback: (...args: T) => void, - ...args: T - ) { - if (args) { - queueMicrotask(() => callback.call(this, ...args)); - } else { - queueMicrotask(callback); - } + if (!core.hasTickScheduled()) { + return true; } - _nextTick = __nextTickQueueMicrotask; + processTicksAndRejections(); + return true; } // `nextTick()` will not enqueue any callback when the process is about to @@ -169,5 +93,48 @@ export function nextTick<T extends Array<unknown>>( callback: (...args: T) => void, ...args: T ) { - _nextTick(callback, ...args); + validateFunction(callback, "callback"); + + if (_exiting) { + return; + } + + // TODO(bartlomieju): seems superfluous if we don't depend on `arguments` + let args_; + switch (args.length) { + case 0: + break; + case 1: + args_ = [args[0]]; + break; + case 2: + args_ = [args[0], args[1]]; + break; + case 3: + args_ = [args[0], args[1], args[2]]; + break; + default: + args_ = new Array(args.length); + for (let i = 0; i < args.length; i++) { + args_[i] = args[i]; + } + } + + if (queue.isEmpty()) { + core.setHasTickScheduled(true); + } + // FIXME(bartlomieju): Deno currently doesn't support async hooks + // const asyncId = newAsyncId(); + // const triggerAsyncId = getDefaultTriggerAsyncId(); + const tickObject = { + // FIXME(bartlomieju): Deno currently doesn't support async hooks + // [async_id_symbol]: asyncId, + // [trigger_async_id_symbol]: triggerAsyncId, + callback, + args: args_, + }; + // FIXME(bartlomieju): Deno currently doesn't support async hooks + // if (initHooksExist()) + // emitInit(asyncId, 'TickObject', triggerAsyncId, tickObject); + queue.push(tickObject); } |