-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pause autoupdate connection on inactivitiy (#2851)
- Loading branch information
1 parent
d840303
commit af2f013
Showing
2 changed files
with
34 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { debounceTime, filter, firstValueFrom, fromEvent, map, Observable } from 'rxjs'; | ||
|
||
@Injectable({ | ||
providedIn: `root` | ||
}) | ||
export class WindowVisibilityService { | ||
public constructor() {} | ||
|
||
public async waitUntilVisible(): Promise<void> { | ||
if (document.visibilityState !== `visible`) { | ||
await firstValueFrom( | ||
fromEvent(document, `visibilitychange`).pipe(filter(() => document.visibilityState === `visible`)) | ||
); | ||
} | ||
} | ||
|
||
public hiddenFor(ms: number): Observable<void> { | ||
return fromEvent(document, `visibilitychange`).pipe( | ||
debounceTime(ms), | ||
filter(() => document.visibilityState === `hidden`), | ||
map(() => {}) | ||
); | ||
} | ||
} |