-
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.
[Controls] Move diffing from the dashboard to the control group (#175146
) Closes #174703 ## Summary Currently, the burden of determining unsaved changes is handled entirely by the Dashboard - this means that we need special logic for comparing the state of every piece of a dashboard, including the controls and the panels. Once the [embeddable refactor](#167429) work is complete, this will no longer be the case; instead, each component will be responsible for handling its **own** unsaved changes, and the Dashboard will simply listen for updates. As an intermediate step, this PR separates out the control group logic from the Dashboard plugin so that, when it comes time, the transition to the new embeddable framework will be much smoother. This PR also unblocks #170396 since, now that the control group is responsible for handling its own unsaved changes, I should be able to determine when to enable/disable the "reset changes" button. ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [x] This was checked for [cross-browser compatibility](https://www.elastic.co/support/matrix#matrix_browsers) ### For maintainers - [ ] This was checked for breaking API changes and was [labeled appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
1 parent
38f0a7a
commit 4bb5fcc
Showing
28 changed files
with
418 additions
and
222 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
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 |
---|---|---|
|
@@ -7,3 +7,5 @@ | |
*/ | ||
|
||
export const MIN_POPOVER_WIDTH = 300; | ||
|
||
export const CHANGE_CHECK_DEBOUNCE = 100; |
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
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
68 changes: 68 additions & 0 deletions
68
src/plugins/controls/public/control_group/state/control_group_diffing_integration.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,68 @@ | ||
/* | ||
* 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 { AnyAction, Middleware } from 'redux'; | ||
import { debounceTime, Observable, startWith, Subject, switchMap } from 'rxjs'; | ||
|
||
import { ControlGroupContainer } from '..'; | ||
import { persistableControlGroupInputIsEqual } from '../../../common'; | ||
import { CHANGE_CHECK_DEBOUNCE } from '../../constants'; | ||
import { controlGroupReducers } from './control_group_reducers'; | ||
|
||
/** | ||
* An array of reducers which cannot cause unsaved changes. Unsaved changes only compares the explicit input | ||
* and the last saved input, so we can safely ignore any output reducers, and most componentState reducers. | ||
* This is only for performance reasons, because the diffing function itself can be quite heavy. | ||
*/ | ||
export const reducersToIgnore: Array<keyof typeof controlGroupReducers> = [ | ||
'setDefaultControlWidth', | ||
'setDefaultControlGrow', | ||
]; | ||
|
||
/** | ||
* Does an initial diff between @param initialInput and @param initialLastSavedInput, and created a middleware | ||
* which listens to the redux store and checks for & publishes the unsaved changes on dispatches. | ||
*/ | ||
export function startDiffingControlGroupState(this: ControlGroupContainer) { | ||
const checkForUnsavedChangesSubject$ = new Subject<null>(); | ||
this.diffingSubscription.add( | ||
checkForUnsavedChangesSubject$ | ||
.pipe( | ||
startWith(null), | ||
debounceTime(CHANGE_CHECK_DEBOUNCE), | ||
switchMap(() => { | ||
return new Observable((observer) => { | ||
if (observer.closed) return; | ||
|
||
const { | ||
explicitInput: currentInput, | ||
componentState: { lastSavedInput }, | ||
} = this.getState(); | ||
const hasUnsavedChanges = !persistableControlGroupInputIsEqual( | ||
currentInput, | ||
lastSavedInput | ||
); | ||
this.unsavedChanges.next(hasUnsavedChanges ? this.getPersistableInput() : undefined); | ||
}); | ||
}) | ||
) | ||
.subscribe() | ||
); | ||
const diffingMiddleware: Middleware<AnyAction> = (store) => (next) => (action) => { | ||
const dispatchedActionName = action.type.split('/')?.[1]; | ||
if ( | ||
dispatchedActionName && | ||
dispatchedActionName !== 'updateEmbeddableReduxOutput' && // ignore any generic output updates. | ||
!reducersToIgnore.includes(dispatchedActionName) | ||
) { | ||
checkForUnsavedChangesSubject$.next(null); | ||
} | ||
next(action); | ||
}; | ||
return diffingMiddleware; | ||
} |
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.