@@ -100,7 +100,7 @@ def __enter__(self: _Self) -> _Self:
100100 def __exit__ (self , * exc : Any ) -> None :
101101 root_csid = self ._root_life_cycle_state_id
102102 root_model_state = self ._model_states_by_life_cycle_state_id [root_csid ]
103- self ._deep_unmount_model_states ([root_model_state ])
103+ self ._unmount_model_states ([root_model_state ])
104104
105105 # delete attributes here to avoid access after exiting context manager
106106 del self ._event_handlers
@@ -320,7 +320,7 @@ def _render_model_children(
320320 self ._render_model_children_without_old_state (new_state , raw_children )
321321 return None
322322 elif not raw_children :
323- self ._deep_unmount_model_states (list (old_state .children_by_key .values ()))
323+ self ._unmount_model_states (list (old_state .children_by_key .values ()))
324324 return None
325325
326326 child_type_key_tuples = list (_process_child_type_and_key (raw_children ))
@@ -335,7 +335,7 @@ def _render_model_children(
335335
336336 old_keys = set (old_state .children_by_key ).difference (new_keys )
337337 if old_keys :
338- self ._deep_unmount_model_states (
338+ self ._unmount_model_states (
339339 [old_state .children_by_key [key ] for key in old_keys ]
340340 )
341341
@@ -352,7 +352,7 @@ def _render_model_children(
352352 )
353353 else :
354354 if old_child_state .is_component_state :
355- self ._shallow_unmount_model_state ( old_child_state )
355+ self ._unmount_model_states ([ old_child_state ] )
356356 new_child_state = _update_element_model_state (
357357 old_child_state ,
358358 new_state ,
@@ -383,7 +383,7 @@ def _render_model_children(
383383 else :
384384 old_child_state = old_state .children_by_key .get (key )
385385 if old_child_state is not None :
386- self ._deep_unmount_model_states ([old_child_state ])
386+ self ._unmount_model_states ([old_child_state ])
387387 new_children .append (child )
388388
389389 def _render_model_children_without_old_state (
@@ -406,21 +406,20 @@ def _render_model_children_without_old_state(
406406 else :
407407 new_children .append (child )
408408
409- def _deep_unmount_model_states (self , old_states : List [_ModelState ]) -> None :
409+ def _unmount_model_states (self , old_states : List [_ModelState ]) -> None :
410410 to_unmount = old_states [::- 1 ] # unmount in reversed order of rendering
411411 while to_unmount :
412412 model_state = to_unmount .pop ()
413- self ._shallow_unmount_model_state (model_state )
414- to_unmount .extend (model_state .children_by_key .values ())
415413
416- def _shallow_unmount_model_state (self , old_state : _ModelState ) -> None :
417- for target in old_state .targets_by_event .values ():
418- del self ._event_handlers [target ]
414+ for target in model_state .targets_by_event .values ():
415+ del self ._event_handlers [target ]
416+
417+ if model_state .is_component_state :
418+ life_cycle_state = model_state .life_cycle_state
419+ del self ._model_states_by_life_cycle_state_id [life_cycle_state .id ]
420+ life_cycle_state .hook .component_will_unmount ()
419421
420- if old_state .is_component_state :
421- life_cycle_state = old_state .life_cycle_state
422- del self ._model_states_by_life_cycle_state_id [life_cycle_state .id ]
423- life_cycle_state .hook .component_will_unmount ()
422+ to_unmount .extend (model_state .children_by_key .values ())
424423
425424 def __repr__ (self ) -> str :
426425 return f"{ type (self ).__name__ } ({ self .root } )"
0 commit comments