From 369a2ef2f63a4c3210976f28c7f44c24e3825b35 Mon Sep 17 00:00:00 2001 From: Masahiro Wada Date: Tue, 31 May 2022 12:44:24 +0000 Subject: [PATCH] refactor: Rename types --- alfort/app.py | 6 +++--- alfort/sub.py | 30 +++++++++++++++--------------- examples/simple_counter/main.py | 6 +++--- tests/test_app.py | 6 +++--- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/alfort/app.py b/alfort/app.py index 90fc997..be9f51d 100644 --- a/alfort/app.py +++ b/alfort/app.py @@ -3,7 +3,7 @@ from itertools import zip_longest from typing import Callable, Generic, TypeAlias, TypeVar -from alfort.sub import Subscriber, Subscriptions +from alfort.sub import Context, Subscriptions from alfort.vdom import ( Element, Node, @@ -57,7 +57,7 @@ class Alfort(Generic[S, M, N]): _view: View[S] _update: Update[M, S] _enqueue: Enqueue - _subscriber: Subscriber[S, M] + _subscriber: Context[S, M] @classmethod def _run_effects(cls, dispatch: Dispatch[M], effects: list[Effect[M]]) -> None: @@ -106,7 +106,7 @@ def __init__( self._view = view self._update = update self._enqueue = enqueue - self._subscriber = Subscriber(subscriptions) + self._subscriber = Context(subscriptions) @abstractmethod def create_element( diff --git a/alfort/sub.py b/alfort/sub.py index d7b169c..18cd511 100644 --- a/alfort/sub.py +++ b/alfort/sub.py @@ -4,14 +4,14 @@ State = TypeVar("State") Dispatch: TypeAlias = Callable[[Msg], None] -UnSubscribe: TypeAlias = Callable[[], None] -Subscribe: TypeAlias = Callable[[Dispatch[Msg]], UnSubscribe] -Subscriptions: TypeAlias = Callable[[State], list[Subscribe[Msg]]] +UnSubscription: TypeAlias = Callable[[], None] +Subscription: TypeAlias = Callable[[Dispatch[Msg]], UnSubscription] +Subscriptions: TypeAlias = Callable[[State], list[Subscription[Msg]]] -class Subscriber(Generic[State, Msg]): +class Context(Generic[State, Msg]): _subscriptions: Subscriptions[State, Msg] | None - _unsubscribes: dict[Subscribe[Msg], UnSubscribe] = {} + _unsubscriptions: dict[Subscription[Msg], UnSubscription] = {} def __init__( self, @@ -23,24 +23,24 @@ def update(self, state: State, dispatch: Dispatch[Msg]) -> None: if self._subscriptions is None: return - rem_unscribes = self._unsubscribes - self._unsubscribes = {} + rem_unscribes = self._unsubscriptions + self._unsubscriptions = {} for s in self._subscriptions(state): if us := rem_unscribes.pop(s, None): - self._unsubscribes[s] = us + self._unsubscriptions[s] = us else: - self._unsubscribes[s] = s(dispatch) + self._unsubscriptions[s] = s(dispatch) for us in rem_unscribes.values(): us() -class _Subscription(Generic[Msg]): - def __init__(self, fun: Subscribe[Msg], key: Hashable) -> None: +class SubscriptionWithKey(Generic[Msg]): + def __init__(self, fun: Subscription[Msg], key: Hashable) -> None: self._key = key self._fun = fun - def __call__(self, dispatch: Dispatch[Msg]) -> UnSubscribe: + def __call__(self, dispatch: Dispatch[Msg]) -> UnSubscription: return self._fun(dispatch) def __eq__(self, other: Any) -> bool: @@ -57,9 +57,9 @@ def __hash__(self) -> int: def subscription( key: Any | None = None, -) -> Callable[[Subscribe[Msg]], _Subscription[Msg]]: - def _constructor(f: Callable[[Any], UnSubscribe]) -> _Subscription[Msg]: +) -> Callable[[Subscription[Msg]], SubscriptionWithKey[Msg]]: + def _constructor(f: Subscription[Msg]) -> SubscriptionWithKey[Msg]: _key = key if key is not None else tuple(f.__code__.co_lines()) - return _Subscription[Msg](f, _key) + return SubscriptionWithKey[Msg](f, _key) return _constructor diff --git a/examples/simple_counter/main.py b/examples/simple_counter/main.py index 7fb70ab..fe39770 100644 --- a/examples/simple_counter/main.py +++ b/examples/simple_counter/main.py @@ -3,7 +3,7 @@ from typing import Any, Callable from alfort import Alfort, Dispatch, Effect -from alfort.sub import Subscribe, UnSubscribe, subscription +from alfort.sub import Subscription, UnSubscription, subscription from alfort.vdom import Node, Patch, PatchText, Props, VDom handlers: dict[str, Callable[[], None]] = {} @@ -75,9 +75,9 @@ def update(msg: Msg, state: int) -> tuple[int, list[Effect[Msg]]]: case Msg.Down: return (state - 1, []) - def subscriptions(state: int) -> list[Subscribe[Msg]]: + def subscriptions(state: int) -> list[Subscription[Msg]]: @subscription() - def on_keydown(dispatch: Dispatch[Msg]) -> UnSubscribe: + def on_keydown(dispatch: Dispatch[Msg]) -> UnSubscription: handlers["u"] = lambda: dispatch(Msg.Up) handlers["d"] = lambda: dispatch(Msg.Down) return lambda: handlers.clear() diff --git a/tests/test_app.py b/tests/test_app.py index 78205ec..b73d9d7 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -5,7 +5,7 @@ from alfort import Alfort, Dispatch, Effect from alfort.app import NodeDom, NodeDomElement, NodeDomText -from alfort.sub import Subscribe, UnSubscribe, subscription +from alfort.sub import Subscription, UnSubscription, subscription from alfort.vdom import ( Node, Patch, @@ -322,9 +322,9 @@ def update( countup = None - def subscriptions(state: dict[str, int]) -> list[Subscribe[Msg]]: + def subscriptions(state: dict[str, int]) -> list[Subscription[Msg]]: @subscription() - def on_countup(dispatch: Dispatch[Msg]) -> UnSubscribe: + def on_countup(dispatch: Dispatch[Msg]) -> UnSubscription: nonlocal countup def _countup() -> None: