https://kotlinlang.org logo
t

Tomas Kormanak

10/19/2021, 9:54 AM
Hi, a quick question. When does the function
Widget.dispose()
is called?
r

Robert Jaros

10/19/2021, 11:31 AM
Simple answer - when the widget is no longer used.
There are a few cases:
t

Tomas Kormanak

10/19/2021, 11:33 AM
By 'no longer used' you mean removed from DOM?
r

Robert Jaros

10/19/2021, 11:33 AM
no
t

Tomas Kormanak

10/19/2021, 11:33 AM
Removed from component hiearchy?
r

Robert Jaros

10/19/2021, 11:34 AM
it depends :)
let me explain step by step :)
🙌 1
👍 1
1. when a container is disposed all its children are disposed as well, so the whole tree under the container is disposed
2. disposing is only initiated by components used with state binding, which regenerate their content dynamically
3. the only exception is disposing Root container, which can be initiated by application hot reload or by some advanced components which use Root containers internally (e.g. Tabulator and React)
4. Disposed components are no longer usable and are no longer used (they should be garbage collected)
5. If you are creating your own containers, which create children components without using functionality inherited from
SimplePanel
you should override dispose() and dispose of your children manually.
The most important part is number 3. When using reactive state binding, your lambda builder is executed on every state change. Your components are recreated with new state but at the same time old tree of your components is disposed.
If you are not using reactive state, there is no need to dispose anything.
And disposing has nothing to do with DOM. Components can be attached and detached from the DOM many times.
t

Tomas Kormanak

10/19/2021, 12:02 PM
Thank you, great explanation. I would put it in documentation.
1
👍 1