Skip to content

Commit

Permalink
Add a string comparison on the output of the derived store (#1186)
Browse files Browse the repository at this point in the history
  • Loading branch information
duranb authored Mar 28, 2024
1 parent 3f2b12f commit fee37ee
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 20 deletions.
13 changes: 2 additions & 11 deletions src/components/ui/Association/AssociationForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down
38 changes: 38 additions & 0 deletions src/stores/derivedDeeply.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { derived, type Readable, type Stores, type StoresValues } from 'svelte/store';

export function derivedDeeply<S extends Stores, T>(
stores: S,
fn: (values: StoresValues<S>) => T,
initialValue?: T,
): Readable<T> {
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,
);
}
25 changes: 16 additions & 9 deletions src/stores/scheduling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -77,7 +78,7 @@ export const schedulingPlanSpecification = gqlSubscribable<SchedulingPlanSpecifi
);

/* Derived. */
export const schedulingConditions = derived(
export const schedulingConditions = derivedDeeply(
[schedulingConditionResponses, tags],
([$schedulingConditionResponses, $tags]) => {
return $schedulingConditionResponses.map(schedulingConditionResponse =>
Expand All @@ -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<SchedulingGoalMetadata, SchedulingGoalDefinition>(schedulingGoalResponse, $tags),
);
});

export const schedulingConditionMetadata = derived(
export const schedulingConditionMetadata = derivedDeeply(
[schedulingConditionResponse, tags],
([$schedulingConditionResponse, $tags]) => {
if ($schedulingConditionResponse) {
Expand All @@ -108,12 +109,18 @@ export const schedulingConditionMetadata = derived(
},
);

export const schedulingGoalMetadata = derived([schedulingGoalResponse, tags], ([$schedulingGoalResponse, $tags]) => {
if ($schedulingGoalResponse) {
return convertResponseToMetadata<SchedulingGoalMetadata, SchedulingGoalDefinition>($schedulingGoalResponse, $tags);
}
return null;
});
export const schedulingGoalMetadata = derivedDeeply(
[schedulingGoalResponse, tags],
([$schedulingGoalResponse, $tags]) => {
if ($schedulingGoalResponse) {
return convertResponseToMetadata<SchedulingGoalMetadata, SchedulingGoalDefinition>(
$schedulingGoalResponse,
$tags,
);
}
return null;
},
);

export const schedulingConditionsMap: Readable<Record<string, SchedulingConditionMetadata>> = derived(
[schedulingConditions],
Expand Down

0 comments on commit fee37ee

Please sign in to comment.