Small refactoring, set variable node

This commit is contained in:
barsdeveloper
2022-11-19 18:40:20 +01:00
parent 0981c15372
commit b55779312b
34 changed files with 524 additions and 253 deletions

View File

@@ -3,6 +3,7 @@ import Observable from "../Observable"
import SerializerFactory from "../serialization/SerializerFactory"
import TypeInitialization from "./TypeInitialization"
import Utility from "../Utility"
import UnionType from "./UnionType"
/**
* @template {IEntity} T
@@ -81,7 +82,6 @@ export default class IEntity extends Observable {
if (defaultValue.serialized) {
defaultValue = ""
} else {
// @ts-expect-error
defaultType = defaultValue.type
defaultValue = defaultValue.value
if (defaultValue instanceof Function) {
@@ -89,6 +89,10 @@ export default class IEntity extends Observable {
}
}
}
if (defaultValue instanceof UnionType) {
defaultType = defaultValue.getFirstType()
defaultValue = TypeInitialization.sanitize(null, defaultType)
}
if (defaultValue instanceof Array) {
defaultValue = []
}

View File

@@ -33,7 +33,7 @@ export default class ObjectEntity extends IEntity {
CustomProperties: [PinEntity],
}
static nameRegex = /(\w+)(?:_(\d+))?/
static nameRegex = /^(\w+?)(?:_(\d+))?$/
constructor(options = {}) {
super(options)
@@ -107,6 +107,10 @@ export default class ObjectEntity extends IEntity {
return `For Each ${this.Enum.getName()}`
case Configuration.nodeType.forEachLoopWithBreak:
return "For Each Loop with Break"
case Configuration.nodeType.variableGet:
return ""
case Configuration.nodeType.variableSet:
return "SET"
default:
if (this.getClass() === Configuration.nodeType.macro) {
return Utility.formatStringName(this.MacroGraphReference.getMacroName())

View File

@@ -10,6 +10,7 @@ import RotatorEntity from "./RotatorEntity"
import SimpleSerializationRotatorEntity from "./SimpleSerializationRotatorEntity"
import SimpleSerializationVectorEntity from "./SimpleSerializationVectorEntity"
import TypeInitialization from "./TypeInitialization"
import UnionType from "./UnionType"
import VectorEntity from "./VectorEntity"
/** @typedef {import("./TypeInitialization").AnyValue} AnyValue */
@@ -36,7 +37,7 @@ export default class PinEntity extends IEntity {
static attributes = {
PinId: GuidEntity,
PinName: "",
PinFriendlyName: new TypeInitialization(LocalizedTextEntity, false, null),
PinFriendlyName: new TypeInitialization(new UnionType(LocalizedTextEntity, String), false, null),
PinToolTip: new TypeInitialization(String, false, ""),
Direction: new TypeInitialization(String, false, ""),
PinType: {
@@ -85,7 +86,7 @@ export default class PinEntity extends IEntity {
super(options)
/** @type {GuidEntity} */ this.PinId
/** @type {String} */ this.PinName
/** @type {LocalizedTextEntity} */ this.PinFriendlyName
/** @type {LocalizedTextEntity | String} */ this.PinFriendlyName
/** @type {String} */ this.PinToolTip
/** @type {String} */ this.Direction
/**
@@ -142,6 +143,10 @@ export default class PinEntity extends IEntity {
return this.DefaultValue
}
isExecution() {
return this.PinType.PinCategory === "exec"
}
isHidden() {
return this.bHidden
}

View File

@@ -1,7 +1,8 @@
import UnionType from "./UnionType"
/**
* @typedef {import("./IEntity").default} IEntity
* @typedef {IEntity | String | Number | Boolean | Array} AnyValue
* @typedef {import("./IEntity").default} IEntity
*/
/**
* @template {AnyValue} T
@@ -9,7 +10,7 @@
*/
/**
* @template {AnyValue} T
* @typedef {IEntityConstructor<T> | StringConstructor | NumberConstructor | BooleanConstructor | ArrayConstructor} AnyValueConstructor
* @typedef {IEntityConstructor<T> | StringConstructor | NumberConstructor | BooleanConstructor | ArrayConstructor | UnionType} AnyValueConstructor
*/
/** @template {AnyValue} T */
@@ -58,15 +59,22 @@ export default class TypeInitialization {
this.#ignored = v
}
static isValueOfType(value, type) {
return value != null && (value instanceof type || value.constructor === type)
}
static sanitize(value, targetType) {
if (targetType === undefined) {
targetType = value?.constructor
}
if (
targetType
// value is not of type targetType
&& !(value?.constructor === targetType || value instanceof targetType)
) {
if (targetType instanceof Array) {
let type = targetType.find(t => TypeInitialization.isValueOfType(value, t))
if (!type) {
type = targetType[0]
}
targetType = type
}
if (targetType && !TypeInitialization.isValueOfType(value, targetType)) {
value = new targetType(value)
}
if (value instanceof Boolean || value instanceof Number || value instanceof String) {

21
js/entity/UnionType.js Normal file
View File

@@ -0,0 +1,21 @@
/**
* @template T
* @typedef {import("./TypeInitialization").AnyValueConstructor<T>} AnyValueConstructor
*/
export default class UnionType {
#types
get types() {
return this.#types
}
/** @param {...AnyValueConstructor<any>} types */
constructor(...types) {
this.#types = types
}
getFirstType() {
return this.#types[0]
}
}