Skip to content

Commit

Permalink
Merge pull request #25 from ar90n/feature/renable-types-about-subscri…
Browse files Browse the repository at this point in the history
…ptions

refactor: Rename types
  • Loading branch information
ar90n authored May 31, 2022
2 parents 585fb37 + 369a2ef commit 1c7d858
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
6 changes: 3 additions & 3 deletions alfort/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down
30 changes: 15 additions & 15 deletions alfort/sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand All @@ -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
6 changes: 3 additions & 3 deletions examples/simple_counter/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]] = {}
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 1c7d858

Please sign in to comment.