-
Notifications
You must be signed in to change notification settings - Fork 16
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
On using AbortSignal
instead of Subscription
#19
Comments
Agreed. The current IDL sketch does already expose |
Well, in RxJS, we have The works roughly as follows: add(teardown: Subscription | () => void) {
if (teardown === this) return;
if (this.closed) {
if (typeof teardown === 'function') {
teardown();
} else {
teardown.unsubscribe();
}
} else {
if (!this.teardowns.has(teardown)) {
this.teardowns.add(teardown);
if (isSubscription(teardown)) {
// make sure the child removes itself from the parent
// if it unsubscribes first.
teardown.add(() => {
this.teardowns.remove(teardown);
})
}
}
}
}
remove(teardown: Subscription | () => void) {
this.teardowns.delete(teardown);
} This is just for reference. The "chaining" part is particularly useful when composing observables and building custom operations. |
Honestly, this |
+1 to adding features to In particular, |
@benlesh AbortSignal support |
@benjamingr that is very nice and helpful. I'll update the reference implementation to use that. The only thing I see Precedent for this would be But a simple Basically something like: AbortSignal.prototype.whenAborted = function (callback, { signal }) {
if (this.aborted) {
callback();
} else {
this.#abort ??= this.on('abort').take(1);
this.#abort.subscribe({ next: callback, signal });
}
} |
For |
FWIW I do prefer AbortController/AbortSignal over Subscription, however I'm sympathetic to the foot-gun-yness of passing in an already-aborted signal and accidentally not handling that case. I think I'd be open to adding Does that seem reasonable? Alternatively we could just leave this alone and purely go the modify-AbortSignal-instead route; I'm pretty open to both. |
I think that's a reasonable compromise. |
@bakkot Honestly, zalgo isn't the biggest concern here, the bigger concern is making sure that any resource (which can be presumed at design time will be "expensive") should be torn down as soon as possible, even if the developer screws up somehow. Generally, no one would be counting on a synchronous side effect inside of an |
Why would you not also want tear down when you're done with the |
I think you would want the teardown to run when the Observable is "done". At least that's what #61 aims to do. The idea is that tear down would end once the observable signals completion or error (for producer-initiated teardown/completion) or when the consumer "unsubscribes" via aborting. Does that make sense? |
I guess I don't understand why a signal would indicate completion. That's not part of abort signals. |
Signal doesn't indicate completion. Signal abortion indicates the consumer of an Observable canceling its interest in the Observable and triggering the supplied teardown function. On the other hand if the Observable calls the subscriber's |
I don't see that in #61? If I look at the logic for what it is supposedly sugar for, the complete case does not seem covered? |
Ah, yeah I guess my saying that |
I'm TOTALLY okay with it, and it solves a lot of problems one might have with the "synchronous firehose" issue that comes with dealing with potentially synchronous observables.
However, if we're doing that, you'd want to have the signal passed through the
subscriber
in the constructor in some way, so you can send it along to any inner subscriptions.It's also worth noting that registering teardown logic with
AbortSignal
is cumbersome and very slow. It might be nice to have a better way to do that.Finally, if we're going that route, the "teardown return" in the constructor is no longer necessary.
Examples:
We may wish to add a custom method to
Subscriber
that does that abort registration for users:The text was updated successfully, but these errors were encountered: