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

fix(zone.js): patch Response methods returned by fetch #50653

Closed
wants to merge 1 commit into from

Conversation

arturovt
Copy link
Contributor

@arturovt arturovt commented Jun 10, 2023

This commit updates the implementation of the fetch patch and additionally patches Response methods which return promises. These are arrayBuffer, blob, formData, json and text. This fixes the issue when zone becomes stable too early before all of the fetch tasks complete. Given the following code:

appRef.isStable.subscribe(console.log);
fetch(...).then(response => response.json()).then(console.log);

The isStable observer would log false, true, false, true. This was happening because json() was returning a native promise (and not a ZoneAwarePromise). But calling then on the native promise returns a ZoneAwarePromise which notifies Angular about the task being scheduled and forces to re-calculate the isStable state.

Closes: #50327

@pullapprove pullapprove bot requested a review from JiaLiPassion June 10, 2023 21:33
@AndrewKushnir AndrewKushnir added action: review The PR is still awaiting reviews from at least one requested reviewer area: zones area: server Issues related to server-side rendering target: patch This PR is targeted for the next patch release labels Jun 11, 2023
@ngbot ngbot bot added this to the Backlog milestone Jun 11, 2023
Copy link
Contributor

@JiaLiPassion JiaLiPassion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit updates the implementation of the `fetch` patch and additionally
patches `Response` methods which return promises. These are `arrayBuffer`, `blob`,
`formData`, `json` and `text`. This fixes the issue when zone becomes stable too early
before all of the `fetch` tasks complete. Given the following code:
```ts
appRef.isStable.subscribe(console.log);
fetch(...).then(response => response.json()).then(console.log);
```
The `isStable` observer would log `false, true, false, true`. This was happening because
`json()` was returning a native promise (and not a `ZoneAwarePromise`). But calling `then`
on the native promise returns a `ZoneAwarePromise` which notifies Angular about the task
being scheduled and forces to re-calculate the `isStable` state.

Issue: angular#50327
Copy link
Contributor

@JiaLiPassion JiaLiPassion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks.

@AndrewKushnir AndrewKushnir added action: presubmit The PR is in need of a google3 presubmit action: global presubmit The PR is in need of a google3 global presubmit and removed action: review The PR is still awaiting reviews from at least one requested reviewer action: presubmit The PR is in need of a google3 presubmit labels Jan 31, 2024
@AndrewKushnir
Copy link
Contributor

Presubmit (TGP).

@AndrewKushnir AndrewKushnir added action: merge The PR is ready for merge by the caretaker and removed action: global presubmit The PR is in need of a google3 global presubmit labels Jan 31, 2024
jessicajaniuk pushed a commit that referenced this pull request Jan 31, 2024
This commit updates the implementation of the `fetch` patch and additionally
patches `Response` methods which return promises. These are `arrayBuffer`, `blob`,
`formData`, `json` and `text`. This fixes the issue when zone becomes stable too early
before all of the `fetch` tasks complete. Given the following code:
```ts
appRef.isStable.subscribe(console.log);
fetch(...).then(response => response.json()).then(console.log);
```
The `isStable` observer would log `false, true, false, true`. This was happening because
`json()` was returning a native promise (and not a `ZoneAwarePromise`). But calling `then`
on the native promise returns a `ZoneAwarePromise` which notifies Angular about the task
being scheduled and forces to re-calculate the `isStable` state.

Issue: #50327

PR Close #50653
@jessicajaniuk
Copy link
Contributor

This PR was merged into the repository by commit 260d3ed.

@arturovt arturovt deleted the fix/50327 branch January 31, 2024 18:01
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Mar 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: server Issues related to server-side rendering area: zones target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fetch only partially patched.
4 participants