-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Replace abort-controller that has no update 7 years with React Native fork
#57230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
retyui
wants to merge
2
commits into
react:main
Choose a base branch
from
retyui:feat/retyui/abort-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
packages/react-native/src/private/webapis/dom/abort-api/AbortController.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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') { | ||
| //$FlowExpectedError[cannot-write] | ||
| Object.defineProperty(AbortController.prototype, Symbol.toStringTag, { | ||
| configurable: true, | ||
| value: 'AbortController', | ||
| }); | ||
| } | ||
197 changes: 197 additions & 0 deletions
197
packages/react-native/src/private/webapis/dom/abort-api/AbortSignal.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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', | ||
| }); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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