From eb9a8623ed4b3e587b96c2f223c07918616b4fba Mon Sep 17 00:00:00 2001 From: break27 Date: Mon, 27 Apr 2026 16:03:44 +0800 Subject: [PATCH] update: added 'actionflow' module --- pyproject.toml | 2 +- src/common/actionflow.py | 67 +++++++++++++++++++++++++++++++++++ src/common/jsonrpc2/client.js | 2 +- 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 src/common/actionflow.py diff --git a/pyproject.toml b/pyproject.toml index 5c77c5e..d2152b2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta" [project] name = "common" description = "Reusable code stubs" -version = "0.1.13" +version = "0.1.14" requires-python = ">=3.13" authors = [ { name="BreakerBear", email="breakerbear@autistic.men" }, diff --git a/src/common/actionflow.py b/src/common/actionflow.py new file mode 100644 index 0000000..1517077 --- /dev/null +++ b/src/common/actionflow.py @@ -0,0 +1,67 @@ +from abc import abstractmethod, ABCMeta + +class Action(BaseException, metaclass=ABCMeta): + def __init__(self, *args): + super().__init__(*args) + + @classmethod + @abstractmethod + def prepare(cls) -> bool: + pass + + @classmethod + def perform(cls): + raise cls() + +class ActionFlow: + def __init__(self): + self.indices: dict[str, int] = {} + self.actions: list[type[Action]] = [] + self.on: list[bool] = [] + + @classmethod + def normalize(cls, kind): + return str.upper(kind.__name__) + + def do(self, action: str): + index = self.indices.get(action) + assert index is not None + + if self.on[index] is not None: + value = self.actions[index].prepare() + self.on[index] = value + + def react(self): + for index in self.indices.values(): + if self.on[index]: + self.actions[index].perform() + + def index(self, kind: type[Action]) -> int: + name = self.normalize(kind) + index = self.indices.get(name) + assert index is not None + return index + + def allow(self, kind, value: bool): + index = self.index(kind) + self.on[index] = False if value else None + + def append(self, kind): + name = self.normalize(kind) + index = len(self.indices) + + self.indices[name] = index + self.actions.insert(index, kind) + self.on.insert(index, None) + + def remove(self, kind): + name = self.normalize(kind) + index = self.index(kind) + + self.indices.pop(name) + self.actions.pop(index) + self.on.pop(index) + + def capabilities(self) -> dict[str, bool]: + items = self.indices.items() + return { k: self.on[v] == False for k, v in items } diff --git a/src/common/jsonrpc2/client.js b/src/common/jsonrpc2/client.js index 3866414..3b55bff 100644 --- a/src/common/jsonrpc2/client.js +++ b/src/common/jsonrpc2/client.js @@ -13,7 +13,7 @@ CONNECTION.addEventListener('message', (e) => { }); while (CONNECTION.readyState !== CONNECTION.OPEN) { - await new Promise(resolve => setTimeout(resolve, 1000)); + await new Promise(resolve => setTimeout(resolve, 200)); } const LogRecord = {