Closed wmcginty closed 2 years ago
@tylermilner @earlgaspard @GrandLarseny thoughts?
Another thing I've been thinking about recently that could work well with a system like this is priority. I've got a use case at the moment where the URLSessionTask.Priority
of each of my requests can change drastically based on a user's scroll position (if it shouldn't be cancelled altogether). After a quick think, a token
like system that represents the task in flight could work as well.
Just a thought 🤷♂️
OK, as promised here is a draft PR for what a revamp to the cancellation mechanism might look like. I have somewhat implemented a
CancellationSource
into theNetworkService
and by extension theBackendService
.1) When executing the request, keep and store a
CancellationSource
:At this point we could definitely provide a customization point into the creation of the token if needed, for instance:
Inside Hyperspace, the
BackendService
simply passes theCancellationSource.Token
into theNetworkService
where it is used in generation of the request:At this point, all you have to do from the client of the
BackendService
has to do is call:This will cancel all the tokens that have been generated by the given
source
. In this specific case, this results in a result of :.failure(cancelled)