fix(breakpoint-observer): fix the breakpoint observer emit count and accuracy #15964
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The breakpoint observer emits multiple and incorrect states when more than one query changes.
Debounce the observer emissions to eliminate the incorrect states and emit once.
References #10925 #15868
I have reproduced this in Chrome, Firefox, Safari, Edge, and IE. It occurs because the media query API fires an event when a query match changes. The breakpoint observer emits once for each event, updating the state of one query each time. Therefore, the observer may emit multiple times for one change and the reported intermediary states can be incorrect. I have reproduced up to five emissions for a single change.
https://stackblitz.com/edit/angular-6pmabc
PR #11007 attempted to resolve this issue. However, the issue still reproduces. My understanding is that the AsapScheduler uses a microtask which fails to debounce the media query events. Using the AsyncScheduler resolves the issue. AsyncScheduler uses a timer, setInterval, that may technically be slower on the first bounce but is more efficient across multiple.