Data: Aborting fetch requests

Created on 28 Apr 2020  路  2Comments  路  Source: emberjs/data

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.

Most helpful comment

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

All 2 comments

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

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.

Was this page helpful?
0 / 5 - 0 ratings