update: added 'actionflow' module

This commit is contained in:
2026-04-27 16:03:44 +08:00
parent 21b69a2c70
commit eb9a8623ed
3 changed files with 69 additions and 2 deletions

View File

@@ -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" },

67
src/common/actionflow.py Normal file
View File

@@ -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 }

View File

@@ -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 = {