Created
March 23, 2021 15:14
-
-
Save MarsiBarsi/9061abc75cd12f20669e5b3fbd848f70 to your computer and use it in GitHub Desktop.
Revisions
-
MarsiBarsi created this gist
Mar 23, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ import {DOCUMENT} from '@angular/common'; import {inject, InjectionToken} from '@angular/core'; import {fromEvent, Observable} from 'rxjs'; import {distinctUntilChanged, map, share, startWith} from 'rxjs/operators'; export const PAGE_VISIBILITY = new InjectionToken<Observable<boolean>>( 'Shared Observable based on `document visibility changed`', { factory: () => { const documentRef = inject(DOCUMENT); return fromEvent(documentRef, 'visibilitychange').pipe( startWith(0), map(() => documentRef.visibilityState !== 'hidden'), distinctUntilChanged(), share(), ); }, }, );