✨ Added blocking/async managers
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
from abc import abstractmethod
|
||||
from gi.repository import Gio
|
||||
|
||||
from gi.repository import GObject
|
||||
|
||||
from src.game import Game
|
||||
|
||||
|
||||
class Manager:
|
||||
class Manager(GObject.Object):
|
||||
"""Class in charge of handling a post creation action for games.
|
||||
|
||||
* May connect to signals on the game to handle them.
|
||||
@@ -13,26 +14,15 @@ class Manager:
|
||||
"""
|
||||
|
||||
run_after: set[type["Manager"]] = set()
|
||||
|
||||
cancellable: Gio.Cancellable
|
||||
errors: list[Exception]
|
||||
blocking: bool = True
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.cancellable = Gio.Cancellable()
|
||||
self.errors = []
|
||||
|
||||
def cancel_tasks(self):
|
||||
"""Cancel all tasks for this manager"""
|
||||
self.cancellable.cancel()
|
||||
|
||||
def reset_cancellable(self):
|
||||
"""Reset the cancellable for this manager.
|
||||
Alreadyn scheduled Tasks will no longer be cancellable."""
|
||||
self.cancellable = Gio.Cancellable()
|
||||
|
||||
def report_error(self, error: Exception):
|
||||
"""Report an error that happened in of run"""
|
||||
"""Report an error that happened in Manager.run"""
|
||||
self.errors.append(error)
|
||||
|
||||
def collect_errors(self) -> list[Exception]:
|
||||
@@ -42,8 +32,26 @@ class Manager:
|
||||
return errors
|
||||
|
||||
@abstractmethod
|
||||
def run(self, game: Game, cancellable: Gio.Cancellable) -> None:
|
||||
def final_run(self, game: Game) -> None:
|
||||
"""
|
||||
Abstract method overriden by final child classes, called by the run method.
|
||||
* May block its thread
|
||||
* May not raise exceptions, as they will be silently ignored
|
||||
"""
|
||||
|
||||
def run(self, game: Game) -> None:
|
||||
"""Pass the game through the manager.
|
||||
May block its thread.
|
||||
May not raise exceptions, as they will be silently ignored."""
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user