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

feat: Add partial sync execution to request interceptors #5759

Closed
wants to merge 1 commit into from

Conversation

dfidalg0
Copy link

@dfidalg0 dfidalg0 commented Jul 2, 2023

What is this about?

This PR makes the execution of request interceptors flagged as synchronous to always be synchronous as long as there isn't any async interceptors added after them.

Why is this relevant?

In Vue, the watchEffect function captures reactive dependencies synchronously within a function execution.

So, in order to trigger an update on a component within an axios call, all reactive bindings must be used synchronously.

Currently, if we use an axios instance with only sync interceptors, we can trigger updates by using reactive variables inside them, but if we add a single async interceptor (even if it will be executed after the sync ones), we lose this capability. A real world example is a header tracked by a Pinia store added synchronously before an access token is issued asynchronously to be set on the Authorization header.

Other frameworks might do something similar and sometimes we might need to perform an action synchronously before any request but also have some async tasks to do.

Considered cases

  • Instance without any request interceptors

Execution will be as usual. The adapter will be called synchronously and then the response interceptors will be called asynchronously one by one.

  • Instance with only sync request interceptors

Same as above. All interceptors being executed synchronously (and the adapter).

  • Instance with only async request interceptors

Same as above. All interceptors will be executed asynchronously, but the adapter will still be called synchronously.

  • Instance with sync interceptors added AFTER async ones

Since interceptors are executed in a LIFO way, we can execute the sync interceptors synchronously, then continue the execution as if all the next interceptors were async.

  • Instance with sync interceptors added BEFORE async ones

Since interceptors order of execution must be respected, those sync interceptors must be called after the async ones are resolved, which means we cannot call them synchronously, so we proceed to call the interceptor chain as if all interceptors were async.

@dfidalg0 dfidalg0 closed this May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant