Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Comment thread
nickofthyme marked this conversation as resolved.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@markov00 The Visualize Embeddable does not use initializeUnsavedChanges in 9.0, 8.18 nor 8.17. Not sure if you wanted to check those version for the issue and fix them or just backport to working branches.

Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import {
} from '@kbn/presentation-publishing';
import { MaybePromise } from '@kbn/utility-types';
import { Observable, combineLatestWith, debounceTime, map, of } from 'rxjs';
import { isEqual, sortBy } from 'lodash';
import { apiHasLastSavedChildState } from '../last_saved_child_state';
import { PresentationContainer } from '../presentation_container';

const UNSAVED_CHANGES_DEBOUNCE = 100;

export const initializeUnsavedChanges = <StateType extends object = object>({
Expand All @@ -29,6 +29,7 @@ export const initializeUnsavedChanges = <StateType extends object = object>({
defaultState,
serializeState,
anyStateChange$,
checkRefEquality,
}: {
uuid: string;
parentApi: unknown;
Expand All @@ -37,6 +38,7 @@ export const initializeUnsavedChanges = <StateType extends object = object>({
getComparators: () => StateComparators<StateType>;
defaultState?: Partial<StateType>;
onReset: (lastSavedPanelState?: SerializedPanelState<StateType>) => MaybePromise<void>;
checkRefEquality?: boolean;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason checking ref equality on links causes an erroneous diff where the lastSavedState.references is initially empty.

Not sure why this is but also to err on the side of caution, we made this ref equality check opt-in.

}): PublishesUnsavedChanges => {
if (!apiHasLastSavedChildState<StateType>(parentApi)) {
return {
Expand All @@ -46,16 +48,25 @@ export const initializeUnsavedChanges = <StateType extends object = object>({
}

const hasUnsavedChanges$ = anyStateChange$.pipe(
combineLatestWith(
parentApi.lastSavedStateForChild$(uuid).pipe(map((panelState) => panelState?.rawState))
),
combineLatestWith(parentApi.lastSavedStateForChild$(uuid)),
debounceTime(UNSAVED_CHANGES_DEBOUNCE),
map(([, lastSavedState]) => {
const currentState = serializeState().rawState;
const currentState = serializeState();

// check ref equality
if (checkRefEquality) {
const lastSavedRefs = sortBy(lastSavedState?.references ?? [], 'id');
const currentRefs = sortBy(currentState?.references ?? [], 'id');
const equalRefs = isEqual(lastSavedRefs, currentRefs);

if (!equalRefs) return true;
}

// check state equality
return !areComparatorsEqual(
getComparators(),
lastSavedState,
currentState,
lastSavedState?.rawState,
currentState.rawState,
defaultState,
(key: string) => {
const childApi = (parentApi as Partial<PresentationContainer>).children$?.getValue()[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export function getDashboardApi({
}

if (saveResult) {
references$.next(saveResult.references);
unsavedChangesManager.internalApi.onSave(saveResult.savedState);
const settings = settingsManager.api.getSettings();
settingsManager.api.setSettings({
Expand All @@ -185,8 +186,6 @@ export function getDashboardApi({
title: saveResult.savedState.title,
});
savedObjectId$.next(saveResult.id);

references$.next(saveResult.references);
}

return saveResult;
Expand All @@ -203,8 +202,8 @@ export function getDashboardApi({
});

if (saveResult?.error) return;
unsavedChangesManager.internalApi.onSave(dashboardState);
references$.next(saveResult.references);
unsavedChangesManager.internalApi.onSave(dashboardState);

return;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export const getVisualizeEmbeddableFactory: (deps: {
serializeState: () => {
return serializeVisualizeEmbeddable(savedObjectId$.getValue(), linkedToLibrary);
},
checkRefEquality: true,
anyStateChange$: merge(
...(dynamicActionsManager ? [dynamicActionsManager.anyStateChange$] : []),
savedObjectId$,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3198,6 +3198,11 @@
"type": "visualization",
"id": "50643b60-3dd3-11e8-b2b9-5d5dc1715159"
},
{
"name":"ffc13252-56b4-4e3f-847e-61373fa0be86:kibanaSavedObjectMeta.searchSourceJSON.index",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixture update LGTM.

"type":"index-pattern",
"id":"a0f483a0-3dc9-11e8-8660-4d65aa086b3c"
},
{
"name": "controlGroup_41827e70-5285-4d44-8375-4c498449b9a7:optionsListControlDataView",
"type": "index-pattern",
Expand Down Expand Up @@ -3317,4 +3322,4 @@
"managed": false,
"coreMigrationVersion": "8.8.0",
"typeMigrationVersion": "10.2.0"
}
}