mirror of
https://github.com/barsdeveloper/ueblueprint.git
synced 2026-02-04 08:50:33 +08:00
Get rid of the Observable class
This commit is contained in:
105
js/Observable.js
105
js/Observable.js
@@ -1,105 +0,0 @@
|
||||
export default class Observable {
|
||||
|
||||
/** @type {Map<String, Object[]>} */
|
||||
#observers = new Map()
|
||||
|
||||
/**
|
||||
* @param {String} property
|
||||
* @param {(value: any) => {}} observer
|
||||
*/
|
||||
subscribe(property, observer) {
|
||||
let observers = this.#observers
|
||||
if (observers.has(property)) {
|
||||
let propertyObservers = observers.get(property)
|
||||
if (propertyObservers.includes(observer)) {
|
||||
return false
|
||||
} else {
|
||||
propertyObservers.push(observer)
|
||||
}
|
||||
} else {
|
||||
let fromPrototype = false
|
||||
let propertyDescriptor = Object.getOwnPropertyDescriptor(this, property)
|
||||
if (!propertyDescriptor) {
|
||||
fromPrototype = true
|
||||
propertyDescriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(this), property) ?? {}
|
||||
if (!propertyDescriptor) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
observers.set(property, [observer])
|
||||
const isValue = "value" in propertyDescriptor
|
||||
const hasSetter = "set" in propertyDescriptor
|
||||
if (!(isValue || hasSetter)) {
|
||||
throw new Error(`Property ${property} is not a value or a setter`)
|
||||
}
|
||||
// A Symbol so it does not show up in Object.getOwnPropertyNames()
|
||||
const storageKey = Symbol.for(property + "Storage")
|
||||
const valInfoKey = Symbol.for(property + "ValInfo")
|
||||
Object.defineProperties(
|
||||
fromPrototype ? Object.getPrototypeOf(this) : this,
|
||||
{
|
||||
[storageKey]: {
|
||||
configurable: true,
|
||||
enumerable: false, // Non enumerable so it does not show up in for...in or Object.keys()
|
||||
...(isValue
|
||||
? {
|
||||
value: this[property],
|
||||
writable: true,
|
||||
}
|
||||
: {
|
||||
get: propertyDescriptor.get,
|
||||
set: propertyDescriptor.set,
|
||||
}
|
||||
)
|
||||
},
|
||||
[valInfoKey]: {
|
||||
configurable: true,
|
||||
enumerable: false,
|
||||
value: [fromPrototype, isValue]
|
||||
},
|
||||
[property]: {
|
||||
configurable: true,
|
||||
...(isValue && {
|
||||
get() {
|
||||
return this[storageKey]
|
||||
}
|
||||
}),
|
||||
set(v) {
|
||||
this[storageKey] = v
|
||||
observers.get(property).forEach(observer => {
|
||||
observer(this[property])
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {String} property
|
||||
* @param {Object} observer
|
||||
*/
|
||||
unsubscribe(property, observer) {
|
||||
let observers = this.#observers.get(property)
|
||||
if (!observers?.includes(observer)) {
|
||||
return false
|
||||
}
|
||||
observers.splice(observers.indexOf(observer), 1)
|
||||
if (observers.length == 0) {
|
||||
const storageKey = Symbol.for(property + "Storage")
|
||||
const valInfoKey = Symbol.for(property + "ValInfo")
|
||||
const fromPrototype = this[valInfoKey][0]
|
||||
const isValue = this[valInfoKey][1]
|
||||
Object.defineProperty(
|
||||
fromPrototype ? Object.getPrototypeOf(this) : this,
|
||||
property,
|
||||
Object.getOwnPropertyDescriptor(fromPrototype ? Object.getPrototypeOf(this) : this, storageKey),
|
||||
)
|
||||
delete this[valInfoKey]
|
||||
delete this[storageKey]
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -161,8 +161,6 @@ export default class NodeElement extends ISelectableDraggableElement {
|
||||
this.pureFunction = this.entity.bIsPureFunc
|
||||
this.dragLinkObjects = []
|
||||
super.setLocation([this.entity.NodePosX.value, this.entity.NodePosY.value])
|
||||
this.entity.subscribe("AdvancedPinDisplay", value => this.advancedPinDisplay = value)
|
||||
this.entity.subscribe("Name", value => this.nodeName = value)
|
||||
if (this.entity.NodeWidth && this.entity.NodeHeight) {
|
||||
this.sizeX = this.entity.NodeWidth.value
|
||||
this.sizeY = this.entity.NodeHeight.value
|
||||
@@ -308,6 +306,7 @@ export default class NodeElement extends ISelectableDraggableElement {
|
||||
}
|
||||
}
|
||||
this.entity.Name = name
|
||||
this.nodeName = this.entity.Name
|
||||
}
|
||||
|
||||
getPinElements() {
|
||||
@@ -338,6 +337,7 @@ export default class NodeElement extends ISelectableDraggableElement {
|
||||
|
||||
setShowAdvancedPinDisplay(value) {
|
||||
this.entity.AdvancedPinDisplay = new IdentifierEntity(value ? "Shown" : "Hidden")
|
||||
this.advancedPinDisplay = this.entity.AdvancedPinDisplay
|
||||
}
|
||||
|
||||
toggleShowAdvancedPinDisplay() {
|
||||
|
||||
@@ -145,15 +145,6 @@ export default class PinElement extends IElement {
|
||||
this.isLinked = false
|
||||
this.pinDirection = entity.isInput() ? "input" : entity.isOutput() ? "output" : "hidden"
|
||||
this.nodeElement = /** @type {NodeElement} */(nodeElement)
|
||||
|
||||
// this.entity.subscribe("DefaultValue", value => this.defaultValue = value.toString())
|
||||
this.entity.subscribe("PinToolTip", value => {
|
||||
let matchResult = value.match(/\s*(.+?(?=\n)|.+\S)\s*/)
|
||||
if (matchResult) {
|
||||
return Utility.formatStringName(matchResult[1])
|
||||
}
|
||||
return Utility.formatStringName(this.entity.PinName)
|
||||
})
|
||||
}
|
||||
|
||||
setup() {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import Observable from "../Observable"
|
||||
import SerializerFactory from "../serialization/SerializerFactory"
|
||||
import SubAttributesDeclaration from "./SubObject"
|
||||
import UnionType from "./UnionType"
|
||||
@@ -28,7 +27,7 @@ import Utility from "../Utility"
|
||||
* @typedef {(new () => T) | EntityConstructor | StringConstructor | NumberConstructor | BooleanConstructor | ArrayConstructor} AnyValueConstructor
|
||||
*/
|
||||
|
||||
export default class IEntity extends Observable {
|
||||
export default class IEntity {
|
||||
|
||||
/** @type {AttributeDeclarations} */
|
||||
static attributes = {}
|
||||
@@ -40,7 +39,6 @@ export default class IEntity extends Observable {
|
||||
}
|
||||
|
||||
constructor(values = {}, suppressWarns = false) {
|
||||
super()
|
||||
/**
|
||||
* @param {Object} target
|
||||
* @param {Object} attributes
|
||||
|
||||
Reference in New Issue
Block a user