Managers use callback functions instead of signals
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
from typing import Callable
|
||||||
|
|
||||||
from gi.repository import Gio
|
from gi.repository import Gio
|
||||||
|
|
||||||
from src.game import Game
|
from src.game import Game
|
||||||
@@ -24,18 +26,17 @@ class AsyncManager(Manager):
|
|||||||
Already scheduled Tasks will no longer be cancellable."""
|
Already scheduled Tasks will no longer be cancellable."""
|
||||||
self.cancellable = Gio.Cancellable()
|
self.cancellable = Gio.Cancellable()
|
||||||
|
|
||||||
def run(self, game: Game) -> None:
|
def run(self, game: Game, callback: Callable) -> None:
|
||||||
data = (game,)
|
task = Task.new(self, self.cancellable, self._task_callback, (callback,))
|
||||||
task = Task.new(self, self.cancellable, self._task_callback, data)
|
task.set_task_data((game,))
|
||||||
task.set_task_data(data)
|
|
||||||
task.run_in_thread(self._task_thread_func)
|
task.run_in_thread(self._task_thread_func)
|
||||||
|
|
||||||
def _task_thread_func(self, _task, _source_object, data, cancellable):
|
def _task_thread_func(self, _task, _source_object, data, cancellable):
|
||||||
"""Task thread entry point"""
|
"""Task thread entry point"""
|
||||||
game, *_rest = data
|
game, *_rest = data
|
||||||
self.emit("started")
|
|
||||||
self.final_run(game)
|
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"""
|
"""Method run after the async task is done"""
|
||||||
self.emit("done")
|
_game, callback, *_rest = data
|
||||||
|
callback(self)
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
from abc import abstractmethod
|
from abc import abstractmethod
|
||||||
|
from typing import Callable
|
||||||
from gi.repository import GObject
|
|
||||||
|
|
||||||
from src.game import Game
|
from src.game import Game
|
||||||
|
|
||||||
|
|
||||||
class Manager(GObject.Object):
|
class Manager:
|
||||||
"""Class in charge of handling a post creation action for games.
|
"""Class in charge of handling a post creation action for games.
|
||||||
|
|
||||||
* May connect to signals on the game to handle them.
|
* 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
|
* 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.
|
"""Pass the game through the manager.
|
||||||
In charge of calling the final_run method."""
|
In charge of calling the final_run method."""
|
||||||
self.emit("started")
|
|
||||||
self.final_run(game)
|
self.final_run(game)
|
||||||
self.emit("done")
|
callback(self)
|
||||||
|
|
||||||
@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
|
|
||||||
|
|||||||
@@ -55,25 +55,21 @@ class Pipeline(GObject.Object):
|
|||||||
# Separate blocking / async managers
|
# Separate blocking / async managers
|
||||||
managers = self.ready
|
managers = self.ready
|
||||||
blocking = set(filter(lambda manager: manager.blocking, managers))
|
blocking = set(filter(lambda manager: manager.blocking, managers))
|
||||||
parallel = managers - parallel
|
parallel = managers - blocking
|
||||||
|
|
||||||
# Schedule parallel managers, then run the blocking ones
|
# Schedule parallel managers, then run the blocking ones
|
||||||
for manager in (*parallel, *blocking):
|
for manager in (*parallel, *blocking):
|
||||||
manager.run(self.game)
|
self.emit("manager-started", manager)
|
||||||
|
manager.run(self.game, self._manager_callback)
|
||||||
|
|
||||||
|
def _manager_callback(self, manager: Manager) -> None:
|
||||||
|
"""Method called by a manager when it's done"""
|
||||||
|
self.emit("manager-done", manager)
|
||||||
|
|
||||||
@GObject.Signal(name="manager-started", arg_types=(object,))
|
@GObject.Signal(name="manager-started", arg_types=(object,))
|
||||||
def manager_started(self, manager: Manager) -> None:
|
def manager_started(self, manager: Manager) -> None:
|
||||||
"""Signal emitted when a manager is started"""
|
"""Signal emitted when a manager is started"""
|
||||||
pass
|
|
||||||
|
|
||||||
@GObject.Signal(name="manager-done", arg_types=(object,))
|
@GObject.Signal(name="manager-done", arg_types=(object,))
|
||||||
def manager_done(self, manager: Manager) -> None:
|
def manager_done(self, manager: Manager) -> None:
|
||||||
"""Signal emitted when a manager is done"""
|
"""Signal emitted when a manager is done"""
|
||||||
pass
|
|
||||||
|
|
||||||
def on_manager_started(self, manager: Manager) -> None:
|
|
||||||
self.emit("manager-started", manager)
|
|
||||||
|
|
||||||
def on_manager_done(self, manager: Manager) -> None:
|
|
||||||
self.emit("manager-done", manager)
|
|
||||||
self.advance()
|
|
||||||
|
|||||||
Reference in New Issue
Block a user