flex-layout icon indicating copy to clipboard operation
flex-layout copied to clipboard

Initial value of ObservableMedia not received

Open erikkolo opened this issue 9 years ago • 9 comments

If I subscribe to ObservableMedia, the first (initial) value ist not received. plunker.

The plunker example never receives the first value. In my own app, I get this first value in "xs" and "sm" size. If my browser windows is larger (gt-sm) at the time of loading the app (or refreshing), I do not get the first value.

erikkolo avatar Apr 04 '17 21:04 erikkolo

Dupe: https://github.com/angular/flex-layout/issues/240

intellix avatar Apr 04 '17 23:04 intellix

https://github.com/angular/flex-layout/issues/240 was closed despite the fact that this is an issue that more than one user is experiencing.

rolandjitsu avatar Apr 18 '17 17:04 rolandjitsu

I also think I know what the issue is (or might be). Right now, I'm subscribing to the ObservableMedia in a ngOnInit() block, whereas if I subscribe to it in a constructor it seems to be triggered. So I wonder if the ObservableMedia acts as a Subject or BehaviorSubject?

The behaviour I'm experiencing leads to the conclusion it's probably not a BehaviorSubject, but it should be. As a user, when I subscribe to it, I'd expect to get the last registered value.

I haven't looked at the actual implementation, but I guess this might be the reason why some users don't see a value on subscribe, or at least that's why I don't.

rolandjitsu avatar Apr 18 '17 19:04 rolandjitsu

As a possible workaround I created an intermediary service that keeps MediaChange value in a ReplaySubject and it's initialized in AppComponent. So you can subscribe on MediaReplay.subject$ within your app.

export class MediaReplay {
    private _media$: ReplaySubject<MediaChange> = new ReplaySubject(1);

    constructor(media:ObservableMedia) {
        media.asObservable()
            .subscribe(res => this._media$.next(res), err => this._media$.error(err), () => this._media$.complete());
    }

    get media$(): Observable<MediaChange> {
        return this._media$.asObservable();
    }
}

export class AppComponent {
    constructor(mediaReplay: MediaReplay) {} //so it's initialized when the app starts
}

karser avatar Apr 20 '17 22:04 karser

IMHO this is a quite serious issue. It makes the JavaScript API unreliable. Are the developers going to fix this anytime soon?

jurafa avatar Jun 07 '17 07:06 jurafa

This will be fixed by next week and included in the upcoming Beta.9 Release.

ThomasBurleson avatar Jun 07 '17 19:06 ThomasBurleson

ObservableMedia uses BehaviorSubject internally; however it only reports breakpoint activations. This issue 245 is a bug. We are investigating.

ThomasBurleson avatar Jun 07 '17 20:06 ThomasBurleson

Will be fixed with PR #313.

See working plunker: https://plnkr.co/edit/yylQr2IdbGy2Yr00srrN?p=preview

ThomasBurleson avatar Jun 10 '17 17:06 ThomasBurleson

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.