Managers use callback functions instead of signals

This commit is contained in:
GeoffreyCoulaud
2023-05-29 00:23:25 +02:00
parent b99c058cd7
commit 8ddb110cbb
3 changed files with 19 additions and 34 deletions

View File

@@ -1,3 +1,5 @@
from typing import Callable
from gi.repository import Gio
from src.game import Game
@@ -24,18 +26,17 @@ class AsyncManager(Manager):
Already scheduled Tasks will no longer be cancellable."""
self.cancellable = Gio.Cancellable()
def run(self, game: Game) -> None:
data = (game,)
task = Task.new(self, self.cancellable, self._task_callback, data)
task.set_task_data(data)
def run(self, game: Game, callback: Callable) -> None:
task = Task.new(self, self.cancellable, self._task_callback, (callback,))
task.set_task_data((game,))
task.run_in_thread(self._task_thread_func)
def _task_thread_func(self, _task, _source_object, data, cancellable):
"""Task thread entry point"""
game, *_rest = data
self.emit("started")
self.final_run(game)
def _task_callback(self, _source_object, _result, _data):
def _task_callback(self, _source_object, _result, data):
"""Method run after the async task is done"""
self.emit("done")
_game, callback, *_rest = data
callback(self)

View File

@@ -1,11 +1,10 @@
from abc import abstractmethod
from gi.repository import GObject
from typing import Callable
from src.game import Game
class Manager(GObject.Object):
class Manager:
"""Class in charge of handling a post creation action for games.
* May connect to signals on the game to handle them.
@@ -39,19 +38,8 @@ class Manager(GObject.Object):
* May not raise exceptions, as they will be silently ignored
"""
def run(self, game: Game) -> None:
def run(self, game: Game, callback: Callable[["Manager"]]) -> None:
"""Pass the game through the manager.
In charge of calling the final_run method."""
self.emit("started")
self.final_run(game)
self.emit("done")
@GObject.Signal(name="started")
def started(self) -> None:
"""Signal emitted when a manager is started"""
pass
@GObject.Signal(name="done")
def done(self) -> None:
"""Signal emitted when a manager is done"""
pass
callback(self)