Skip to content
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

Aborting fetch requests #7152

Closed
fusion2004 opened this issue Apr 28, 2020 · 5 comments
Closed

Aborting fetch requests #7152

fusion2004 opened this issue Apr 28, 2020 · 5 comments

Comments

@fusion2004
Copy link

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.

@SergeAstapov
Copy link
Contributor

SergeAstapov commented May 28, 2020

@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 });
}

@runspired
Copy link
Contributor

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.

@DingoEatingFuzz
Copy link
Contributor

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.

@pjcarly
Copy link

pjcarly commented May 11, 2022

I'd be interested as well @DingoEatingFuzz

@runspired
Copy link
Contributor

Closing in favor of emberjs/rfcs#860 which as AbortController built in

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

No branches or pull requests

5 participants