connector value caching and concatinator

This commit is contained in:
Luca Conte 2025-02-17 16:10:45 +01:00
parent 14f95d0165
commit dddefbcf1d
3 changed files with 32 additions and 0 deletions

22
elements/Concatinator.js Normal file
View File

@ -0,0 +1,22 @@
class Concatinator extends Box {
constructor() {
super("Concatinator");
this.inputs.push(new Connector(Connector.INPUT, "any", ""));
this.inputs.push(new Connector(Connector.INPUT, "any", ""));
this.outputs.push(new Connector(Connector.OUTPUT, "string", ""));
for (let input of this.inputs) {
input.addListener((d) => {
let concatinated = "";
for (let input2 of this.inputs) {
concatinated = concatinated + input2.cache;
}
for (let output of this.outputs) {
output.send(concatinated);
}
})
}
}
}

View File

@ -13,6 +13,8 @@ class Connector {
listeners = [];
cache;
constructor(direction, type, label = null) {
this.direction = direction;
this.type = type;
@ -64,6 +66,10 @@ class Connector {
connect(connector) {
this.connection = connector;
this.element.classList.add("connected");
if (this.direction == this.INPUT) {
this.cache = this.connection.cache;
}
}
disconnect() {
@ -79,6 +85,7 @@ class Connector {
send(data) {
if (!this.connection) return;
this.cache = data;
this.connection.receive(data);
}
@ -87,6 +94,7 @@ class Connector {
console.error("invalid type received", data, typeof data, this.type);
return;
}
this.cache = data;
for (let listener of this.listeners) {
listener(data);
}

View File

@ -12,6 +12,7 @@
<script src="elements/RandomNumberGenerator.js"></script>
<script src="elements/NumberBox.js"></script>
<script src="elements/Duplicator.js"></script>
<script src="elements/Concatinator.js"></script>
<script src="elements/URLConverter.js"></script>
<script src="controllers/menu-controller.js"></script>
<script src="controllers/input-controller.js"></script>
@ -72,6 +73,7 @@
<div class="elementsGroup">
<span>Others</span>
<button onclick="addElement(new Duplicator())">Duplicator</button>
<button onclick="addElement(new Concatinator())">Concatinator</button>
</div>
</div>
</div>