I changed a few things
1) I moved to 8.0.0
2) This avoids the "already registered"-error even though I wonder why this would be necessary
if (!Blockly.Extensions.isRegistered("add_math_number")) {
Blockly.Extensions.register('add_math_number', function() {....}
}
3) Just to be 100% sure I added this around the apply-method in init()
if (Blockly.Extensions.isRegistered("add_math_number")) {
console.log("init -> add_math_number is registered, so apply it")
Blockly.Extensions.apply("add_math_number", this, false);
}
At least I don't get exceptions anymore but the duplication still happens
I added a few logs (in init, in registering and in the function of the math_number block)
a) I am open the blockly workspace and it registers add_math_number
-------- registering add_math_number
b) I open the toolbox with that parent block
------- INIT
init -> add_math_number is registered, so apply it
add_math_number function is called
Note that the number block is not created which is most like because of the following line that checks the id's of the workspace
if (
this.workspace.id == Blockly.getMainWorkspace().id) {
console.log('----- creating number block')
let mathNumberBlock = this.workspace.newBlock('math_number');
c) I drag to block to the workspace
------- INIT
init -> add_math_number is registered, so apply it
-------- add_math_number function is called
----- creating number block id= tZ}D|[jsEUTZKv3@=U,h
Note that only one number block in the parent is shown on the workspace
d) I drag the block
------- INIT
init -> add_math_number is registered, so apply it
-------- add_math_number function is called
----- creating number block id = O0jF+)wrG(4ba.hz5.wU
Init is called again and another block is created!
Note that this block does not appear
e) I save and check the serialization and it only reveals one number block which at least is consistent to what I view
<block type="oh_zdt_temporal_unit_input" id="o35YQ!A_)rpIN5(PAX9h" x="93" y="319">
<value name="value">
<block type="math_number" id="tZ}D|[jsEUTZKv3@=U,h">
<field name="NUM">0</field>
</block>
</value>
</block>
</xml>
You can see that only the first num-block created (c) appears within the serialization
f) The problem happens when I reopen the workspace (=deserialization)
(Before the workspace is opened I verify that the persisted xml is still the same as above and it is)
------- INIT
init -> add_math_number is registered, so apply it
-------- add_math_number function is called
----- creating number block id = z2$[{jv[e)8oCEZ64D-
It is creating a new number block with a new id and this time is a single number block floating around in the workspace
and to no surprise it appears in the serialized XML
<block type="oh_zdt_temporal_unit_input" id="o35YQ!A_)rpIN5(PAX9h" x="93" y="319">
<value name="value">
<block type="math_number" id="tZ}D|[jsEUTZKv3@=U,h">
<field. name="NUM">0</field>
</block>
</value>
</block>
<block type="math_number id="z2$[{jv[e)8oCEZ64D-" x="166" y="355">
<field name="NUM">0</field>
</block>
</xml>