Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/react-native/Libraries/Core/setUpXHR.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ polyfillGlobal('URL', () => require('../Blob/URL').URL);
polyfillGlobal('URLSearchParams', () => require('../Blob/URL').URLSearchParams);
polyfillGlobal(
'AbortController',
() => require('abort-controller/dist/abort-controller').AbortController, // flowlint-line untyped-import:off
() =>
require('../../src/private/webapis/dom/abort-api/AbortController')
.AbortController, // flowlint-line untyped-import:off
);
polyfillGlobal(
'AbortSignal',
() => require('abort-controller/dist/abort-controller').AbortSignal, // flowlint-line untyped-import:off
() =>
require('../../src/private/webapis/dom/abort-api/AbortSignal').AbortSignal, // flowlint-line untyped-import:off
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* @flow strict
* @format
*/
import {AbortSignal, abortSignal, createAbortSignal} from './AbortSignal';

/**
* The AbortController.
* @see https://dom.spec.whatwg.org/#abortcontroller
*/
export class AbortController {
/**
* Initialize this controller.
*/
constructor() {
signals.set(this, createAbortSignal());
}

/**
* Returns the `AbortSignal` object associated with this object.
*/
// $FlowExpectedError[unsafe-getters-setters]
get signal(): AbortSignal {
return getSignal(this);
}

/**
* Abort and signal to any observers that the associated activity is to be aborted.
*/
abort(reason: unknown): void {
abortSignal(reason, getSignal(this));
}
}

/**
* Associated signals.
*/
const signals = new WeakMap<AbortController, AbortSignal>();

/**
* Get the associated signal of a given controller.
*/
function getSignal(controller: AbortController): AbortSignal {
const signal = signals.get(controller);
if (signal == null) {
throw new TypeError(
`Expected 'this' to be an 'AbortController' object, but got ${
// $FlowExpectedError[invalid-compare]
controller === null ? 'null' : typeof controller
}`,
);
}
return signal;
}

// Properties should be enumerable.
//$FlowExpectedError[cannot-write]
Object.defineProperties(AbortController.prototype, {
signal: {enumerable: true},
abort: {enumerable: true},
});

if (typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol') {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this if can be removed

//$FlowExpectedError[cannot-write]
Object.defineProperty(AbortController.prototype, Symbol.toStringTag, {
configurable: true,
value: 'AbortController',
});
}
197 changes: 197 additions & 0 deletions packages/react-native/src/private/webapis/dom/abort-api/AbortSignal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/**
* @flow strict
* @format
*/
import DOMException from '../../errors/DOMException';
import Event from '../events/Event';
import EventTarget from '../events/EventTarget';
import {AbortController} from './AbortController';

const reasons = new WeakMap<AbortSignal, unknown>();

/**
* The signal class.
* @see https://dom.spec.whatwg.org/#abortsignal
*/
export class AbortSignal extends EventTarget {
/**
* AbortSignal.timeout static method
* Docs: https:developer.mozilla.org/en-US/docs/Web/API/AbortSignal/timeout_static
* Spec: https://dom.spec.whatwg.org/#dom-abortsignal-timeout
*/
static timeout(timeInMs: number): AbortSignal {
if (!(timeInMs >= 0)) {
throw new TypeError(
"Failed to execute 'timeout' on 'AbortSignal': The provided value have to be a non-negative number.",
);
}
const controller = new AbortController();
setTimeout(
() =>
controller.abort(new DOMException('signal timed out', 'TimeoutError')),
timeInMs,
);
return controller.signal;
}

/**
* 3. AbortSignal.any static method
* Docs: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/any_static
* Spec: https://dom.spec.whatwg.org/#dom-abortsignal-any
*/
static any(signals: AbortSignal[]): AbortSignal {
if (!Array.isArray(signals)) {
throw new Error('The signals value must be an instance of Array');
}

const controller = new AbortController();
const listeners = [];
const cleanup = () => listeners.forEach(unsubscribe => unsubscribe());

for (let i = 0; i < signals.length; i++) {
const signal = signals[i];

// Validate that each item is an AbortSignal
if (!(signal instanceof AbortSignal)) {
cleanup(); // Remove all listeners added so far
throw new Error(
'The "signals[' +
i +
']" argument must be an instance of AbortSignal',
);
}

// Abort immediately if one of the signals is already aborted
if (signal.aborted) {
cleanup(); // Remove all listeners added so far
controller.abort(signal.reason);
break;
}

const onAbort = () => {
controller.abort(signal.reason);
cleanup();
};
signal.addEventListener('abort', onAbort);
listeners.push(() => signal.removeEventListener('abort', onAbort));
}
return controller.signal;
}

/**
* AbortSignal cannot be constructed directly.
*/
constructor() {
super();
throw new TypeError('AbortSignal cannot be constructed directly');
}

/**
* Returns `true` if this `AbortSignal`'s `AbortController` has signaled to abort, and `false` otherwise.
*/
// $FlowExpectedError[unsafe-getters-setters]
get aborted(): boolean {
const aborted = abortedFlags.get(this);
if (typeof aborted !== 'boolean') {
throw new TypeError(
`Expected 'this' to be an 'AbortSignal' object, but got ${
// $FlowExpectedError[invalid-compare]
this === null ? 'null' : typeof this
}`,
);
}
return aborted;
}

// $FlowExpectedError[unsafe-getters-setters]
get reason(): unknown {
return reasons.get(this);
}

throwIfAborted(): void {
if (this.aborted) {
throw this.reason;
}
}
}

const listeners = new WeakMap<AbortSignal, () => void>();
Object.defineProperty(AbortSignal.prototype, `onabort`, {
enumerable: true,
configurable: true,
get() {
// $FlowExpectedError[object-this-reference]
return listeners.get(this) || null;
},
// $FlowExpectedError[missing-local-annot]
set(value) {
// $FlowExpectedError[object-this-reference]
const currentListener = listeners.get(this);
if (currentListener === value) return; // same handler? do nothing!
if (currentListener) {
// Before setting a new listener, remove the old one if exists
// $FlowExpectedError[object-this-reference]
this.removeEventListener('abort', currentListener);
}
if (typeof value === 'function') {
// $FlowExpectedError[object-this-reference]
listeners.set(this, value);
// $FlowExpectedError[object-this-reference]
this.addEventListener('abort', value);
} else {
// $FlowExpectedError[object-this-reference]
listeners.delete(this);
}
},
});

/**
* Create an AbortSignal object.
*/
export function createAbortSignal(): AbortSignal {
const signal = Object.create(AbortSignal.prototype);
// $FlowExpectedError[incompatible-type]
EventTarget.call(signal);
abortedFlags.set(signal, false);
return signal;
}

/**
* Abort a given signal.
*/
export function abortSignal(
reason: unknown | void = new DOMException(
'signal is aborted without reason',
'AbortError',
),
signal: AbortSignal,
): void {
if (abortedFlags.get(signal) !== false) {
return;
}

abortedFlags.set(signal, true);
reasons.set(signal, reason);
// $FlowExpectedError[incompatible-type]
signal.dispatchEvent(new Event('abort'));
}

/**
* Aborted flag for each instances.
*/
const abortedFlags = new WeakMap<AbortSignal, boolean>();

// Properties should be enumerable.
//$FlowExpectedError[cannot-write]
Object.defineProperties(AbortSignal.prototype, {
aborted: {enumerable: true},
reason: {enumerable: true},
});

// `toString()` should return `"[object AbortSignal]"`
if (typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol') {
Object.defineProperty(AbortSignal.prototype, Symbol.toStringTag, {
configurable: true,
value: 'AbortSignal',
});
}
Loading
Loading