mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-04-16 06:03:11 +00:00
Avoid re-creating View children on edit
The older code attempted to be clever and sync component additions and removals with what the message edit is doing. In some cases, this led to the re-creation of those components causing lost attributes to be dropped such as `_rendered_row` which would mess up handling of view weights. Instead of recreating the children list every time and keeping track of additions and removals, this change just updates the old state with the new state while ignoring any new or removed additions. This should work fine in theory due to additions or removals already being present before editing the View instance in the first place. Closes #7231 #7511
This commit is contained in:
parent
da9abca74d
commit
e198a0e7e6
@ -441,25 +441,26 @@ class View:
|
||||
asyncio.create_task(self._scheduled_task(item, interaction), name=f'discord-ui-view-dispatch-{self.id}')
|
||||
|
||||
def _refresh(self, components: List[Component]) -> None:
|
||||
# This is pretty hacky at the moment
|
||||
# fmt: off
|
||||
old_state: Dict[Tuple[int, str], Item[Any]] = {
|
||||
(item.type.value, item.custom_id): item # type: ignore
|
||||
old_state: Dict[str, Item[Any]] = {
|
||||
item.custom_id: item # type: ignore
|
||||
for item in self._children
|
||||
if item.is_dispatchable()
|
||||
}
|
||||
# fmt: on
|
||||
children: List[Item[Any]] = []
|
||||
|
||||
for component in _walk_all_components(components):
|
||||
custom_id = getattr(component, 'custom_id', None)
|
||||
if custom_id is None:
|
||||
continue
|
||||
|
||||
try:
|
||||
older = old_state[(component.type.value, component.custom_id)] # type: ignore
|
||||
except (KeyError, AttributeError):
|
||||
children.append(_component_to_item(component))
|
||||
older = old_state[custom_id]
|
||||
except KeyError:
|
||||
_log.debug('View interaction referenced an unknown item custom_id %s. Discarding', custom_id)
|
||||
continue
|
||||
else:
|
||||
older._refresh_component(component)
|
||||
children.append(older)
|
||||
|
||||
self._children = children
|
||||
|
||||
def stop(self) -> None:
|
||||
"""Stops listening to interaction events from this view.
|
||||
|
Loading…
x
Reference in New Issue
Block a user