-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[embeddable] centralize should fetch embeddable observable logic (#15…
…1799) Fixes #151223 and #151128 PR does the following 1) creates `shouldFetch$` method that centralizes logic for checking when an embeddable should fetch. 2) updates Lens and Maps embeddable to use `shouldFetch$` 3) Adds unit tests for Maps embeddable to capture behavior of unique edge cases --------- Co-authored-by: kibanamachine <[email protected]>
- Loading branch information
1 parent
b833b10
commit afb251c
Showing
9 changed files
with
381 additions
and
57 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
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
44 changes: 44 additions & 0 deletions
44
src/plugins/embeddable/public/lib/filterable_embeddable/should_fetch.tsx
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,44 @@ | ||
/* | ||
* 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 fastIsEqual from 'fast-deep-equal'; | ||
import { Observable } from 'rxjs'; | ||
import { map, distinctUntilChanged, skip, startWith } from 'rxjs/operators'; | ||
import { COMPARE_ALL_OPTIONS, onlyDisabledFiltersChanged } from '@kbn/es-query'; | ||
import type { FilterableEmbeddableInput } from './types'; | ||
|
||
export const shouldRefreshFilterCompareOptions = { | ||
...COMPARE_ALL_OPTIONS, | ||
// do not compare $state to avoid refreshing when filter is pinned/unpinned (which does not impact results) | ||
state: false, | ||
}; | ||
|
||
export function shouldFetch$< | ||
TFilterableEmbeddableInput extends FilterableEmbeddableInput = FilterableEmbeddableInput | ||
>( | ||
updated$: Observable<unknown>, | ||
getInput: () => TFilterableEmbeddableInput | ||
): Observable<TFilterableEmbeddableInput> { | ||
return updated$.pipe(map(() => getInput())).pipe( | ||
// wrapping distinctUntilChanged with startWith and skip to prime distinctUntilChanged with an initial input value. | ||
startWith(getInput()), | ||
distinctUntilChanged((a: TFilterableEmbeddableInput, b: TFilterableEmbeddableInput) => { | ||
if ( | ||
!fastIsEqual( | ||
[a.searchSessionId, a.query, a.timeRange, a.timeslice], | ||
[b.searchSessionId, b.query, b.timeRange, b.timeslice] | ||
) | ||
) { | ||
return false; | ||
} | ||
|
||
return onlyDisabledFiltersChanged(a.filters, b.filters, shouldRefreshFilterCompareOptions); | ||
}), | ||
skip(1) | ||
); | ||
} |
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
Oops, something went wrong.