-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pause refresh when page is hidden (#177693)
## Summary close #1878 Pauses auto-refresh when a page is not visible. I tested and didn't notice any issues, but looking for more testing help ## Release Notes Auto-refresh pauses when the page is not visible.
- Loading branch information
Showing
4 changed files
with
93 additions
and
2 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
28 changes: 28 additions & 0 deletions
28
src/plugins/data/public/query/timefilter/lib/page_visibility.test.ts
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,28 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { createPageVisibility$ } from './page_visibility'; | ||
|
||
let mockPageVisibility: DocumentVisibilityState = 'visible'; | ||
jest.spyOn(document, 'visibilityState', 'get').mockImplementation(() => mockPageVisibility); | ||
|
||
test('createPageVisibility$ returns an observable that emits visibility state', () => { | ||
const pageVisibility$ = createPageVisibility$(); | ||
|
||
const fn = jest.fn(); | ||
pageVisibility$.subscribe(fn); | ||
|
||
expect(fn).toHaveBeenCalledTimes(1); | ||
expect(fn).toHaveBeenLastCalledWith('visible'); | ||
|
||
mockPageVisibility = 'hidden'; | ||
document.dispatchEvent(new Event('visibilitychange')); | ||
|
||
expect(fn).toHaveBeenCalledTimes(2); | ||
expect(fn).toHaveBeenLastCalledWith('hidden'); | ||
}); |
18 changes: 18 additions & 0 deletions
18
src/plugins/data/public/query/timefilter/lib/page_visibility.ts
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,18 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { fromEvent, Observable } from 'rxjs'; | ||
import { map, shareReplay, startWith } from 'rxjs/operators'; | ||
|
||
export function createPageVisibility$(): Observable<DocumentVisibilityState> { | ||
return fromEvent(document, 'visibilitychange').pipe( | ||
startWith(document.visibilityState), | ||
map(() => document.visibilityState), | ||
shareReplay({ refCount: true, bufferSize: 1 }) | ||
); | ||
} |