forked from reduxjs/redux-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(createAsyncThunk): signal.reason contains the first argument pro…
…vided to asyncThunkHandle.abort reduxjs#2395 Other changes: fixes signal.aborted not set in AbortController shim
- Loading branch information
1 parent
b0f59fc
commit b72d7f5
Showing
9 changed files
with
146 additions
and
79 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,43 @@ | ||
/** | ||
* @internal | ||
* At the time of writing `lib.dom.ts` does not provide `abortSignal.reason`. | ||
*/ | ||
export type AbortSignalWithReason<T> = AbortSignal & { reason?: T } | ||
|
||
/** | ||
* Calls `abortController.abort(reason)` and patches `signal.reason`. | ||
* if it is not supported. | ||
* | ||
* At the time of writing `signal.reason` is available in FF chrome, edge node 17 and deno. | ||
* @param abortController | ||
* @param reason | ||
* @returns | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/reason | ||
*/ | ||
export const abortControllerWithReason = <T>( | ||
abortController: AbortController, | ||
reason: T | ||
): void => { | ||
type Consumer<T> = (val: T) => void | ||
|
||
const signal = abortController.signal as AbortSignalWithReason<T> | ||
|
||
if (signal.aborted) { | ||
return | ||
} | ||
|
||
// Patch `reason` if necessary. | ||
// - We use defineProperty here because reason is a getter of `AbortSignal.__proto__`. | ||
// - We need to patch 'reason' before calling `.abort()` because listeners to the 'abort' | ||
// event are are notified immediately. | ||
if (!('reason' in signal)) { | ||
Object.defineProperty(signal, 'reason', { | ||
enumerable: true, | ||
value: reason, | ||
configurable: true, | ||
writable: true, | ||
}) | ||
} | ||
|
||
;(abortController.abort as Consumer<typeof reason>)(reason) | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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