|
1 | 1 | # ruff: noqa: A003, D100, D101, D102, D103, D104, D105, D107 |
2 | 2 | from __future__ import annotations |
3 | 3 |
|
4 | | -from typing import Any, Callable, Generic, Sequence, TypeGuard |
| 4 | +from typing import Any, Callable, Generic, Protocol, Sequence, TypeAlias, TypeGuard |
5 | 5 |
|
6 | 6 | from immutable import Immutable |
7 | 7 | from typing_extensions import TypeVar |
@@ -41,11 +41,6 @@ class CompleteReducerResult(Immutable, Generic[State, Action, Event]): |
41 | 41 |
|
42 | 42 | AutorunOriginalReturnType = TypeVar('AutorunOriginalReturnType', infer_variance=True) |
43 | 43 |
|
44 | | -EventSubscriber = Callable[ |
45 | | - [Event, Callable[[Event], Any], EventSubscriptionOptions | None], |
46 | | - Callable[[], None], |
47 | | -] |
48 | | - |
49 | 44 |
|
50 | 45 | class InitializationActionError(Exception): |
51 | 46 | def __init__(self: InitializationActionError, action: BaseAction) -> None: |
@@ -75,3 +70,79 @@ def is_reducer_result( |
75 | 70 |
|
76 | 71 | def is_state(result: ReducerResult[State, Action, Event]) -> TypeGuard[State]: |
77 | 72 | return not isinstance(result, CompleteReducerResult) |
| 73 | + |
| 74 | + |
| 75 | +class Scheduler(Protocol): |
| 76 | + def __call__(self: Scheduler, callback: Callable, *, interval: bool) -> None: |
| 77 | + ... |
| 78 | + |
| 79 | + |
| 80 | +class CreateStoreOptions(Immutable): |
| 81 | + auto_init: bool = False |
| 82 | + threads: int = 5 |
| 83 | + autorun_initial_run: bool = True |
| 84 | + scheduler: Scheduler | None = None |
| 85 | + action_middleware: Callable[[BaseAction], Any] | None = None |
| 86 | + event_middleware: Callable[[BaseEvent], Any] | None = None |
| 87 | + |
| 88 | + |
| 89 | +class AutorunType(Protocol, Generic[State]): |
| 90 | + def __call__( |
| 91 | + self: AutorunType, |
| 92 | + selector: Callable[[State], SelectorOutput], |
| 93 | + comparator: Callable[[State], Any] | None = None, |
| 94 | + ) -> AutorunDecorator[State, SelectorOutput]: |
| 95 | + ... |
| 96 | + |
| 97 | + |
| 98 | +class AutorunDecorator(Protocol, Generic[State, SelectorOutput]): |
| 99 | + def __call__( |
| 100 | + self: AutorunDecorator, |
| 101 | + func: Callable[[SelectorOutput], AutorunOriginalReturnType] |
| 102 | + | Callable[[SelectorOutput, SelectorOutput], AutorunOriginalReturnType], |
| 103 | + ) -> AutorunReturnType[AutorunOriginalReturnType]: |
| 104 | + ... |
| 105 | + |
| 106 | + |
| 107 | +class AutorunReturnType(Protocol, Generic[AutorunOriginalReturnType]): |
| 108 | + def __call__(self: AutorunReturnType) -> AutorunOriginalReturnType: |
| 109 | + ... |
| 110 | + |
| 111 | + @property |
| 112 | + def value(self: AutorunReturnType) -> AutorunOriginalReturnType: |
| 113 | + ... |
| 114 | + |
| 115 | + def subscribe( |
| 116 | + self: AutorunReturnType, |
| 117 | + callback: Callable[[AutorunOriginalReturnType], Any], |
| 118 | + ) -> Callable[[], None]: |
| 119 | + ... |
| 120 | + |
| 121 | + |
| 122 | +class EventSubscriber(Protocol): |
| 123 | + def __call__( |
| 124 | + self: EventSubscriber, |
| 125 | + event_type: type[Event], |
| 126 | + handler: EventHandler[Event], |
| 127 | + options: EventSubscriptionOptions | None = None, |
| 128 | + ) -> Callable[[], None]: |
| 129 | + ... |
| 130 | + |
| 131 | + |
| 132 | +DispatchParameters: TypeAlias = Action | Event | list[Action | Event] |
| 133 | + |
| 134 | + |
| 135 | +class Dispatch(Protocol, Generic[State, Action, Event]): |
| 136 | + def __call__( |
| 137 | + self: Dispatch, |
| 138 | + *items: Action | Event | list[Action | Event], |
| 139 | + with_state: Callable[[State | None], Action | Event | list[Action | Event]], |
| 140 | + ) -> None: |
| 141 | + ... |
| 142 | + |
| 143 | + |
| 144 | +class InitializeStateReturnValue(Immutable, Generic[State, Action, Event]): |
| 145 | + dispatch: Dispatch[State, Action, Event] |
| 146 | + subscribe: Callable[[Callable[[State], Any]], Callable[[], None]] |
| 147 | + subscribe_event: EventSubscriber |
| 148 | + autorun: AutorunType[State] |
0 commit comments