@tokens-studio/graph-engine
Version:
An execution engine to handle Token Studios generators and resolvers
51 lines • 1.7 kB
JavaScript
import { AnySchema, StringSchema } from '../../schemas/index.js';
import { Node } from '../../programmatic/node.js';
import { annotatedDynamicInputs } from '../../annotations/index.js';
/**
* @example
* The expected way to use the switch since it relies on dynamic inputs, is to add new Inputs for the switch node
* ```ts
* const switchNode = new SwitchNode();
* switchNode.addInput("foo", {
* type: AnySchema,
* });
*
* switchNode.addInput("bar", {
* type: AnySchema,
* });
*
* //Now if the condition matches the name 'foo' it will output the value of the foo input
* // If no condition matches, it will output the value of the `default` input
*
* ```
*/
export default class NodeDefinition extends Node {
static title = 'Switch';
static type = 'studio.tokens.logic.switch';
static description = 'Switch node allows you to conditionally choose a value based on a condition.';
constructor(props) {
super(props);
this.annotations[annotatedDynamicInputs] = true;
this.addInput('default', {
type: AnySchema
});
this.addInput('condition', {
type: StringSchema
});
this.addOutput('value', {
type: AnySchema
});
}
execute() {
const { condition } = this.getAllInputs();
const defaultVal = this.inputs.default;
//Check if an input matches the condition
if (this.inputs[condition]) {
const input = this.inputs[condition];
this.outputs.value.set(input.value, input.type);
return;
}
this.outputs.value.set(defaultVal.value, defaultVal.type);
}
}
//# sourceMappingURL=switch.js.map