clan_vm_manager: Fix GLib.idle_add rexecuting the VM push multiple times because of missing GLib.SOURCE_REMOVE
Some checks failed
checks / check-links (pull_request) Successful in 23s
checks / checks-impure (pull_request) Successful in 1m56s
checks / checks (pull_request) Failing after 2m22s

This commit is contained in:
Luis Hebendanz 2024-03-01 01:58:03 +07:00
parent d079bc85a8
commit 5f1191148e
3 changed files with 19 additions and 10 deletions

View File

@ -136,15 +136,18 @@ class GKVStore(GObject.GObject, Gio.ListModel, Generic[K, V]):
# O(1) operation if the key does not exist, O(n) if it does
def __setitem__(self, key: K, value: V) -> None:
# If the key already exists, remove it O(n)
# TODO: We have to check if updating an existing key is working correctly
if key in self._items:
log.warning("Updating an existing key in GKVStore is O(n)")
del self[key]
# Add the new key-value pair
self._items[key] = value
self._items.move_to_end(key)
position = len(self._items) - 1
self.items_changed(position, 0, 1)
position = self.keys().index(key)
self._items[key] = value
self.items_changed(position, 0, 1)
else:
# Add the new key-value pair
position = max(len(self._items) - 1, 0)
self._items[key] = value
self._items.move_to_end(key)
self.items_changed(position, 0, 1)
# O(n) operation
def __delitem__(self, key: K) -> None:

View File

@ -35,7 +35,7 @@ class JoinValue(GObject.Object):
def __join(self) -> None:
add_history(self.url, all_machines=False)
GLib.idle_add(lambda: self.emit("join_finished", self))
GLib.idle_add(self.emit, "join_finished", self)
def join(self) -> None:
threading.Thread(target=self.__join).start()

View File

@ -1,5 +1,5 @@
import logging
import threading
import time
from pathlib import Path
from typing import Any
@ -19,6 +19,8 @@ from gi.repository import Adw, Gio, GLib, Gtk
from ..trayicon import TrayIcon
log = logging.getLogger(__name__)
class MainWindow(Adw.ApplicationWindow):
def __init__(self, config: ClanConfig) -> None:
@ -59,6 +61,10 @@ class MainWindow(Adw.ApplicationWindow):
self.connect("destroy", self.on_destroy)
def push_vm(self, vm: VM) -> bool:
VMs.use().push(vm)
return GLib.SOURCE_REMOVE
def _populate_vms(self) -> None:
# Execute `clan flakes add <path>` to democlan for this to work
# TODO: Make list_history a generator function
@ -72,7 +78,7 @@ class MainWindow(Adw.ApplicationWindow):
icon=Path(icon),
data=entry,
)
GLib.idle_add(lambda: VMs.use().push(vm))
GLib.idle_add(self.push_vm, vm)
def on_destroy(self, *_args: Any) -> None:
self.tray_icon.destroy()