Skip to content

Commit ca75cb3

Browse files
committed
Simplify to only trigger cache updates on transform type changes
Signed-off-by: Tyler Ohlsen <ohltyler@amazon.com>
1 parent 2e69e05 commit ca75cb3

File tree

1 file changed

+45
-62
lines changed
  • public/pages/workflow_detail/workflow_inputs/processor_inputs/ml_processor_inputs

1 file changed

+45
-62
lines changed

public/pages/workflow_detail/workflow_inputs/processor_inputs/ml_processor_inputs/model_inputs.tsx

+45-62
Original file line numberDiff line numberDiff line change
@@ -117,70 +117,12 @@ export function ModelInputs(props: ModelInputsProps) {
117117
number | undefined
118118
>(undefined);
119119

120-
const [debouncedInputMap, setDebouncedInputMap] = useState<any>();
121-
useEffect(() => {
122-
// Set a timeout to update debounced value after 500ms
123-
const handler = setTimeout(() => {
124-
setDebouncedInputMap(getIn(values, inputMapFieldPath));
125-
}, 500);
126-
127-
// Cleanup the timeout if `query` changes before 500ms
128-
return () => {
129-
clearTimeout(handler);
130-
};
131-
}, [getIn(values, inputMapFieldPath)]);
132-
133-
// TODO: this works ok, but does not allow clearing out any form value, or it resets to the last-known/cached value.
134-
// need to filter out on this use case.
135-
136120
// Temporarily cache any configured transformations for different transform types.
137121
// For example, if a user configures a prompt, swaps the transform
138122
// type to "Data field", and swaps back to "Prompt", the prompt will be persisted.
139123
const [inputMapCache, _] = useState<{
140124
[idx: number]: Transform[];
141125
}>({});
142-
useEffect(() => {
143-
const curFormValues = debouncedInputMap as InputMapFormValue | undefined;
144-
if (curFormValues !== undefined && !isEmpty(curFormValues)) {
145-
// for each form value: populate the cache with a non-empty value and/or populate the
146-
// form value with its cached value, if found.
147-
curFormValues.forEach((mapEntry, idx) => {
148-
const curCacheForIdx = inputMapCache[idx];
149-
if (curCacheForIdx === undefined || isEmpty(curCacheForIdx)) {
150-
// case 1: there is no persisted state for this entry index. create a fresh arr
151-
inputMapCache[idx] = [mapEntry.value];
152-
} else if (
153-
!curCacheForIdx.some(
154-
(transform: Transform) =>
155-
transform.transformType === mapEntry.value.transformType
156-
)
157-
) {
158-
// case 2: there is persisted state for this entry index, but not for the particular
159-
// transform type. append to the arr
160-
inputMapCache[idx] = [...inputMapCache[idx], mapEntry.value];
161-
} else {
162-
// case 3: there is persisted state for this entry index, and for the particular transform type.
163-
// Either update the cache with the current form value(s) (if non-empty), or update the form
164-
// with any value found in the cache
165-
inputMapCache[idx] = inputMapCache[idx].map((cachedEntry) => {
166-
if (cachedEntry.transformType === mapEntry.value.transformType) {
167-
const formValue = mapEntry.value.value;
168-
// form is non-empty. update the cache
169-
if (formValue !== undefined && !isEmpty(formValue)) {
170-
return mapEntry.value;
171-
// form is empty. update the form with cached value(s)
172-
} else {
173-
setFieldValue(`${inputMapFieldPath}.${idx}.value`, cachedEntry);
174-
return cachedEntry;
175-
}
176-
} else {
177-
return cachedEntry;
178-
}
179-
});
180-
}
181-
});
182-
}
183-
}, [debouncedInputMap]);
184126

185127
// persisting doc/query/index mapping fields to collect a list
186128
// of options to display in the dropdowns when configuring input / output maps
@@ -448,19 +390,60 @@ export function ModelInputs(props: ModelInputsProps) {
448390
) || ''
449391
}
450392
onChange={(option) => {
393+
// before updating, cache any form values
394+
const curCache = inputMapCache[idx];
395+
if (
396+
curCache === undefined ||
397+
isEmpty(curCache)
398+
) {
399+
// case 1: there is no persisted state for this entry index. create a fresh arr
400+
inputMapCache[idx] = [mapEntry.value];
401+
} else if (
402+
!curCache.some(
403+
(transform: Transform) =>
404+
transform.transformType ===
405+
mapEntry.value.transformType
406+
)
407+
) {
408+
// case 2: there is persisted state for this entry index, but not for the particular
409+
// transform type. append to the arr
410+
inputMapCache[idx] = [
411+
...inputMapCache[idx],
412+
mapEntry.value,
413+
];
414+
} else {
415+
// case 3: there is persisted state for this entry index, and for the particular transform type.
416+
// Update the cache with the current form value(s)
417+
inputMapCache[idx] = inputMapCache[
418+
idx
419+
].map((cachedEntry) => {
420+
if (
421+
cachedEntry.transformType ===
422+
mapEntry.value.transformType
423+
) {
424+
return mapEntry.value;
425+
} else {
426+
return cachedEntry;
427+
}
428+
});
429+
}
430+
451431
setFieldValue(
452432
`${inputMapFieldPath}.${idx}.value.transformType`,
453433
option
454434
);
455-
// If the transform type changes, clear any set value and/or nested vars,
456-
// as it will likely not make sense under other types/contexts.
435+
// Pre-populate with any cached values, if found
436+
const curCacheForOption = curCache?.find(
437+
(transform: Transform) =>
438+
transform.transformType === option
439+
);
457440
setFieldValue(
458441
`${inputMapFieldPath}.${idx}.value.value`,
459-
''
442+
curCacheForOption?.value || ''
460443
);
461444
setFieldValue(
462445
`${inputMapFieldPath}.${idx}.value.nestedVars`,
463-
[]
446+
curCacheForOption?.nestedVars || []
464447
);
465448
}}
466449
/>

0 commit comments

Comments
 (0)