fix mixing of messages when changing applications too quickly
This commit is contained in:
@@ -166,16 +166,25 @@ class MainApplication(QtWidgets.QApplication):
|
|||||||
else:
|
else:
|
||||||
self.gotify_client.stop(reset_wait=True)
|
self.gotify_client.stop(reset_wait=True)
|
||||||
|
|
||||||
def application_selection_changed_callback(
|
def abort_get_messages_task(self):
|
||||||
self, item: ApplicationModelItem | ApplicationAllMessagesItem
|
"""
|
||||||
):
|
Abort any tasks that will result in new messages getting appended to messages_model
|
||||||
|
"""
|
||||||
|
aborted_tasks = []
|
||||||
|
for s in ["get_application_messages_task", "get_messages_task"]:
|
||||||
|
if task := getattr(self, s, None):
|
||||||
|
task.abort()
|
||||||
|
aborted_tasks.append(task)
|
||||||
|
|
||||||
|
for task in aborted_tasks:
|
||||||
|
task.wait()
|
||||||
|
|
||||||
|
def application_selection_changed_callback(self, item: ApplicationModelItem | ApplicationAllMessagesItem):
|
||||||
|
self.abort_get_messages_task()
|
||||||
self.messages_model.clear()
|
self.messages_model.clear()
|
||||||
|
|
||||||
if isinstance(item, ApplicationModelItem):
|
if isinstance(item, ApplicationModelItem):
|
||||||
self.get_application_messages_task = GetApplicationMessagesTask(
|
self.get_application_messages_task = GetApplicationMessagesTask(item.data(ApplicationItemDataRole.ApplicationRole).id, self.gotify_client)
|
||||||
item.data(ApplicationItemDataRole.ApplicationRole).id,
|
|
||||||
self.gotify_client,
|
|
||||||
)
|
|
||||||
self.get_application_messages_task.message.connect(self.messages_model.append_message)
|
self.get_application_messages_task.message.connect(self.messages_model.append_message)
|
||||||
self.get_application_messages_task.start()
|
self.get_application_messages_task.start()
|
||||||
|
|
||||||
|
|||||||
@@ -26,11 +26,18 @@ class BaseTask(QtCore.QThread):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(BaseTask, self).__init__()
|
super(BaseTask, self).__init__()
|
||||||
self.running = False
|
self.running = False
|
||||||
|
self._abort = False
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def task(self):
|
def task(self):
|
||||||
...
|
...
|
||||||
|
|
||||||
|
def abort(self):
|
||||||
|
self._abort = True
|
||||||
|
|
||||||
|
def abort_requested(self) -> bool:
|
||||||
|
return self._abort
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.running = True
|
self.running = True
|
||||||
try:
|
try:
|
||||||
@@ -111,6 +118,8 @@ class GetApplicationMessagesTask(BaseTask):
|
|||||||
self.error.emit(result)
|
self.error.emit(result)
|
||||||
else:
|
else:
|
||||||
for message in process_messages(result.messages):
|
for message in process_messages(result.messages):
|
||||||
|
if self.abort_requested():
|
||||||
|
return
|
||||||
self.message.emit(message)
|
self.message.emit(message)
|
||||||
|
|
||||||
# Prevent locking up the UI when there are a lot of messages ready at the same time
|
# Prevent locking up the UI when there are a lot of messages ready at the same time
|
||||||
@@ -133,6 +142,8 @@ class GetMessagesTask(BaseTask):
|
|||||||
self.error.emit(result)
|
self.error.emit(result)
|
||||||
else:
|
else:
|
||||||
for message in process_messages(result.messages):
|
for message in process_messages(result.messages):
|
||||||
|
if self.abort_requested():
|
||||||
|
return
|
||||||
self.message.emit(message)
|
self.message.emit(message)
|
||||||
time.sleep(0.001)
|
time.sleep(0.001)
|
||||||
self.success.emit(result)
|
self.success.emit(result)
|
||||||
|
|||||||
Reference in New Issue
Block a user