Skip to content

Commit

Permalink
use unique object instead of index as default key
Browse files Browse the repository at this point in the history
we can change this in a later pull request
  • Loading branch information
rmorshea committed Apr 17, 2021
1 parent 384096d commit 5727ab4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
54 changes: 27 additions & 27 deletions src/idom/core/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,33 @@ async def get(self) -> AbstractComponent:
return component


def _process_child_type_and_key(
children: List[Any],
) -> Iterator[Tuple[Any, int, Any]]:
for child in children:
if isinstance(child, dict):
child_type = _DICT_TYPE
key = child.get("key")
elif isinstance(child, AbstractComponent):
child_type = _COMPONENT_TYPE
key = getattr(child, "key", None)
else:
child = f"{child}"
child_type = _STRING_TYPE
key = None

if key is None:
key = object()

yield (child, child_type, key)


# used in _process_child_type_and_key
_DICT_TYPE = 1
_COMPONENT_TYPE = 2
_STRING_TYPE = 3


class _ModelEventTarget(TypedDict):
target: str
preventDefault: bool # noqa
Expand All @@ -475,30 +502,3 @@ class _ModelVdomRequired(TypedDict, total=True):

class _ModelVdom(_ModelVdomRequired, _ModelVdomOptional):
"""A VDOM dictionary model specifically for use with a :class:`Layout`"""


def _process_child_type_and_key(
raw_children: List[Any],
) -> Iterator[Tuple[Any, int, Any]]:
for index, child in enumerate(raw_children):
if isinstance(child, dict):
child_type = _DICT_TYPE
key = child.get("key")
elif isinstance(child, AbstractComponent):
child_type = _COMPONENT_TYPE
key = getattr(child, "key", None)
else:
child = f"{child}"
child_type = _STRING_TYPE
key = None

if key is None:
key = index

yield (child, child_type, key)


# used in _process_child_type_and_key
_DICT_TYPE = 1
_COMPONENT_TYPE = 2
_STRING_TYPE = 3
4 changes: 2 additions & 2 deletions tests/test_core/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,14 @@ async def test_components_are_garbage_collected():
def Outer():
component = idom.hooks.current_hook().component
live_components.add(id(component))
finalize(component, live_components.remove, id(component))
finalize(component, live_components.discard, id(component))
return Inner()

@idom.component
def Inner():
component = idom.hooks.current_hook().component
live_components.add(id(component))
finalize(component, live_components.remove, id(component))
finalize(component, live_components.discard, id(component))
return idom.html.div()

async with idom.Layout(Outer()) as layout:
Expand Down

0 comments on commit 5727ab4

Please sign in to comment.