-
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
Cancellable forEach #3977
Cancellable forEach #3977
Conversation
- Adds documentation - Deprecates promise constructor
src/internal/Observable.ts
Outdated
forEach(next: (value: T) => void, promiseCtor?: PromiseConstructorLike): Promise<void> { | ||
forEach(next: (value: T) => void, promiseCtorOrSubscription?: PromiseConstructorLike|Subscription): Promise<void> { | ||
let promiseCtor: PromiseConstructorLike; | ||
let subs = isSubscription(promiseCtorOrSubscription) ? promiseCtorOrSubscription : undefined; | ||
promiseCtor = getPromiseCtor(promiseCtor); |
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.
Shouldn't promiseCtorOrSubscription
be passed here? In the refactoring, the promiseCtor
that's passed to getPromiseCtor
will always be undefined
.
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.
updated.
spec/Observable-spec.ts
Outdated
// Since the consuming code can no longer interfere with the synchronous | ||
// producer, the remaining results are nexted. | ||
expect(results).to.deep.equal([1, 2, 3, 4, expected]); | ||
expect(results).to.deep.equal([1, 2, 3, expected]); |
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.
The change to this test's expectation suggests that the behaviour of the previous, non-cancellable implementation was actually a bug - as next
was called despite an error being effected. The new behaviour makes more sense to me, as an error effected from next
sees the observer unsubscribe.
Could the comment be removed? It's no longer applicable, as the remaining results are not nexted.
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.
Yup. I'll make sure to add the fix to the changelog too.
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.
LGTM.
IIRC, the consensus in the meeting was that it would be good to throw an instance of a specific error class when an explicit unsubscribe is performed.
Is that something that would be done in this PR or in a separate PR?
…by passed Subscription
@cartant ... I'm having it reject with an |
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.
LGTM
What I am worried about is forward compatibility with whatever cancellation primitive makes it into the spec. We already have AbortSignal in the DOM spec, and there is ongoing work to get a primitive into ES, which I would love love love to see supported seamlessly in rxjs. |
I'm not worried about this in the slightest. We can deprecate and pivot if we need to, but honestly, I have yet to see a proposal or cancellation type that isn't compatible with what we have here. The committees are slow, and unlikely to pass anything related to any of this. CancellationToken failed once. Observable is stuck at stage 1 forever. |
forEach
, by allowing aSubscription
to be passed as a sort of cancellation token.forEach
. (That can be configured globally, and I'd like to remove it for v7)Behavior Described
You can read the docs in the PR for this, but basically:
If you call
forEach
with a second argument that is aSubscription
, that subscription will act as a cancellation token, such that if youunsubscribe
from it, the forEach's returned promise will reject with anError
with the messageObservable forEach unsubscribed
. This is done, so there's a way to gracefully handle cancellations in async-await code, and I think provides a better alternative totakeUntil
in those use cases.