blockly: Custom handling when the block is disposed
Is your feature request related to a problem? Please describe.
If I understood correctly (and has it is in the code), we cannot yet make a variable referenced by a field other than a FieldVariable. I needed a way to make 2 fields, a text field and a dropdown field, being linked somehow, so whenever we change the value in the text field, it is updated in the dropdown field at the same time, mainly when the value selected is the same as the value from the text field. In order to achieve this, when the block of the text field is created, it creates a variable with the same id as the block id, and the same name as the text field value. Then, when the value of the text field is updated, it renames the appropriate variable with the new value. From there, the block of the dropdown field listen when the variable is renamed, and then update the current value if needed. Everything works just fine, except the block of the text field is disposed. As the block does not get the “delete” event of himself, it can’t delete the related variable, and the value will never be updated in the dropdown field.
Describe the solution you’d like
There’s already init function at the block definition, maybe we could have destroy function which would be called when the block is disposed, then we would have the same principle has a constructor/destructor.
Describe alternatives you’ve considered
Other ideas:
- Create a new type of extension that would be called when the block is disposed instead of when the block is created.
- Dispose the block after the
onchangefunction is called which would let the current block get the event of it’s deletion.
Additional context
Note that this is pretty much a workaround a field not being able to reference a variable. It would be better to get this feature as the variable’s name would depend on the text field value, and calling refreshVariableName would update the dropdown field value. However, we would still need a way to delete the variable when the block of the text field is disposed.
Any other workarounds or ideas to handle this. The only thing I want to avoid if possible, is to handle this in the workspace change listener, as I would need to handle every disposed block in there.
Thank you.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 18 (6 by maintainers)
BTW:
destroyseems like a reasonable choice, but I might have gone withonDisposeorfinalize(well, actuallyfinalise, but I realise that Neil and I are probably losing the battle for proper spelling…)Hi @BeksOmega, thank you for reopening the issue. To be honest, since my code was working with the workspace listener, I didn’t try out to handle it in the block listener. However, I would like to suggest one last time the
destroyfunction, I don’t really see why it would not be appropriate since we already have theinitfunction, maybe it’s one more function to the api, but I just think that when you know that you have a function to setup your block, if you want to clean, you would lookup to a specific function as well, at least that’s how I think, maybe I’m wrong. Anyway, thank you for considering this issue!