The value of document.visibilityState returned by the mapTo() operator when used as a pipe with an observable of the document.visibilitychange event is not as expected.
fromEvent(document, 'visibilitychange')
.pipe(
mapTo(document.visibilityState)
)
.subscribe((val) => {
console.log(val, document.visibilityState);
});
Example pen: https://codepen.io/nametoforget/pen/ZEQmdBj
See console log in pen.
mapTocaptures return value once at the beginning and use this initial value all the time. If you want to have most recent value usemapinstead: