From fee37ee1ce7823d3cc95234f5af724c2a57d09fb Mon Sep 17 00:00:00 2001 From: Bryan Date: Thu, 28 Mar 2024 11:56:14 -0600 Subject: [PATCH] Add a string comparison on the output of the `derived` store (#1186) --- .../ui/Association/AssociationForm.svelte | 13 +------ src/stores/derivedDeeply.ts | 38 +++++++++++++++++++ src/stores/scheduling.ts | 25 +++++++----- 3 files changed, 56 insertions(+), 20 deletions(-) create mode 100644 src/stores/derivedDeeply.ts diff --git a/src/components/ui/Association/AssociationForm.svelte b/src/components/ui/Association/AssociationForm.svelte index 1fed4c87cb..432f4ec9ee 100644 --- a/src/components/ui/Association/AssociationForm.svelte +++ b/src/components/ui/Association/AssociationForm.svelte @@ -110,19 +110,10 @@ let saveButtonEnabled: boolean = false; let saveButtonText: string = 'Save'; - let prevMetadataTags: Tag[] = initialMetadataTags; - let prevDefinitionTags: Tag[] = initialDefinitionTags; - - $: if (diffTags(prevMetadataTags, initialMetadataTags)) { - metadataTags = initialMetadataTags; - prevMetadataTags = initialMetadataTags; - } + $: metadataTags = initialMetadataTags; + $: definitionTags = initialDefinitionTags; $: defintionAuthor = initialDefinitionAuthor ?? user?.id ?? null; $: definitionCode = initialDefinitionCode; - $: if (diffTags(prevDefinitionTags, initialDefinitionTags)) { - definitionTags = initialDefinitionTags; - prevDefinitionTags = initialDefinitionTags; - } $: isMetadataModified = diffMetadata( { diff --git a/src/stores/derivedDeeply.ts b/src/stores/derivedDeeply.ts new file mode 100644 index 0000000000..d66cefcf9a --- /dev/null +++ b/src/stores/derivedDeeply.ts @@ -0,0 +1,38 @@ +import { derived, type Readable, type Stores, type StoresValues } from 'svelte/store'; + +export function derivedDeeply( + stores: S, + fn: (values: StoresValues) => T, + initialValue?: T, +): Readable { + return derived( + stores, + ($stores, _set, update) => { + const nextValue = fn($stores); + + update(prevValue => { + let prevValueString: string; + let nextValueString: string; + + if (typeof prevValue === 'object') { + prevValueString = JSON.stringify(prevValue); + } else { + prevValueString = `${prevValue}`; + } + + if (typeof nextValue === 'object') { + nextValueString = JSON.stringify(nextValue); + } else { + nextValueString = `${nextValue}`; + } + + if (prevValueString === nextValueString) { + return prevValue; + } + + return nextValue; + }); + }, + initialValue, + ); +} diff --git a/src/stores/scheduling.ts b/src/stores/scheduling.ts index 6c59f995e3..4013af45b0 100644 --- a/src/stores/scheduling.ts +++ b/src/stores/scheduling.ts @@ -17,6 +17,7 @@ import type { } from '../types/scheduling'; import gql from '../utilities/gql'; import { convertResponseToMetadata } from '../utilities/scheduling'; +import { derivedDeeply } from './derivedDeeply'; import { simulationDatasetsPlan } from './simulation'; import { gqlSubscribable } from './subscribable'; import { tags } from './tags'; @@ -77,7 +78,7 @@ export const schedulingPlanSpecification = gqlSubscribable { return $schedulingConditionResponses.map(schedulingConditionResponse => @@ -89,13 +90,13 @@ export const schedulingConditions = derived( }, ); -export const schedulingGoals = derived([schedulingGoalResponses, tags], ([$schedulingGoalResponses, $tags]) => { +export const schedulingGoals = derivedDeeply([schedulingGoalResponses, tags], ([$schedulingGoalResponses, $tags]) => { return $schedulingGoalResponses.map(schedulingGoalResponse => convertResponseToMetadata(schedulingGoalResponse, $tags), ); }); -export const schedulingConditionMetadata = derived( +export const schedulingConditionMetadata = derivedDeeply( [schedulingConditionResponse, tags], ([$schedulingConditionResponse, $tags]) => { if ($schedulingConditionResponse) { @@ -108,12 +109,18 @@ export const schedulingConditionMetadata = derived( }, ); -export const schedulingGoalMetadata = derived([schedulingGoalResponse, tags], ([$schedulingGoalResponse, $tags]) => { - if ($schedulingGoalResponse) { - return convertResponseToMetadata($schedulingGoalResponse, $tags); - } - return null; -}); +export const schedulingGoalMetadata = derivedDeeply( + [schedulingGoalResponse, tags], + ([$schedulingGoalResponse, $tags]) => { + if ($schedulingGoalResponse) { + return convertResponseToMetadata( + $schedulingGoalResponse, + $tags, + ); + } + return null; + }, +); export const schedulingConditionsMap: Readable> = derived( [schedulingConditions],