2020-06-18 22:22:17 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const {
|
2020-11-15 18:12:43 +01:00
|
|
|
FunctionPrototypeBind,
|
2020-06-18 22:22:17 +02:00
|
|
|
Promise,
|
|
|
|
PromiseReject,
|
2021-05-12 12:16:43 +02:00
|
|
|
SafePromisePrototypeFinally,
|
2020-06-18 22:22:17 +02:00
|
|
|
} = primordials;
|
|
|
|
|
|
|
|
const {
|
|
|
|
Timeout,
|
|
|
|
Immediate,
|
|
|
|
insert
|
|
|
|
} = require('internal/timers');
|
|
|
|
|
|
|
|
const {
|
2020-11-29 19:01:24 +01:00
|
|
|
AbortError,
|
2020-06-18 22:22:17 +02:00
|
|
|
codes: { ERR_INVALID_ARG_TYPE }
|
|
|
|
} = require('internal/errors');
|
|
|
|
|
2021-01-31 00:16:18 +01:00
|
|
|
const {
|
|
|
|
validateAbortSignal,
|
|
|
|
validateBoolean,
|
|
|
|
validateObject,
|
|
|
|
} = require('internal/validators');
|
2020-12-22 16:23:23 +01:00
|
|
|
|
2020-11-06 16:07:43 +01:00
|
|
|
function cancelListenerHandler(clear, reject) {
|
|
|
|
if (!this._destroyed) {
|
|
|
|
clear(this);
|
2020-11-29 19:01:24 +01:00
|
|
|
reject(new AbortError());
|
2020-11-06 16:07:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-18 22:22:17 +02:00
|
|
|
function setTimeout(after, value, options = {}) {
|
|
|
|
const args = value !== undefined ? [value] : value;
|
|
|
|
if (options == null || typeof options !== 'object') {
|
|
|
|
return PromiseReject(
|
|
|
|
new ERR_INVALID_ARG_TYPE(
|
|
|
|
'options',
|
|
|
|
'Object',
|
|
|
|
options));
|
|
|
|
}
|
|
|
|
const { signal, ref = true } = options;
|
2020-12-22 16:23:23 +01:00
|
|
|
try {
|
|
|
|
validateAbortSignal(signal, 'options.signal');
|
|
|
|
} catch (err) {
|
|
|
|
return PromiseReject(err);
|
2020-06-18 22:22:17 +02:00
|
|
|
}
|
|
|
|
if (typeof ref !== 'boolean') {
|
|
|
|
return PromiseReject(
|
|
|
|
new ERR_INVALID_ARG_TYPE(
|
|
|
|
'options.ref',
|
|
|
|
'boolean',
|
|
|
|
ref));
|
|
|
|
}
|
2021-04-15 12:11:05 +02:00
|
|
|
// TODO(@jasnell): If a decision is made that this cannot be backported
|
|
|
|
// to 12.x, then this can be converted to use optional chaining to
|
|
|
|
// simplify the check.
|
|
|
|
if (signal && signal.aborted) {
|
2020-11-29 19:01:24 +01:00
|
|
|
return PromiseReject(new AbortError());
|
2020-08-13 21:00:55 +02:00
|
|
|
}
|
2020-11-06 16:07:43 +01:00
|
|
|
let oncancel;
|
|
|
|
const ret = new Promise((resolve, reject) => {
|
2021-04-20 22:13:59 +02:00
|
|
|
const timeout = new Timeout(resolve, after, args, false, ref);
|
2020-06-18 22:22:17 +02:00
|
|
|
insert(timeout, timeout._idleTimeout);
|
|
|
|
if (signal) {
|
2020-11-15 18:12:43 +01:00
|
|
|
oncancel = FunctionPrototypeBind(cancelListenerHandler,
|
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
timeout, clearTimeout, reject);
|
2020-11-06 16:07:43 +01:00
|
|
|
signal.addEventListener('abort', oncancel);
|
2020-06-18 22:22:17 +02:00
|
|
|
}
|
|
|
|
});
|
2020-11-06 16:07:43 +01:00
|
|
|
return oncancel !== undefined ?
|
2021-05-12 12:16:43 +02:00
|
|
|
SafePromisePrototypeFinally(
|
2020-11-06 16:07:43 +01:00
|
|
|
ret,
|
|
|
|
() => signal.removeEventListener('abort', oncancel)) : ret;
|
2020-06-18 22:22:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function setImmediate(value, options = {}) {
|
|
|
|
if (options == null || typeof options !== 'object') {
|
|
|
|
return PromiseReject(
|
|
|
|
new ERR_INVALID_ARG_TYPE(
|
|
|
|
'options',
|
|
|
|
'Object',
|
|
|
|
options));
|
|
|
|
}
|
|
|
|
const { signal, ref = true } = options;
|
2020-12-22 16:23:23 +01:00
|
|
|
try {
|
|
|
|
validateAbortSignal(signal, 'options.signal');
|
|
|
|
} catch (err) {
|
|
|
|
return PromiseReject(err);
|
2020-06-18 22:22:17 +02:00
|
|
|
}
|
|
|
|
if (typeof ref !== 'boolean') {
|
|
|
|
return PromiseReject(
|
|
|
|
new ERR_INVALID_ARG_TYPE(
|
|
|
|
'options.ref',
|
|
|
|
'boolean',
|
|
|
|
ref));
|
|
|
|
}
|
2021-04-15 12:11:05 +02:00
|
|
|
// TODO(@jasnell): If a decision is made that this cannot be backported
|
|
|
|
// to 12.x, then this can be converted to use optional chaining to
|
|
|
|
// simplify the check.
|
|
|
|
if (signal && signal.aborted) {
|
2020-11-29 19:01:24 +01:00
|
|
|
return PromiseReject(new AbortError());
|
2020-08-13 21:00:55 +02:00
|
|
|
}
|
2020-11-06 16:07:43 +01:00
|
|
|
let oncancel;
|
|
|
|
const ret = new Promise((resolve, reject) => {
|
2020-06-18 22:22:17 +02:00
|
|
|
const immediate = new Immediate(resolve, [value]);
|
|
|
|
if (!ref) immediate.unref();
|
|
|
|
if (signal) {
|
2020-11-15 18:12:43 +01:00
|
|
|
oncancel = FunctionPrototypeBind(cancelListenerHandler,
|
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
immediate, clearImmediate, reject);
|
2020-11-06 16:07:43 +01:00
|
|
|
signal.addEventListener('abort', oncancel);
|
2020-06-18 22:22:17 +02:00
|
|
|
}
|
|
|
|
});
|
2020-11-06 16:07:43 +01:00
|
|
|
return oncancel !== undefined ?
|
2021-05-12 12:16:43 +02:00
|
|
|
SafePromisePrototypeFinally(
|
2020-11-06 16:07:43 +01:00
|
|
|
ret,
|
|
|
|
() => signal.removeEventListener('abort', oncancel)) : ret;
|
2020-06-18 22:22:17 +02:00
|
|
|
}
|
|
|
|
|
2021-01-31 00:16:18 +01:00
|
|
|
async function* setInterval(after, value, options = {}) {
|
|
|
|
validateObject(options, 'options');
|
|
|
|
const { signal, ref = true } = options;
|
|
|
|
validateAbortSignal(signal, 'options.signal');
|
|
|
|
validateBoolean(ref, 'options.ref');
|
|
|
|
|
|
|
|
if (signal?.aborted)
|
|
|
|
throw new AbortError();
|
|
|
|
|
|
|
|
let onCancel;
|
|
|
|
let interval;
|
|
|
|
try {
|
|
|
|
let notYielded = 0;
|
|
|
|
let callback;
|
|
|
|
interval = new Timeout(() => {
|
|
|
|
notYielded++;
|
|
|
|
if (callback) {
|
|
|
|
callback();
|
|
|
|
callback = undefined;
|
|
|
|
}
|
2021-04-20 22:13:59 +02:00
|
|
|
}, after, undefined, true, ref);
|
2021-01-31 00:16:18 +01:00
|
|
|
insert(interval, interval._idleTimeout);
|
|
|
|
if (signal) {
|
|
|
|
onCancel = () => {
|
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
clearInterval(interval);
|
|
|
|
if (callback) {
|
|
|
|
callback(PromiseReject(new AbortError()));
|
|
|
|
callback = undefined;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
signal.addEventListener('abort', onCancel, { once: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!signal?.aborted) {
|
|
|
|
if (notYielded === 0) {
|
|
|
|
await new Promise((resolve) => callback = resolve);
|
|
|
|
}
|
|
|
|
for (; notYielded > 0; notYielded--) {
|
|
|
|
yield value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new AbortError();
|
|
|
|
} finally {
|
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
clearInterval(interval);
|
|
|
|
signal?.removeEventListener('abort', onCancel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-18 22:22:17 +02:00
|
|
|
module.exports = {
|
|
|
|
setTimeout,
|
|
|
|
setImmediate,
|
2021-01-31 00:16:18 +01:00
|
|
|
setInterval,
|
2020-06-18 22:22:17 +02:00
|
|
|
};
|