data icon indicating copy to clipboard operation
data copied to clipboard

Aborting fetch requests

Open fusion2004 opened this issue 4 years ago • 4 comments

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 avatar Apr 28 '20 21:04 fusion2004

@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:

  1. set useFetch: true when extending the adapter to force ember-data to use fetch rather than jQuery
  2. pass signal to fetch. E.g. by providing abortController.signal to store method via adapterOptions 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 });
}

SergeAstapov avatar May 28 '20 01:05 SergeAstapov

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.

runspired avatar May 22 '21 01:05 runspired

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.

DingoEatingFuzz avatar Apr 27 '22 21:04 DingoEatingFuzz

I'd be interested as well @DingoEatingFuzz

pjcarly avatar May 11 '22 15:05 pjcarly

Closing in favor of https://github.com/emberjs/rfcs/pull/860 which as AbortController built in

runspired avatar Nov 17 '22 02:11 runspired