From adbe2d88ee309d479a2a9f477ac00b749f94d368 Mon Sep 17 00:00:00 2001 From: miruka Date: Sun, 15 Mar 2020 16:18:32 -0400 Subject: [PATCH] Fix dict changing size problem in retry_decrypt_.. Also add a copy() method to models, and make them able to not have a sync_id (in which case they won't send pyotherside events). --- src/backend/matrix_client.py | 2 +- src/backend/models/model.py | 23 ++++++++++++++----- src/backend/models/model_item.py | 15 ++++++------ .../PythonBridge/Privates/EventHandlers.qml | 4 ++-- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/backend/matrix_client.py b/src/backend/matrix_client.py index ff6e2f51..33e0f86d 100644 --- a/src/backend/matrix_client.py +++ b/src/backend/matrix_client.py @@ -981,7 +981,7 @@ class MatrixClient(nio.AsyncClient): _, room_id, _ = sync_id - for ev in model.values(): + for ev in model.copy().values(): room = self.all_rooms[room_id] if isinstance(ev.source, nio.MegolmEvent): diff --git a/src/backend/models/model.py b/src/backend/models/model.py index 947dd902..343d70d8 100644 --- a/src/backend/models/model.py +++ b/src/backend/models/model.py @@ -2,7 +2,9 @@ from bisect import bisect from threading import RLock -from typing import TYPE_CHECKING, Any, Dict, Iterator, List, MutableMapping +from typing import ( + TYPE_CHECKING, Any, Dict, Iterator, List, MutableMapping, Optional, +) from blist import blist @@ -29,8 +31,8 @@ class Model(MutableMapping): Items in the model are kept sorted using the `ModelItem` subclass `__lt__`. """ - def __init__(self, sync_id: SyncId) -> None: - self.sync_id: SyncId = sync_id + def __init__(self, sync_id: Optional[SyncId]) -> None: + self.sync_id: Optional[SyncId] = sync_id self._data: Dict[Any, "ModelItem"] = {} self._sorted_data: List["ModelItem"] = blist() self._write_lock: RLock = RLock() @@ -86,7 +88,8 @@ class Model(MutableMapping): index = bisect(self._sorted_data, new) self._sorted_data.insert(index, new) - ModelItemInserted(self.sync_id, index, new) + if self.sync_id: + ModelItemInserted(self.sync_id, index, new) def __delitem__(self, key) -> None: @@ -98,7 +101,8 @@ class Model(MutableMapping): index = self._sorted_data.index(item) del self._sorted_data[index] - ModelItemDeleted(self.sync_id, index) + if self.sync_id: + ModelItemDeleted(self.sync_id, index) def __iter__(self) -> Iterator: @@ -116,4 +120,11 @@ class Model(MutableMapping): def clear(self) -> None: super().clear() - ModelCleared(self.sync_id) + if self.sync_id: + ModelCleared(self.sync_id) + + + def copy(self, sync_id: Optional[SyncId] = None) -> "Model": + new = type(self)(sync_id=sync_id) + new.update(self) + return new diff --git a/src/backend/models/model_item.py b/src/backend/models/model_item.py index 0177ed73..a71bdea1 100644 --- a/src/backend/models/model_item.py +++ b/src/backend/models/model_item.py @@ -42,13 +42,14 @@ class ModelItem: self.parent_model._sorted_data.sort() new_index = self.parent_model._sorted_data.index(self) - ModelItemFieldChanged( - self.parent_model.sync_id, - old_index, - new_index, - name, - self.serialize_field(name), - ) + if self.parent_model.sync_id: + ModelItemFieldChanged( + self.parent_model.sync_id, + old_index, + new_index, + name, + self.serialize_field(name), + ) def __delattr__(self, name: str) -> None: diff --git a/src/gui/PythonBridge/Privates/EventHandlers.qml b/src/gui/PythonBridge/Privates/EventHandlers.qml index 46124d86..113d7344 100644 --- a/src/gui/PythonBridge/Privates/EventHandlers.qml +++ b/src/gui/PythonBridge/Privates/EventHandlers.qml @@ -60,13 +60,13 @@ QtObject { function onModelItemInserted(syncId, index, item) { - // print("insert", syncId, index, item) + print("insert", syncId, index, item) ModelStore.get(syncId).insert(index, item) } function onModelItemFieldChanged(syncId, oldIndex, newIndex, field, value){ - // print("change", syncId, oldIndex, newIndex, field, value) + print("change", syncId, oldIndex, newIndex, field, value) const model = ModelStore.get(syncId) model.setProperty(oldIndex, field, value)