programmatically add inputs to a custom block

snap8
Jens Mönig 2022-05-01 18:36:37 +02:00
rodzic 105acbd7cd
commit 47145ca8f1
2 zmienionych plików z 23 dodań i 3 usunięć

Wyświetl plik

@ -47,6 +47,7 @@
### 2022-05-01
* byob: programmatically reduce the number of inputs in a custom block
* byob: programmatically add inputs to a custom block
### 2022-04-28
* threads, byob: programmatically re-define custom blocks, experimental, under construction

Wyświetl plik

@ -569,11 +569,13 @@ CustomBlockDefinition.prototype.setBlockDefinition = function (aContext) {
declarations = this.declarations,
parts = [];
// remove excess inputs or add missing ones
if (oldInputs.length > newInputs.length) {
this.removeInputs(oldInputs.length - newInputs.length);
spec = this.abstractBlockSpec();
} else if (oldInputs.length !== newInputs.length) {
throw new Error('expecting the number of inputs to match');
} else if (oldInputs.length > newInputs.length) {
this.addInputs(newInputs.length - oldInputs.length);
spec = this.abstractBlockSpec();
}
// change the input names in the spec to those of the given context
@ -618,7 +620,24 @@ CustomBlockDefinition.prototype.removeInputs = function (count) {
};
CustomBlockDefinition.prototype.addInputs = function (count) {
// private
// private - only to be called from a Process that also does housekeeping
var inputNames = this.inputNames(),
i;
// create gensyms
for (i = 0; i < count; i += 1) {
inputNames.push(this.gensym(inputNames));
}
// add gensyms to the spec
this.spec = this.parseSpec(this.spec).concat(
inputNames.slice(-count).map(str => '%' + str)
).join(' ').trim();
// add slot declarations for the gensyms
inputNames.slice(-count).forEach(name =>
this.declarations.set(name, ['%s'])
);
};
CustomBlockDefinition.prototype.gensym = function (existing) {