-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Feature: AbortSignal support first value from last value from #6675
Closed
benlesh
wants to merge
3
commits into
ReactiveX:master
from
benlesh:feat-6442-signal-support-firstValueFrom-lastValueFrom
Closed
Changes from all commits
Commits
Show all changes
3 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 was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { createErrorClass } from './createErrorClass'; | ||
|
||
export interface AbortError extends Error {} | ||
|
||
export interface AbortErrorCtor { | ||
/** | ||
* @deprecated Internal implementation detail. Do not construct error instances. | ||
* Cannot be tagged as internal: https://github.com/ReactiveX/rxjs/issues/6269 | ||
*/ | ||
new (): AbortError; | ||
} | ||
|
||
/** | ||
* An error thrown when an abort signal is received and causes a promise to reject. | ||
* | ||
* @see {@link firstValueFrom} | ||
* @see {@link lastValueFrom} | ||
* | ||
* @class AbortError | ||
*/ | ||
export const AbortError: AbortErrorCtor = createErrorClass( | ||
(_super) => | ||
function AbortErrorImpl(this: any) { | ||
_super(this); | ||
this.name = 'AbortError'; | ||
this.message = 'Aborted by AbortSignal'; | ||
} | ||
); |
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,12 @@ | ||
import { Subscription } from '../Subscription'; | ||
|
||
export function linkSignalToSubscription(signal: AbortSignal, subscription: Subscription, onAbort: () => void) { | ||
const handler = () => { | ||
subscription.unsubscribe(); | ||
onAbort(); | ||
}; | ||
signal.addEventListener('abort', handler, { once: true }); | ||
subscription.add(() => { | ||
signal.removeEventListener('abort', handler); | ||
}); | ||
} |
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 that it should be tagged as "internal" because IMO this interface (and the
AbortError
class) shouldn't be publicly exported.IMO if a user wants to check whether a Promise rejection was caused by an Abortion, the right way to check that would be:
e instanceof Error && e.name === 'AbortError'
.What would happen, otherwise, if all libraries that want to support
AbortSignal
export their own version ofAbortError
? A user could accidentally import theAbortError
instance from a different library and then trying to doe instanceof AbortError
would return false for a rejection caused by anAbortSignal
. That's why I think that in order to avoid that kind of ambiguity it's probably better if libraries that want to supportAbortSignal
do not expose theirAbortError
classes, and treat them as an internal implementation detail.Although, I would be curios to know what @benjamingr thoughts are in this regard, because I'm sure that he must have already put some thought on what's the best way to detect rejections caused by an
AbortSignal
.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.
@josepot I wish I had a good answer but WHATWG are pretty much in "you shouldn't do that" camp and have recently made a change that means you can
controller.abort(anyError)
and then you are expected tothrow signal.reason
.This is quite frustrating to me (possibly because I don't understand it well) but I don't have the capacity to engage with them and resolve it quite now.
Ref: whatwg/dom#1033
If this is important to RxJS - please also say it there.
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.
@benjamingr thanks for your response! After I read all the comments in the issue that you shared, I ended up spending a lot of time trying to understand the history behind
AbortController
. Let's just say that I have a lot of thoughts and opinions about it 😅. However, in order to stay constructive, I will keep those thoughts to myself and I will try to propose a solution forRxJS
that I think that should work for all users, despite the current (and hopefully temporary) misalignment between the DOM spec and the NodeJS API:RxJS should keep its
AbortError
class as an internal implementation detail, since AFAIK doinge instanceof AbortError
has never been the recommended way of identifying theAbortError
exception.When the
onAbort
event happens, then RxJS should first check whethersignal.hasOwnProperty('reason')
, and then it should reject using that reason if it exists, otherwise it should use a new instance of its internalAbortError
as a fallback.Rejecting abortions this way would guarantee that the RxJS implementation works for both kinds of ursers: DOM and NodeJS. Given the current state of things, I think that's the most sensible approach to stay compliant with the current misalignment.
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.
For reference nodejs/node#41008