Skip to content

Could the concepts of cancellation, closure, and unsubscription be merged? #23

@dead-claudia

Description

@dead-claudia

This is really a shot in the dark, and I expect some initial resistance, but hear me out on this one.

I was thinking you could merge cancelling a request with closing a socket. I know these are two separate concepts, but they are heavily intertwined in practice.

  • When you cancel a request, you frequently are also closing a socket of some kind. This won't come up often in the browser since it's all done in C++ land, but in Node and other environments that define the sockets mostly in JS, that cancel token would be closing the socket alongside the request.
  • When you close a stream, if that stream is more than a simple read/map/write pipeline, it's often also performing async tasks that you might wish to cancel. When reading these streams, you may have to await the next buffer as well, and being able to cancel that request, too, would be nice. So instead of creating a new synthetic cancel token, you could just pass around the same one you received to all places that need it.

This also extends to observables: you could subscribe with a close token instead of just subscribing and receiving an object with an unsubscribe() method and a close property - the close token would already have this info for you.

Also, informally, I've seen these used quite interchangeably, with a heavy bias towards "close":

  • I've heard "close a request", "cancel a request", and "unsubscribe from a request" all three used interchangeably in numerous circles, ranging from IT professionals to seasoned web developers.
  • I've heard "close a stream" and occasionally "abort a stream" or "unsubscribe from a stream" with reference to various streams of data from an even broader group of people. The last is mostly used for when you don't control the stream, but the first two seem preferred in my experience when you control the stream.
  • I've heard "close a subscription", "cancel a subscription", and "unsubscribe from an observable" all three interchangeably among web devs, with the first two common even among non-technical people.

You probably notice I went from "cancel token" to "close token", talking about "closure" instead of "cancellation", and that's on purpose: I'm thinking you could merge cancellation and stream closure to a more abstract "close" operation. You could keep the existing cancel token API or something similar, but I feel there's a lot to be gained by having only one concept instead of 3.

  • Any syntax assist would benefit all three variants, not just promises.
  • There's a sharply reduced need to split them apart and recombine them when dealing with streams for data processing.
  • More complex observables and userland streams could have much simpler closure semantics - they could mostly do the right thing implicitly, up to and including cancelling requests and child observables/streams.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions