0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-29 08:58:01 -04:00
denoland-deno/ext/node/polyfills/_next_tick.ts
Bartek Iwańczuk b40086fd7d
refactor(core): include_js_files! 'dir' option doesn't change specifiers (#18019)
This commit changes "include_js_files!" macro from "deno_core"
in a way that "dir" option doesn't cause specifiers to be rewritten 
to include it.

Example:
```
include_js_files! {
  dir "js",
  "hello.js",
}
```

The above definition required embedders to use:
`import ... from "internal:<ext_name>/js/hello.js"`. 
But with this change, the "js" directory in which the files are stored
is an implementation detail, which for embedders results in: 
`import ... from "internal:<ext_name>/hello.js"`.

The directory the files are stored in, is an implementation detail and 
in some cases might result in a significant size difference for the 
snapshot. As an example, in "deno_node" extension, we store the 
source code in "polyfills" directory; which resulted in each specifier 
to look like "internal:deno_node/polyfills/<module_name>", but with 
this change it's "internal:deno_node/<module_name>". 

Given that "deno_node" has over 100 files, many of them having 
several import specifiers to the same extension, this change removes
10 characters from each import specifier.
2023-03-05 02:31:38 +00:00

149 lines
4.3 KiB
TypeScript

// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// Copyright Joyent, Inc. and other Node contributors.
import { core } from "internal:deno_node/_core.ts";
import { validateFunction } from "internal:deno_node/internal/validators.mjs";
import { _exiting } from "internal:deno_node/_process/exiting.ts";
import { FixedQueue } from "internal:deno_node/internal/fixed_queue.ts";
interface Tock {
callback: (...args: Array<unknown>) => void;
args: Array<unknown>;
}
let nextTickEnabled = false;
export function enableNextTick() {
nextTickEnabled = true;
}
const queue = new FixedQueue();
export function processTicksAndRejections() {
let tock;
do {
// deno-lint-ignore no-cond-assign
while (tock = queue.shift()) {
// FIXME(bartlomieju): Deno currently doesn't support async hooks
// const asyncId = tock[async_id_symbol];
// emitBefore(asyncId, tock[trigger_async_id_symbol], tock);
try {
const callback = (tock as Tock).callback;
if ((tock as Tock).args === undefined) {
callback();
} else {
const args = (tock as Tock).args;
switch (args.length) {
case 1:
callback(args[0]);
break;
case 2:
callback(args[0], args[1]);
break;
case 3:
callback(args[0], args[1], args[2]);
break;
case 4:
callback(args[0], args[1], args[2], args[3]);
break;
default:
callback(...args);
}
}
} finally {
// FIXME(bartlomieju): Deno currently doesn't support async hooks
// if (destroyHooksExist())
// emitDestroy(asyncId);
}
// FIXME(bartlomieju): Deno currently doesn't support async hooks
// emitAfter(asyncId);
}
core.runMicrotasks();
// FIXME(bartlomieju): Deno currently doesn't unhandled rejections
// } while (!queue.isEmpty() || processPromiseRejections());
} while (!queue.isEmpty());
core.setHasTickScheduled(false);
// FIXME(bartlomieju): Deno currently doesn't unhandled rejections
// setHasRejectionToWarn(false);
}
export function runNextTicks() {
// FIXME(bartlomieju): Deno currently doesn't unhandled rejections
// if (!hasTickScheduled() && !hasRejectionToWarn())
// runMicrotasks();
// if (!hasTickScheduled() && !hasRejectionToWarn())
// return;
if (!core.hasTickScheduled()) {
core.runMicrotasks();
return true;
}
processTicksAndRejections();
return true;
}
// `nextTick()` will not enqueue any callback when the process is about to
// exit since the callback would not have a chance to be executed.
export function nextTick(this: unknown, callback: () => void): void;
export function nextTick<T extends Array<unknown>>(
this: unknown,
callback: (...args: T) => void,
...args: T
): void;
export function nextTick<T extends Array<unknown>>(
this: unknown,
callback: (...args: T) => void,
...args: T
) {
// If we're snapshotting we don't want to push nextTick to be run. We'll
// enable next ticks in "__bootstrapNodeProcess()";
if (!nextTickEnabled) {
return;
}
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);
}