2020-05-23 01:11:14 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// Modeled very closely on the AbortController implementation
|
|
|
|
// in https://github.com/mysticatea/abort-controller (MIT license)
|
|
|
|
|
|
|
|
const {
|
2020-09-27 15:39:01 +00:00
|
|
|
ObjectAssign,
|
|
|
|
ObjectDefineProperties,
|
2020-11-12 14:26:06 +00:00
|
|
|
ObjectSetPrototypeOf,
|
2020-11-14 11:46:29 +00:00
|
|
|
ObjectDefineProperty,
|
2020-05-23 01:11:14 +00:00
|
|
|
Symbol,
|
2020-11-14 11:46:29 +00:00
|
|
|
SymbolToStringTag,
|
2020-11-12 14:26:06 +00:00
|
|
|
TypeError,
|
2020-05-23 01:11:14 +00:00
|
|
|
} = primordials;
|
|
|
|
|
|
|
|
const {
|
2020-10-30 22:38:26 +00:00
|
|
|
defineEventHandler,
|
2020-05-23 01:11:14 +00:00
|
|
|
EventTarget,
|
2020-10-26 11:56:48 +00:00
|
|
|
Event,
|
|
|
|
kTrustEvent
|
2020-05-23 01:11:14 +00:00
|
|
|
} = require('internal/event_target');
|
|
|
|
const {
|
|
|
|
customInspectSymbol,
|
|
|
|
} = require('internal/util');
|
|
|
|
const { inspect } = require('internal/util/inspect');
|
|
|
|
|
|
|
|
const kAborted = Symbol('kAborted');
|
|
|
|
|
|
|
|
function customInspect(self, obj, depth, options) {
|
|
|
|
if (depth < 0)
|
|
|
|
return self;
|
|
|
|
|
2020-09-27 15:39:01 +00:00
|
|
|
const opts = ObjectAssign({}, options, {
|
2020-05-23 01:11:14 +00:00
|
|
|
depth: options.depth === null ? null : options.depth - 1
|
|
|
|
});
|
|
|
|
|
|
|
|
return `${self.constructor.name} ${inspect(obj, opts)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
class AbortSignal extends EventTarget {
|
2020-11-12 14:26:06 +00:00
|
|
|
constructor() {
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
|
|
throw new TypeError('Illegal constructor');
|
|
|
|
}
|
|
|
|
|
2020-05-23 01:11:14 +00:00
|
|
|
get aborted() { return !!this[kAborted]; }
|
|
|
|
|
|
|
|
[customInspectSymbol](depth, options) {
|
|
|
|
return customInspect(this, {
|
|
|
|
aborted: this.aborted
|
|
|
|
}, depth, options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-27 15:39:01 +00:00
|
|
|
ObjectDefineProperties(AbortSignal.prototype, {
|
2020-05-23 01:11:14 +00:00
|
|
|
aborted: { enumerable: true }
|
|
|
|
});
|
|
|
|
|
2020-11-14 11:46:29 +00:00
|
|
|
ObjectDefineProperty(AbortSignal.prototype, SymbolToStringTag, {
|
|
|
|
writable: false,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
|
|
|
value: 'AbortSignal',
|
|
|
|
});
|
|
|
|
|
2020-11-02 20:59:54 +00:00
|
|
|
defineEventHandler(AbortSignal.prototype, 'abort');
|
|
|
|
|
2020-11-12 14:26:06 +00:00
|
|
|
function createAbortSignal() {
|
|
|
|
const signal = new EventTarget();
|
|
|
|
ObjectSetPrototypeOf(signal, AbortSignal.prototype);
|
|
|
|
signal[kAborted] = false;
|
|
|
|
return signal;
|
|
|
|
}
|
|
|
|
|
2020-05-23 01:11:14 +00:00
|
|
|
function abortSignal(signal) {
|
|
|
|
if (signal[kAborted]) return;
|
|
|
|
signal[kAborted] = true;
|
2020-10-26 11:56:48 +00:00
|
|
|
const event = new Event('abort', {
|
|
|
|
[kTrustEvent]: true
|
|
|
|
});
|
2020-05-23 01:11:14 +00:00
|
|
|
signal.dispatchEvent(event);
|
|
|
|
}
|
|
|
|
|
2020-07-14 15:36:56 +00:00
|
|
|
// TODO(joyeecheung): V8 snapshot does not support instance member
|
|
|
|
// initializers for now:
|
|
|
|
// https://bugs.chromium.org/p/v8/issues/detail?id=10704
|
|
|
|
const kSignal = Symbol('signal');
|
2020-05-23 01:11:14 +00:00
|
|
|
class AbortController {
|
|
|
|
constructor() {
|
2020-11-12 14:26:06 +00:00
|
|
|
this[kSignal] = createAbortSignal();
|
2020-05-23 01:11:14 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 15:36:56 +00:00
|
|
|
get signal() { return this[kSignal]; }
|
|
|
|
abort() { abortSignal(this[kSignal]); }
|
2020-05-23 01:11:14 +00:00
|
|
|
|
|
|
|
[customInspectSymbol](depth, options) {
|
|
|
|
return customInspect(this, {
|
|
|
|
signal: this.signal
|
|
|
|
}, depth, options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-27 15:39:01 +00:00
|
|
|
ObjectDefineProperties(AbortController.prototype, {
|
2020-05-23 01:11:14 +00:00
|
|
|
signal: { enumerable: true },
|
|
|
|
abort: { enumerable: true }
|
|
|
|
});
|
|
|
|
|
2020-11-14 11:46:29 +00:00
|
|
|
ObjectDefineProperty(AbortController.prototype, SymbolToStringTag, {
|
|
|
|
writable: false,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
|
|
|
value: 'AbortController',
|
|
|
|
});
|
|
|
|
|
2020-05-23 01:11:14 +00:00
|
|
|
module.exports = {
|
|
|
|
AbortController,
|
|
|
|
AbortSignal,
|
|
|
|
};
|