Skip to content

Commit 5727ab4

Browse files
committed
use unique object instead of index as default key
we can change this in a later pull request
1 parent 384096d commit 5727ab4

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

src/idom/core/layout.py

+27-27
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,33 @@ async def get(self) -> AbstractComponent:
450450
return component
451451

452452

453+
def _process_child_type_and_key(
454+
children: List[Any],
455+
) -> Iterator[Tuple[Any, int, Any]]:
456+
for child in children:
457+
if isinstance(child, dict):
458+
child_type = _DICT_TYPE
459+
key = child.get("key")
460+
elif isinstance(child, AbstractComponent):
461+
child_type = _COMPONENT_TYPE
462+
key = getattr(child, "key", None)
463+
else:
464+
child = f"{child}"
465+
child_type = _STRING_TYPE
466+
key = None
467+
468+
if key is None:
469+
key = object()
470+
471+
yield (child, child_type, key)
472+
473+
474+
# used in _process_child_type_and_key
475+
_DICT_TYPE = 1
476+
_COMPONENT_TYPE = 2
477+
_STRING_TYPE = 3
478+
479+
453480
class _ModelEventTarget(TypedDict):
454481
target: str
455482
preventDefault: bool # noqa
@@ -475,30 +502,3 @@ class _ModelVdomRequired(TypedDict, total=True):
475502

476503
class _ModelVdom(_ModelVdomRequired, _ModelVdomOptional):
477504
"""A VDOM dictionary model specifically for use with a :class:`Layout`"""
478-
479-
480-
def _process_child_type_and_key(
481-
raw_children: List[Any],
482-
) -> Iterator[Tuple[Any, int, Any]]:
483-
for index, child in enumerate(raw_children):
484-
if isinstance(child, dict):
485-
child_type = _DICT_TYPE
486-
key = child.get("key")
487-
elif isinstance(child, AbstractComponent):
488-
child_type = _COMPONENT_TYPE
489-
key = getattr(child, "key", None)
490-
else:
491-
child = f"{child}"
492-
child_type = _STRING_TYPE
493-
key = None
494-
495-
if key is None:
496-
key = index
497-
498-
yield (child, child_type, key)
499-
500-
501-
# used in _process_child_type_and_key
502-
_DICT_TYPE = 1
503-
_COMPONENT_TYPE = 2
504-
_STRING_TYPE = 3

tests/test_core/test_layout.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,14 @@ async def test_components_are_garbage_collected():
181181
def Outer():
182182
component = idom.hooks.current_hook().component
183183
live_components.add(id(component))
184-
finalize(component, live_components.remove, id(component))
184+
finalize(component, live_components.discard, id(component))
185185
return Inner()
186186

187187
@idom.component
188188
def Inner():
189189
component = idom.hooks.current_hook().component
190190
live_components.add(id(component))
191-
finalize(component, live_components.remove, id(component))
191+
finalize(component, live_components.discard, id(component))
192192
return idom.html.div()
193193

194194
async with idom.Layout(Outer()) as layout:

0 commit comments

Comments
 (0)