Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only reset unsaved changes when value has changed #190103

Closed
wants to merge 3 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
Subscription,
} from 'rxjs';
import {
getComparatorFunction,
getInitialValuesFromComparators,
PublishesUnsavedChanges,
PublishingSubject,
Expand Down Expand Up @@ -95,9 +96,13 @@ export const initializeUnsavedChanges = <RuntimeState extends {} = {}>(
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@
*/

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';
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ import { StateComparators } from './types';

const defaultComparator = <T>(a: T, b: T) => a === b;

export function getComparatorFunction<StateType extends object = object>(
comparators: StateComparators<StateType>,
key: keyof StateType
) {
const customComparator = comparators[key]?.[2]; // 2nd element of the tuple is the custom comparator
return customComparator ?? defaultComparator;
}

export const getInitialValuesFromComparators = <StateType extends object = object>(
comparators: StateComparators<StateType>,
comparatorKeys: Array<keyof StateType>
Expand All @@ -35,8 +43,7 @@ export const runComparators = <StateType extends object = object>(
}
const latestChanges: Partial<StateType> = {};
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];
}
Expand Down
1 change: 1 addition & 0 deletions packages/presentation/presentation_publishing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface EmbeddableApiContext {
}

export {
getComparatorFunction,
getInitialValuesFromComparators,
getUnchangingComparator,
runComparators,
Expand Down