flex-layout: ObservableMedia: does not emit initial value in AOT mode

I’ve noticed a subtle difference in behavior in the ObservableMedia service when building with the CLI’s AOT mode being on vs off.

I have a component property that dictates the responsive display of a section of content, and it’s initialized via the subscription to the media change observable:

  constructor(private mediaQueryService: ObservableMedia) { }

  ngOnInit() {
      this.initializeFormOptions();
      this.initializeWatchers();
  }

  private initializeWatchers(): void {
    this.mediaWatcher = this.mediaQueryService
      .subscribe(mediaChange => {
        this.activeMediaViewport = mediaChange.mqAlias;
        this.lineEntryDisplayMode = this.calculateComponentDisplayMode(this.activeMediaViewport);
      });
  }

This works fine in non-AOT mode as the active mqAlias is available when the application loads. However, in AOT mode no value is emitted until a window resize event is triggered.

Since there is no direct query to the service available to get the current mqAlias, I am left with a less desirable brute force approach of querying individual possibilities using the isActive API method.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 23 (11 by maintainers)

Commits related to this issue

Most upvoted comments

We can reproduce the same issue with @angular/flex-layout: 6.0.0-beta.17 in our application and use RxJS startWith operator as a workaround:

this.mobile$ = this.mediaService.asObservable().pipe(
  map(mediaChange => mediaChange.mqAlias === 'xs'),
  startWith(this.mediaService.isActive('xs'))
);

I’m seeing an issue as well. Using ng serve and I have this little bit of code in one of my components:

this.media.subscribe(a => {
  console.log(a);
});

This does not log until I resize the window. I’d expect that it would log immediately when the app launches?

Edit: After more testing I can see if the screen is small or x-small the value emits immediately. Anything greater (medium, large, x-large) does not.

Am getting this same problem. Initial value sometimes fires, sometimes not.
@angular/flex-layout: 6.0.0-beta.16