flowGraphBlock.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { RandomGUID } from "../Misc/guid.js";
  2. import { FlowGraphConnectionType } from "./flowGraphConnection.js";
  3. import { FlowGraphDataConnection } from "./flowGraphDataConnection.js";
  4. import { Tools } from "../Misc/tools.js";
  5. import { defaultValueParseFunction, defaultValueSerializationFunction, needsPathConverter } from "./serialization.js";
  6. /**
  7. * @experimental
  8. * A block in a flow graph. The most basic form
  9. * of a block has inputs and outputs that contain
  10. * data.
  11. */
  12. export class FlowGraphBlock {
  13. /** Constructor is protected so only subclasses can be instantiated
  14. * @param config optional configuration for this block
  15. */
  16. constructor(
  17. /**
  18. * the configuration of the block
  19. */
  20. config) {
  21. this.config = config;
  22. /**
  23. * A randomly generated GUID for each block.
  24. */
  25. this.uniqueId = RandomGUID();
  26. this.name = this.config?.name ?? this.getClassName();
  27. this.dataInputs = [];
  28. this.dataOutputs = [];
  29. }
  30. /**
  31. * @internal
  32. */
  33. _updateOutputs(_context) {
  34. // empty by default, overriden in data blocks
  35. }
  36. /**
  37. * Registers a data input on the block.
  38. * @param name the name of the input
  39. * @param richType the type of the input
  40. * @returns the created connection
  41. */
  42. registerDataInput(name, richType) {
  43. const input = new FlowGraphDataConnection(name, FlowGraphConnectionType.Input, this, richType);
  44. this.dataInputs.push(input);
  45. return input;
  46. }
  47. /**
  48. * Registers a data output on the block.
  49. * @param name the name of the input
  50. * @param richType the type of the input
  51. * @returns the created connection
  52. */
  53. registerDataOutput(name, richType) {
  54. const output = new FlowGraphDataConnection(name, FlowGraphConnectionType.Output, this, richType);
  55. this.dataOutputs.push(output);
  56. return output;
  57. }
  58. /**
  59. * Given the name of a data input, returns the connection if it exists
  60. * @param name the name of the input
  61. * @returns the connection if it exists, undefined otherwise
  62. */
  63. getDataInput(name) {
  64. return this.dataInputs.find((i) => i.name === name);
  65. }
  66. /**
  67. * Given the name of a data output, returns the connection if it exists
  68. * @param name the name of the output
  69. * @returns the connection if it exists, undefined otherwise
  70. */
  71. getDataOutput(name) {
  72. return this.dataOutputs.find((i) => i.name === name);
  73. }
  74. /**
  75. * Serializes this block
  76. * @param serializationObject the object to serialize to
  77. * @param _valueSerializeFunction a function that serializes a specific value
  78. */
  79. serialize(serializationObject = {}, _valueSerializeFunction = defaultValueSerializationFunction) {
  80. serializationObject.uniqueId = this.uniqueId;
  81. serializationObject.config = {};
  82. if (this.config) {
  83. serializationObject.config["name"] = this.config.name;
  84. }
  85. serializationObject.dataInputs = [];
  86. serializationObject.dataOutputs = [];
  87. serializationObject.className = this.getClassName();
  88. for (const input of this.dataInputs) {
  89. const serializedInput = {};
  90. input.serialize(serializedInput);
  91. serializationObject.dataInputs.push(serializedInput);
  92. }
  93. for (const output of this.dataOutputs) {
  94. const serializedOutput = {};
  95. output.serialize(serializedOutput);
  96. serializationObject.dataOutputs.push(serializedOutput);
  97. }
  98. }
  99. /**
  100. * Gets the class name of this block
  101. * @returns the class name
  102. */
  103. getClassName() {
  104. return "FGBlock";
  105. }
  106. /**
  107. * Parses a block from a serialization object
  108. * @param serializationObject the object to parse from
  109. * @param parseOptions options for parsing the block
  110. * @returns the parsed block
  111. */
  112. static Parse(serializationObject, parseOptions) {
  113. const classType = Tools.Instantiate(serializationObject.className);
  114. const parsedConfig = {};
  115. const valueParseFunction = parseOptions.valueParseFunction ?? defaultValueParseFunction;
  116. if (serializationObject.config) {
  117. for (const key in serializationObject.config) {
  118. parsedConfig[key] = valueParseFunction(key, serializationObject.config, parseOptions.scene);
  119. }
  120. }
  121. if (needsPathConverter(serializationObject.className)) {
  122. parsedConfig.pathConverter = parseOptions.pathConverter;
  123. }
  124. const obj = new classType(parsedConfig);
  125. obj.uniqueId = serializationObject.uniqueId;
  126. for (let i = 0; i < serializationObject.dataInputs.length; i++) {
  127. const dataInput = obj.getDataInput(serializationObject.dataInputs[i].name);
  128. if (dataInput) {
  129. dataInput.deserialize(serializationObject.dataInputs[i]);
  130. }
  131. else {
  132. throw new Error("Could not find data input with name " + serializationObject.dataInputs[i].name + " in block " + serializationObject.className);
  133. }
  134. }
  135. for (let i = 0; i < serializationObject.dataOutputs.length; i++) {
  136. const dataOutput = obj.getDataOutput(serializationObject.dataOutputs[i].name);
  137. if (dataOutput) {
  138. dataOutput.deserialize(serializationObject.dataOutputs[i]);
  139. }
  140. else {
  141. throw new Error("Could not find data output with name " + serializationObject.dataOutputs[i].name + " in block " + serializationObject.className);
  142. }
  143. }
  144. obj.metadata = serializationObject.metadata;
  145. obj.deserialize && obj.deserialize(serializationObject);
  146. return obj;
  147. }
  148. }
  149. //# sourceMappingURL=flowGraphBlock.js.map