Skip to content

Commit bd22df7

Browse files
committed
feat: autorun decorator accepts a default value for when store is not initialized
feat: `autorun` decorator takes its options in its keyword arguments
1 parent 44b1317 commit bd22df7

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Changelog
22

3-
## Version 0.9.16
3+
## Version 0.9.18
4+
5+
- feat: `autorun` decorator accepts a default value for when store is not initialized
6+
- feat: `autorun` decorator takes its options in its keyword arguments
7+
8+
## Version 0.9.17
49

510
- refactor: make `dispatch` accept a `with_state(store)` function as parameter, if
611
provided it will dispatch return value of this function

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.16"
3+
version = "0.9.18"
44
description = "Redux implementation for Python"
55
authors = ["Sassan Haradji <sassanh@gmail.com>"]
66
license = "Apache-2.0"

redux/basic_types.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,17 @@ def __call__(
9191
self: AutorunType,
9292
selector: Callable[[State], SelectorOutput],
9393
comparator: Callable[[State], Any] | None = None,
94-
) -> AutorunDecorator[State, SelectorOutput]:
94+
*,
95+
default_value: AutorunOriginalReturnType | None = None,
96+
initial_run: bool = True,
97+
) -> AutorunDecorator[State, SelectorOutput, AutorunOriginalReturnType]:
9598
...
9699

97100

98-
class AutorunDecorator(Protocol, Generic[State, SelectorOutput]):
101+
class AutorunDecorator(
102+
Protocol,
103+
Generic[State, SelectorOutput, AutorunOriginalReturnType],
104+
):
99105
def __call__(
100106
self: AutorunDecorator,
101107
func: Callable[[SelectorOutput], AutorunOriginalReturnType]
@@ -136,7 +142,8 @@ class Dispatch(Protocol, Generic[State, Action, Event]):
136142
def __call__(
137143
self: Dispatch,
138144
*items: Action | Event | list[Action | Event],
139-
with_state: Callable[[State | None], Action | Event | list[Action | Event]],
145+
with_state: Callable[[State | None], Action | Event | list[Action | Event]]
146+
| None = None,
140147
) -> None:
141148
...
142149

redux/main.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ def handle_finish_event(_event: Event) -> None:
163163
def autorun(
164164
selector: Callable[[State], SelectorOutput],
165165
comparator: Callable[[State], ComparatorOutput] | None = None,
166-
) -> AutorunDecorator[State, SelectorOutput]:
166+
*,
167+
default_value: AutorunOriginalReturnType | None = None,
168+
initial_run: bool = _options.autorun_initial_run,
169+
) -> AutorunDecorator[State, SelectorOutput, AutorunOriginalReturnType]:
167170
nonlocal state
168171

169172
def decorator(
@@ -172,7 +175,7 @@ def decorator(
172175
) -> AutorunReturnType[AutorunOriginalReturnType]:
173176
last_selector_result: SelectorOutput | None = None
174177
last_comparator_result: ComparatorOutput = cast(ComparatorOutput, object())
175-
last_value: AutorunOriginalReturnType | None = None
178+
last_value: AutorunOriginalReturnType | None = default_value
176179
subscriptions: list[Callable[[AutorunOriginalReturnType], Any]] = []
177180

178181
def check_and_call(state: State) -> None:
@@ -212,7 +215,7 @@ def check_and_call(state: State) -> None:
212215
for subscriber in subscriptions:
213216
subscriber(last_value)
214217

215-
if _options.autorun_initial_run and state is not None:
218+
if initial_run and state is not None:
216219
check_and_call(state)
217220

218221
subscribe(check_and_call)

0 commit comments

Comments
 (0)