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).
This commit is contained in:
		@@ -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):
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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:
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user