Skip to content

Commit 246d48d

Browse files
committed
feat: allow subscribe_event callback parameter take zero arguments
1 parent 05a1504 commit 246d48d

File tree

4 files changed

+10
-4
lines changed

4 files changed

+10
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Version 0.9.14
4+
5+
- feat: allow `subscribe_event` callback parameter take zero arguments
6+
37
## Version 0.9.13
48

59
- feat: make `subscribe` method of `autorun`'s return value, call its callback with

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "python-redux"
3-
version = "0.9.13"
3+
version = "0.9.14"
44
description = "Redux implementation for Python"
55
authors = ["Sassan Haradji <sassanh@gmail.com>"]
66
license = "Apache-2.0"

redux/basic_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class EventSubscriptionOptions(Immutable):
2727
SelectorOutput = TypeVar('SelectorOutput', infer_variance=True)
2828
ComparatorOutput = TypeVar('ComparatorOutput', infer_variance=True)
2929
Comparator = Callable[[State], ComparatorOutput]
30-
EventHandler = Callable[[Event], Any]
30+
EventHandler = Callable[[Event], Any] | Callable[[], Any]
3131

3232

3333
class CompleteReducerResult(Immutable, Generic[State, Action, Event]):

redux/main.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class EventSubscriber(Protocol):
8181
def __call__(
8282
self: EventSubscriber,
8383
event_type: type[Event],
84-
handler: Callable[[Event], Any],
84+
handler: EventHandler[Event],
8585
options: EventSubscriptionOptions | None = None,
8686
) -> Callable[[], None]: # pyright: ignore[reportGeneralTypeIssues]
8787
...
@@ -166,8 +166,10 @@ def run() -> None:
166166
for event_handler, options in event_handlers[type(event)].copy():
167167
if options.run_async:
168168
event_handlers_queue.put((event_handler, event))
169+
elif len(signature(event_handler).parameters) == 1:
170+
cast(Callable[[Event], Any], event_handler)(event)
169171
else:
170-
event_handler(event)
172+
cast(Callable[[], Any], event_handler)()
171173

172174
def dispatch(*parameters: Action | Event | list[Action | Event]) -> None:
173175
items = [

0 commit comments

Comments
 (0)