Skip to content

Instantly share code, notes, and snippets.

@MarsiBarsi
Created March 23, 2021 15:14
Show Gist options
  • Select an option

  • Save MarsiBarsi/9061abc75cd12f20669e5b3fbd848f70 to your computer and use it in GitHub Desktop.

Select an option

Save MarsiBarsi/9061abc75cd12f20669e5b3fbd848f70 to your computer and use it in GitHub Desktop.

Revisions

  1. MarsiBarsi created this gist Mar 23, 2021.
    20 changes: 20 additions & 0 deletions token.ts
    Original 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(),
    );
    },
    },
    );