question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BreakpointObserver doesn't emit immediately

See original GitHub issue

Bug, feature request, or proposal:

Bug

What is the expected behavior?

In v6, BreakpointObserver immediately emits when subscribed to

What is the current behavior?

In v7, BreakpointObserver does not emit immediately.

What are the steps to reproduce?

https://stackblitz.com/edit/angular-material2-issue-breakpointobserver

The initial code is using CDK 6.4.7. The console prints only true and string. If you swap the contents of package.json with package.json.2, this bumps up the version to 7.0.0. The console will prints null, then true. The behaviour of | async has not changed, evidenced by the fact that string is still printed in the same way.

What is the use-case or motivation for changing an existing behavior?

This causes issues in any code that was not using a strict negation check (eg. !(isMobile$ | async)), as the check becomes truthy temporarily. As an example, this code:

https://stackblitz.com/edit/angular-material2-issue-breakpointobserver-usecase

this.isMobile$ = this.breakpointObserver
  .observe(Breakpoints.XSmall)
  .pipe(map(obs => obs.matches));
this.isNotMobile$ = this.breakpointObserver
  .observe(Breakpoints.XSmall)
  .pipe(map(obs => !obs.matches));
<div *ngIf="!(isMobile$ | async)">
  {{ foo$ | async }}
</div>
<div *ngIf="(isNotMobile$ | async)">
  {{ bar$ | async }}
</div>

You would expect the two to have the same functionality: nothing is displayed for mobile, and everything is displayed for non-mobile. This is exactly what happens if you move between mobile and non-mobile.

However, if you load it in mobile view (ie. when nothing should show), foo$ is still subscribed to (you can tell by the log), because breakpointObserver doesn’t emit immediately and isMobile$ | async === null, which is false-y. This can cause unnecessary network requests to be fired, for example.

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

@angular/cdk 7.0.0

Is there anything else we should know?

This isn’t necessarily a bad thing - any problematic behaviour caused can be fixed with strict checks or using startWith, however given it seems to be an undocumented side-effect of #11007 , I’m not sure if it’s intended or not

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
manklucommented, Oct 29, 2018

You shouldn’t wrap your observable with a function. If you use the observable directly it works as expected.

https://stackblitz.com/edit/angular-material2-issue-breakpointobserver-vfv5jl

For your sample, something like the following seems to work:

this.isNotMobile$ = this.isMobile$
  .pipe(map(matches => !matches))
<ng-container *ngIf="(isNotMobile$ | async) && (data$ | async) as data">
  <!-- use data -->
</ng-container>

https://stackblitz.com/edit/angular-material2-issue-breakpointobserver-lyaaca

0reactions
angular-automatic-lock-bot[bot]commented, Jun 27, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

angular - BreakpointObserver not firing - Stack Overflow
I have set up my BreakpointObserver as follows, but it doesn't seem to be firing when ever my page loads. My page is...
Read more >
Introduction to Angular CDK's BreakpointObserver - Morioh
The Angular CDK's LayoutModule has a BreakpointObserver class that you can ... and it will emit each time you cross the threshold of...
Read more >
Angular Responsive Design: Complete Guide
Notice that there is a breakpoints map emitted by the BreakpointObserver service, that contains all the currently matching breakpoints, and we ...
Read more >
How To Detect Breakpoints Using the Angular CDK
Learn how to detect viewport size changes or matches against media queries using the BreakpointObserver and MediaMatcher services from the ...
Read more >
Angular Material & Breakpoints…. details they didn't explain in ...
BreakpointObserver is a utility for evaluating media queries and ... to get an observable stream that will emit whenever one of the given ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found