clan_vm_manager: Fix incorrect signal behaviour in GKVStore setitem

This commit is contained in:
Luis Hebendanz 2024-03-03 15:44:16 +07:00
parent c6a2db15a7
commit 976b4a2c3a
3 changed files with 13 additions and 6 deletions

View File

@ -95,6 +95,10 @@ class GKVStore(GObject.GObject, Gio.ListModel, Generic[K, V]):
# # # #
######################### #########################
def insert(self, position: int, item: V) -> None: def insert(self, position: int, item: V) -> None:
log.warning("Inserting is O(n) in GKVStore. Better use append")
log.warning(
"This functions may have incorrect items_changed signal behavior. Please test it"
)
key = self.key_gen(item) key = self.key_gen(item)
if key in self._items: if key in self._items:
raise ValueError("Key already exists in the dictionary") raise ValueError("Key already exists in the dictionary")
@ -141,12 +145,12 @@ class GKVStore(GObject.GObject, Gio.ListModel, Generic[K, V]):
log.warning("Updating an existing key in GKVStore is O(n)") log.warning("Updating an existing key in GKVStore is O(n)")
position = self.keys().index(key) position = self.keys().index(key)
self._items[key] = value self._items[key] = value
self.items_changed(position, 0, 1) self.items_changed(position, 1, 1)
else: else:
# Add the new key-value pair # Add the new key-value pair
position = max(len(self._items) - 1, 0)
self._items[key] = value self._items[key] = value
self._items.move_to_end(key) self._items.move_to_end(key)
position = max(len(self._items) - 1, 0)
self.items_changed(position, 0, 1) self.items_changed(position, 0, 1)
# O(n) operation # O(n) operation

View File

@ -1,5 +1,6 @@
import logging import logging
import threading import threading
from collections.abc import Callable
from typing import Any, ClassVar from typing import Any, ClassVar
import gi import gi
@ -67,17 +68,20 @@ class JoinList:
def is_empty(self) -> bool: def is_empty(self) -> bool:
return self.list_store.get_n_items() == 0 return self.list_store.get_n_items() == 0
def push(self, value: JoinValue) -> None: def push(
self, value: JoinValue, after_join: Callable[[JoinValue, JoinValue], None]
) -> None:
""" """
Add a join request. Add a join request.
This method can add multiple join requests if called subsequently for each request. This method can add multiple join requests if called subsequently for each request.
""" """
if value.url.get_id() in [item.url.get_id() for item in self.list_store]: if value.url.get_id() in [item.url.get_id() for item in self.list_store]:
log.info(f"Join request already exists: {value.url}") log.info(f"Join request already exists: {value.url}. Ignoring.")
return return
value.connect("join_finished", self._on_join_finished) value.connect("join_finished", self._on_join_finished)
value.connect("join_finished", after_join)
self.list_store.append(value) self.list_store.append(value)

View File

@ -223,8 +223,7 @@ class ClanList(Gtk.Box):
log.debug("Join request: %s", url) log.debug("Join request: %s", url)
clan_uri = ClanURI.from_str(url) clan_uri = ClanURI.from_str(url)
value = JoinValue(url=clan_uri) value = JoinValue(url=clan_uri)
JoinList.use().push(value) JoinList.use().push(value, self.on_after_join)
value.connect("join_finished", self.on_after_join)
def on_after_join(self, source: JoinValue, item: JoinValue) -> None: def on_after_join(self, source: JoinValue, item: JoinValue) -> None:
# If the join request list is empty disable the shadow artefact # If the join request list is empty disable the shadow artefact