Why connections are lost when removipung all ints in updateShape_() and reattaching multiple inputs immediately?

23 views
Skip to first unread message

Andrey

unread,
Oct 30, 2025, 10:54:59 AM (5 days ago) Oct 30
to Blockly
  Why connections are lost when removing all inputs in updateShape_() and reattaching multiple inputs immediately?

  Hi everyone,

I’m working with a custom Blockly block that supports multiple mutation types.
Inside the updateShape_() method, I remove all existing inputs and then recreate them dynamically using appendValueInput().
If I reattach one input, its connection is restored correctly.
But when I reattach two or more inputs in sequence, the connections don’t reattach — the blocks become detached.

Here’s a simplified version of what my code looks like:

  1. updateShape_: function (renderShape = false) {
  2.   // Remove all dynamic inputs
  3.   for (let i = 0; true; i++) {
  4.     let removed = 0;
  5.     const inputs = ['REQUEST', 'RESPONSE'];
  6.     inputs.forEach((name) => {
  7.       try {
  8.         this.removeInput(this.getFullInputName_(name, i));
  9.         removed++;
  10.       } catch (err) {}
  11.     });
  12.     if (removed === 0) break;
  13.   }

  14.   // Recreate inputs
  15.   for (let i = 0; i < this.mutationCount_; i++) {
  16.     this.updateParam_(i, renderShape);
  17.   }
  18. },
  19.  
  20.  
  21.  
  22. updateParam_: function (i, renderShape = false) {
  23.   const mutationName = this.getMutationName(i);
  24.   const inputs = this.getMutationInputs(i);
  25.   const fields = this.getMutationFields(i);

  26.   switch (mutationName) {
  27.     case 'sap_call_method_param':
  28.       let input = this.appendValueInput(this.getFullInputName_('VALUE', i))
  29.         .appendField('field')
  30.         .appendField(new window.Blockly.FieldTextInput(fields?.NAME ?? ''), `${this.paramPrefix_}${i}_NAME`)
  31.         .appendField('as')
  32.         .appendField(new window.Blockly.FieldTextInput(fields?.AS ?? ''), `${this.paramPrefix_}${i}_AS`)
  33.         .appendField('type');
  34.       if (renderShape && inputs?.VALUE) {
  35.         input.connection.connect(
  36.           window.Blockly.getMainWorkspace().getBlockById(inputs.VALUE).outputConnection
  37.         );
  38.       }
  39.       break;

  40.     case 'sap_call_method_request':
  41.       let input2 = this.appendValueInput(this.getFullInputName_('REQUEST', i))
  42.         .appendField('TYPE')
  43.         .appendField(new window.Blockly.FieldTextInput(fields?.TYPE ?? ''), `${this.paramPrefix_}${i}_TYPE`)
  44.         .appendField('MODE')
  45.         .appendField(new window.Blockly.FieldTextInput(fields?.MODE ?? ''), `${this.paramPrefix_}${i}_MODE`)
  46.         .appendField('request');
  47.       if (renderShape && inputs?.REQUEST) {
  48.         input2.connection.connect(
  49.           window.Blockly.getMainWorkspace().getBlockById(inputs.REQUEST).outputConnection
  50.         );
  51.       }

  52.       let input3 = this.appendValueInput(this.getFullInputName_('RESPONSE', i))
  53.         .appendField('WHERE')
  54.         .appendField(new window.Blockly.FieldTextInput(fields?.WHERE ?? ''), `${this.paramPrefix_}${i}_WHERE`)
  55.         .appendField('PROTOCOL')
  56.         .appendField(new window.Blockly.FieldTextInput(fields?.PROTOCOL ?? ''), `${this.paramPrefix_}${i}_PROTOCOL`)
  57.         .appendField('response');
  58.       if (renderShape && inputs?.RESPONSE) {
  59.         input3.connection.connect(
  60.           window.Blockly.getMainWorkspace().getBlockById(inputs.RESPONSE).outputConnection
  61.         );
  62.       }
  63.       break;
  64.   }
  65. }



Problem
  • In the case of sap_call_method_param, the VALUE input reconnects successfully. 

  • In the case of sap_call_method_request, neither the REQUEST nor the RESPONSE inputs reconnect.

I confirmed that:

  • The inputs.REQUEST and inputs.RESPONSE contain valid block IDs

  • The blocks exist in the workspace at the moment of reconnection.


  Why do multiple inputs (REQUEST, RESPONSE) fail to reconnect when the block shape is rebuilt, while a single input (VALUE) reconnects just fine?  
Видео от 2025-10-30 10-42-00.mp4

epas...@google.com

unread,
Oct 30, 2025, 12:27:57 PM (5 days ago) Oct 30
to Blockly
From that code snippet it looks like you're trying to get the previously connected blocks after you've removed the inputs. When an input is removed, it's immediately disposed of which disconnects any blocks attached to it. You'd need to collect the list of blocks to reattach before removing any inputs and then pass it to your function that recreates the inputs.

You could also use your browser's debugging tools to step through the code and confirm what's in your list of inputs when you're trying to reconnect them.

Cheers,
Erik

Reply all
Reply to author
Forward
0 new messages