From 9ab6d90c655115711229372a48e73f4baf9be893 Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Wed, 7 Aug 2024 16:42:29 -0600 Subject: [PATCH 1/2] Only reset unsaved changes when value has changed --- .../unsaved_changes/initialize_unsaved_changes.ts | 9 +++++++-- .../presentation_publishing/comparators/index.ts | 2 +- .../comparators/state_comparators.ts | 8 ++++++-- packages/presentation/presentation_publishing/index.ts | 1 + 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/presentation/presentation_containers/interfaces/unsaved_changes/initialize_unsaved_changes.ts b/packages/presentation/presentation_containers/interfaces/unsaved_changes/initialize_unsaved_changes.ts index 7f4770d39cd2d..290203e6ef042 100644 --- a/packages/presentation/presentation_containers/interfaces/unsaved_changes/initialize_unsaved_changes.ts +++ b/packages/presentation/presentation_containers/interfaces/unsaved_changes/initialize_unsaved_changes.ts @@ -16,6 +16,7 @@ import { Subscription, } from 'rxjs'; import { + getComparatorFunction, getInitialValuesFromComparators, PublishesUnsavedChanges, PublishingSubject, @@ -96,9 +97,13 @@ export const initializeUnsavedChanges = ( unsavedChanges, resetUnsavedChanges: () => { const lastSaved = lastSavedState$.getValue(); + const currentState = snapshotRuntimeState(); for (const key of comparatorKeys) { - const setter = comparators[key][1]; // setter function is the 1st element of the tuple - setter(lastSaved?.[key] as RuntimeState[typeof key]); + const comparator = getComparatorFunction(comparators, key); + if (!comparator(lastSaved?.[key], currentState[key], lastSaved, currentState)) { + const setter = comparators[key][1]; // setter function is the 1st element of the tuple + setter(lastSaved?.[key] as RuntimeState[typeof key]); + } } }, snapshotRuntimeState, diff --git a/packages/presentation/presentation_publishing/comparators/index.ts b/packages/presentation/presentation_publishing/comparators/index.ts index c870fbca9daf8..88197aa3d4cb3 100644 --- a/packages/presentation/presentation_publishing/comparators/index.ts +++ b/packages/presentation/presentation_publishing/comparators/index.ts @@ -7,5 +7,5 @@ */ export type { ComparatorFunction, ComparatorDefinition, StateComparators } from './types'; -export { getInitialValuesFromComparators, runComparators } from './state_comparators'; +export { getComparatorFunction, getInitialValuesFromComparators, runComparators } from './state_comparators'; export { getUnchangingComparator } from './fallback_comparator'; diff --git a/packages/presentation/presentation_publishing/comparators/state_comparators.ts b/packages/presentation/presentation_publishing/comparators/state_comparators.ts index 9506984464718..397dfebd20238 100644 --- a/packages/presentation/presentation_publishing/comparators/state_comparators.ts +++ b/packages/presentation/presentation_publishing/comparators/state_comparators.ts @@ -10,6 +10,11 @@ import { StateComparators } from './types'; const defaultComparator = (a: T, b: T) => a === b; +export function getComparatorFunction(comparators: StateComparators, key: keyof StateType) { + const customComparator = comparators[key]?.[2]; // 2nd element of the tuple is the custom comparator + return customComparator ?? defaultComparator; +} + export const getInitialValuesFromComparators = ( comparators: StateComparators, comparatorKeys: Array @@ -34,8 +39,7 @@ export const runComparators = ( } const latestChanges: Partial = {}; for (const key of comparatorKeys) { - const customComparator = comparators[key]?.[2]; // 2nd element of the tuple is the custom comparator - const comparator = customComparator ?? defaultComparator; + const comparator = getComparatorFunction(comparators, key); if (!comparator(lastSavedState?.[key], latestState[key], lastSavedState, latestState)) { latestChanges[key] = latestState[key]; } diff --git a/packages/presentation/presentation_publishing/index.ts b/packages/presentation/presentation_publishing/index.ts index 8d90462c8a1bd..30ef3163dc869 100644 --- a/packages/presentation/presentation_publishing/index.ts +++ b/packages/presentation/presentation_publishing/index.ts @@ -15,6 +15,7 @@ export interface EmbeddableApiContext { } export { + getComparatorFunction, getInitialValuesFromComparators, getUnchangingComparator, runComparators, From 4792179a575bbed77c004397e909dfc20c075320 Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Wed, 7 Aug 2024 16:43:40 -0600 Subject: [PATCH 2/2] eslint --- .../presentation_publishing/comparators/index.ts | 6 +++++- .../comparators/state_comparators.ts | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/presentation/presentation_publishing/comparators/index.ts b/packages/presentation/presentation_publishing/comparators/index.ts index 88197aa3d4cb3..a313ab259a3a7 100644 --- a/packages/presentation/presentation_publishing/comparators/index.ts +++ b/packages/presentation/presentation_publishing/comparators/index.ts @@ -7,5 +7,9 @@ */ export type { ComparatorFunction, ComparatorDefinition, StateComparators } from './types'; -export { getComparatorFunction, getInitialValuesFromComparators, runComparators } from './state_comparators'; +export { + getComparatorFunction, + getInitialValuesFromComparators, + runComparators, +} from './state_comparators'; export { getUnchangingComparator } from './fallback_comparator'; diff --git a/packages/presentation/presentation_publishing/comparators/state_comparators.ts b/packages/presentation/presentation_publishing/comparators/state_comparators.ts index 397dfebd20238..d069fdcbaad86 100644 --- a/packages/presentation/presentation_publishing/comparators/state_comparators.ts +++ b/packages/presentation/presentation_publishing/comparators/state_comparators.ts @@ -10,7 +10,10 @@ import { StateComparators } from './types'; const defaultComparator = (a: T, b: T) => a === b; -export function getComparatorFunction(comparators: StateComparators, key: keyof StateType) { +export function getComparatorFunction( + comparators: StateComparators, + key: keyof StateType +) { const customComparator = comparators[key]?.[2]; // 2nd element of the tuple is the custom comparator return customComparator ?? defaultComparator; }