Big performance refactoring & various improvements
Instead of passing all sorts of events for the JS to handle and manually
add to different data models, we now handle everything we can in Python.
For any change, the python models send a sync event with their
contents (no more than 4 times per second) to JS, and the QSyncable
library's JsonListModel takes care of converting it to a QML ListModel
and sending the appropriate signals.
The SortFilterProxyModel library is not used anymore, the only case
where we need to filter/sort something now is when the user interacts
with the "Filter rooms" or "Filter members" fields. These cases are
handled by a simple JS function.
We now keep separated room and timeline models for different accounts,
the previous approach of sharing all the data we could between accounts
created a lot of complications (local echoes, decrypted messages
replacing others, etc).
The users's own account profile changes are now hidden in the timeline.
On startup, if all events for a room were only own profile changes, more
events will be loaded.
Any kind of image format supported by Qt is now handled by the
pyotherside image provider, instead of just PNG/JPG.
SVGs which previously caused errors are supported as well.
The typing members bar paddings/margins are fixed.
The behavior of the avatar/"upload a profile picture" overlay is fixed.
Config files read from disk are now cached (TODO: make them reloadable
again).
Pylint is not used anymore because of all its annoying false warnings
and lack of understanding for dataclasses, it is replaced by flake8 with
a custom config and various plugins.
Debug mode is now considered on if the program was compiled with
the right option, instead of taking an argument from CLI.
When on, C++ will set a flag in the Window QML component.
The loading screen is now unloaded after the UI is ready, where
previously it just stayed in the background invisible and wasted CPU.
The overall refactoring and improvements make us now able to handle
rooms with thousand of members and no lazy-loading, where previously
everything would freeze and simply scrolling up to load past events
in any room would block the UI for a few seconds.
2019-08-11 22:01:22 +10:00
|
|
|
import logging as log
|
|
|
|
import time
|
|
|
|
from threading import Lock, Thread
|
|
|
|
from typing import Any, Dict, Iterator, List, MutableMapping, Optional
|
|
|
|
|
|
|
|
from . import SyncId
|
|
|
|
from ..pyotherside_events import ModelUpdated
|
|
|
|
from .model_item import ModelItem
|
|
|
|
|
|
|
|
|
|
|
|
class Model(MutableMapping):
|
|
|
|
def __init__(self, sync_id: SyncId) -> None:
|
|
|
|
self.sync_id: SyncId = sync_id
|
|
|
|
self.sortable: Optional[bool] = None
|
|
|
|
self._data: Dict[Any, ModelItem] = {}
|
|
|
|
|
|
|
|
self._changed: bool = False
|
|
|
|
self._sync_lock: Lock = Lock()
|
|
|
|
self._sync_thread: Thread = Thread(target=self._sync_loop, daemon=True)
|
|
|
|
self._sync_thread.start()
|
|
|
|
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
try:
|
|
|
|
from pprintpp import pformat
|
|
|
|
except ImportError:
|
|
|
|
from pprint import pformat # type: ignore
|
|
|
|
|
|
|
|
if isinstance(self.sync_id, tuple):
|
|
|
|
sid = (self.sync_id[0].__name__, *self.sync_id[1:]) # type: ignore
|
|
|
|
else:
|
|
|
|
sid = self.sync_id.__name__ # type: ignore
|
|
|
|
|
|
|
|
return "%s(sync_id=%s, sortable=%r, %s)" % (
|
|
|
|
type(self).__name__, sid, self.sortable, pformat(self._data),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
if isinstance(self.sync_id, tuple):
|
2019-08-15 23:57:58 +10:00
|
|
|
reprs = tuple(repr(s) for s in self.sync_id[1:])
|
|
|
|
sid = ", ".join((self.sync_id[0].__name__, *reprs)) # type: ignore
|
|
|
|
sid = f"({sid})"
|
Big performance refactoring & various improvements
Instead of passing all sorts of events for the JS to handle and manually
add to different data models, we now handle everything we can in Python.
For any change, the python models send a sync event with their
contents (no more than 4 times per second) to JS, and the QSyncable
library's JsonListModel takes care of converting it to a QML ListModel
and sending the appropriate signals.
The SortFilterProxyModel library is not used anymore, the only case
where we need to filter/sort something now is when the user interacts
with the "Filter rooms" or "Filter members" fields. These cases are
handled by a simple JS function.
We now keep separated room and timeline models for different accounts,
the previous approach of sharing all the data we could between accounts
created a lot of complications (local echoes, decrypted messages
replacing others, etc).
The users's own account profile changes are now hidden in the timeline.
On startup, if all events for a room were only own profile changes, more
events will be loaded.
Any kind of image format supported by Qt is now handled by the
pyotherside image provider, instead of just PNG/JPG.
SVGs which previously caused errors are supported as well.
The typing members bar paddings/margins are fixed.
The behavior of the avatar/"upload a profile picture" overlay is fixed.
Config files read from disk are now cached (TODO: make them reloadable
again).
Pylint is not used anymore because of all its annoying false warnings
and lack of understanding for dataclasses, it is replaced by flake8 with
a custom config and various plugins.
Debug mode is now considered on if the program was compiled with
the right option, instead of taking an argument from CLI.
When on, C++ will set a flag in the Window QML component.
The loading screen is now unloaded after the UI is ready, where
previously it just stayed in the background invisible and wasted CPU.
The overall refactoring and improvements make us now able to handle
rooms with thousand of members and no lazy-loading, where previously
everything would freeze and simply scrolling up to load past events
in any room would block the UI for a few seconds.
2019-08-11 22:01:22 +10:00
|
|
|
else:
|
|
|
|
sid = self.sync_id.__name__ # type: ignore
|
|
|
|
|
|
|
|
return f"{sid!s}: {len(self)} items"
|
|
|
|
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
return self._data[key]
|
|
|
|
|
|
|
|
|
|
|
|
def __setitem__(self, key, value: ModelItem) -> None:
|
|
|
|
new = value
|
|
|
|
|
|
|
|
if key in self:
|
|
|
|
existing = dict(self[key].__dict__) # copy to not alter with pop
|
|
|
|
merged = {**existing, **value.__dict__}
|
|
|
|
|
|
|
|
existing.pop("parent_model", None)
|
|
|
|
merged.pop("parent_model", None)
|
|
|
|
|
|
|
|
if merged == existing:
|
|
|
|
return
|
|
|
|
|
|
|
|
new = type(value)(**merged) # type: ignore
|
|
|
|
|
|
|
|
new.parent_model = self
|
|
|
|
|
|
|
|
with self._sync_lock:
|
|
|
|
self._data[key] = new
|
|
|
|
self._changed = True
|
|
|
|
|
|
|
|
|
|
|
|
def __delitem__(self, key) -> None:
|
|
|
|
with self._sync_lock:
|
|
|
|
del self._data[key]
|
|
|
|
self._changed = True
|
|
|
|
|
|
|
|
|
|
|
|
def __iter__(self) -> Iterator:
|
|
|
|
return iter(self._data)
|
|
|
|
|
|
|
|
|
|
|
|
def __len__(self) -> int:
|
|
|
|
return len(self._data)
|
|
|
|
|
|
|
|
|
|
|
|
def _sync_loop(self) -> None:
|
|
|
|
while True:
|
|
|
|
time.sleep(0.25)
|
|
|
|
|
|
|
|
if self._changed:
|
|
|
|
with self._sync_lock:
|
|
|
|
log.debug("Syncing %s", self)
|
|
|
|
ModelUpdated(self.sync_id, self.serialized())
|
|
|
|
self._changed = False
|
|
|
|
|
|
|
|
|
|
|
|
def serialized(self) -> List[Dict[str, Any]]:
|
|
|
|
if self.sortable is True:
|
|
|
|
return [item.__dict__ for item in sorted(self._data.values())]
|
|
|
|
|
|
|
|
if self.sortable is False or len(self) < 2:
|
|
|
|
return [item.__dict__ for item in self._data.values()]
|
|
|
|
|
|
|
|
try:
|
|
|
|
return [item.__dict__ for item in sorted(self._data.values())]
|
|
|
|
except TypeError:
|
|
|
|
self.sortable = False
|
|
|
|
else:
|
|
|
|
self.sortable = True
|
|
|
|
|
|
|
|
return self.serialized()
|
2019-08-15 23:57:58 +10:00
|
|
|
|
|
|
|
|
|
|
|
def __lt__(self, other: "Model") -> bool:
|
|
|
|
return str(self.sync_id) < str(other.sync_id)
|