flowGraphExecutionBlock.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { FlowGraphBlock } from "./flowGraphBlock.js";
  2. import { FlowGraphConnectionType } from "./flowGraphConnection.js";
  3. import { FlowGraphSignalConnection } from "./flowGraphSignalConnection.js";
  4. /**
  5. * @experimental
  6. * A block that executes some action. Always has an input signal (which is not used by event blocks).
  7. * Can have one or more output signals.
  8. */
  9. export class FlowGraphExecutionBlock extends FlowGraphBlock {
  10. constructor(config) {
  11. super(config);
  12. this.signalInputs = [];
  13. this.signalOutputs = [];
  14. this.in = this._registerSignalInput("in");
  15. }
  16. _registerSignalInput(name) {
  17. const input = new FlowGraphSignalConnection(name, FlowGraphConnectionType.Input, this);
  18. this.signalInputs.push(input);
  19. return input;
  20. }
  21. _registerSignalOutput(name) {
  22. const output = new FlowGraphSignalConnection(name, FlowGraphConnectionType.Output, this);
  23. this.signalOutputs.push(output);
  24. return output;
  25. }
  26. /**
  27. * Given a name of a signal input, return that input if it exists
  28. * @param name the name of the input
  29. * @returns if the input exists, the input. Otherwise, undefined.
  30. */
  31. getSignalInput(name) {
  32. return this.signalInputs.find((input) => input.name === name);
  33. }
  34. /**
  35. * Given a name of a signal output, return that input if it exists
  36. * @param name the name of the input
  37. * @returns if the input exists, the input. Otherwise, undefined.
  38. */
  39. getSignalOutput(name) {
  40. return this.signalOutputs.find((output) => output.name === name);
  41. }
  42. /**
  43. * Serializes this block
  44. * @param serializationObject the object to serialize in
  45. */
  46. serialize(serializationObject = {}) {
  47. super.serialize(serializationObject);
  48. serializationObject.signalInputs = [];
  49. serializationObject.signalOutputs = [];
  50. for (const input of this.signalInputs) {
  51. const serializedInput = {};
  52. input.serialize(serializedInput);
  53. serializationObject.signalInputs.push(serializedInput);
  54. }
  55. for (const output of this.signalOutputs) {
  56. const serializedOutput = {};
  57. output.serialize(serializedOutput);
  58. serializationObject.signalOutputs.push(serializedOutput);
  59. }
  60. }
  61. /**
  62. * Deserializes from an object
  63. * @param serializationObject the object to deserialize from
  64. */
  65. deserialize(serializationObject) {
  66. for (let i = 0; i < serializationObject.signalInputs.length; i++) {
  67. const signalInput = this.getSignalInput(serializationObject.signalInputs[i].name);
  68. if (signalInput) {
  69. signalInput.deserialize(serializationObject.signalInputs[i]);
  70. }
  71. else {
  72. throw new Error("Could not find signal input with name " + serializationObject.signalInputs[i].name + " in block " + serializationObject.className);
  73. }
  74. }
  75. for (let i = 0; i < serializationObject.signalOutputs.length; i++) {
  76. const signalOutput = this.getSignalOutput(serializationObject.signalOutputs[i].name);
  77. if (signalOutput) {
  78. signalOutput.deserialize(serializationObject.signalOutputs[i]);
  79. }
  80. else {
  81. throw new Error("Could not find signal output with name " + serializationObject.signalOutputs[i].name + " in block " + serializationObject.className);
  82. }
  83. }
  84. }
  85. /**
  86. * @returns the class name
  87. */
  88. getClassName() {
  89. return "FGExecutionBlock";
  90. }
  91. }
  92. //# sourceMappingURL=flowGraphExecutionBlock.js.map