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,
|
2020-11-06 16:07:43 +01:00
|
|
|
PromisePrototypeFinally,
|
2020-06-18 22:22:17 +02:00
|
|
|
PromiseReject,
|
|
|
|
} = 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');
|
|
|
|
|
2020-12-22 16:23:23 +01:00
|
|
|
const { validateAbortSignal } = require('internal/validators');
|
|
|
|
|
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-01-04 11:39:35 +01:00
|
|
|
if (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 timeout = new Timeout(resolve, after, args, false, true);
|
|
|
|
if (!ref) timeout.unref();
|
|
|
|
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 ?
|
|
|
|
PromisePrototypeFinally(
|
|
|
|
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-01-04 11:39:35 +01:00
|
|
|
if (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 ?
|
|
|
|
PromisePrototypeFinally(
|
|
|
|
ret,
|
|
|
|
() => signal.removeEventListener('abort', oncancel)) : ret;
|
2020-06-18 22:22:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
setTimeout,
|
|
|
|
setImmediate,
|
|
|
|
};
|