data
data copied to clipboard
Aborting fetch requests
Would it be possible to set up a way to use an AbortController
to abort fetch requests?
I'm interested in having the ember-data fetches that are triggered in model hooks in Ember be aborted if the route transition is aborted.
If this is something wanted in ember-data
, I'd love to help implement it but I have no idea where to start, or if it's even possible right now.
@fusion2004 ember-data supports fetch
as a mechanism to make network requests.
See #6759 for some more info.
You would need https://github.com/ember-cli/ember-fetch installed.
An example of what you might need to make it work:
- set
useFetch: true
when extending the adapter to force ember-data to usefetch
rather than jQuery - pass
signal
tofetch
. E.g. by providingabortController.signal
tostore
method viaadapterOptions
and use it in adapter e.g. like so:
findRecord (store, type, id, snapshot) {
const url = this.buildURL(type.modelName, id, snapshot, 'findRecord');
const query = this.buildQuery(snapshot);
return this.ajax(url, 'GET', { data: query, signal: snapshot.adapterOptions.signal });
}
this is complicate because some requests, when aborted, will cascade errors through the store state that we likely aren't prepared for. We would want to add some handling in the fetch-manager we have internally to cleanup after an abort.
I am interested in the behavior, and I'd be happy to assist in a spike to prove we could do it, but would require an RFC to make fully public.
If error states on records/requests doesn't matter to your app though, you can totally do this today by implementing your own adapter(s) with an abort signal and sending it when you cancel a transition.
We hacked AbortController
support in Nomad using an approach similar to what @SergeAstapov suggested, but as @runspired said, we also had to specially handle the AbortError
.
Ultimately, since the store calls adapter#findRecord
and friends, this can't be handled in application code. We were in a good spot on 3.24 but (surprise surprise) upgrading to 3.28 caused our trick of returning an undefined
payload from findRecord
to start throwing assertion errors. Curiously, those assertions have been in ED for ages but something shifted and now they aren't being swallowed.
Point being, this behavior really needs to be implemented in Ember Data since it's Ember Data itself that calls adapters.
I think I'd be up for writing an RFC for this one. It might help me learn the internals better too.
I'd be interested as well @DingoEatingFuzz
Closing in favor of https://github.com/emberjs/rfcs/pull/860 which as AbortController built in