Files
DiceCloud/app/imports/api/engine/action/functions/userInput/saveInputChoices.ts
2024-03-30 21:12:35 +02:00

29 lines
941 B
TypeScript

import { EngineAction } from '/imports/api/engine/action/EngineActions';
import InputProvider from '/imports/api/engine/action/functions/userInput/InputProvider';
/**
* Create a new version of the user input function, that saves the user's choices to an array
* before returning them
*/
export default function saveInputChoices(action: EngineAction, userInput: InputProvider): InputProvider {
const newInputProvider: Partial<InputProvider> = {};
if (!action._choices) {
action._choices = [];
}
// For every function in the given input provider
for (const key in userInput) {
const oldFn = userInput[key];
// Make a new function that does the same thing, but saves the result to action._choices
const newFn = (...args) => {
const result = oldFn(...args);
action._choices.push(result);
return result;
}
newInputProvider[key] = newFn;
}
return newInputProvider as InputProvider;
}