Skip to content

Commit 8187b6b

Browse files
committed
refactor(autorun)!: setting initial_run option of autorun to False used to make the autorun simply not call the function on initialization, now it makes sure the function is not called until the selector's value actually changes
1 parent 286dbba commit 8187b6b

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

CHANGELOG.md

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

3+
## Version 0.15.0
4+
5+
- refactor(autorun)!: setting `initial_run` option of autorun to `False` used to
6+
make the autorun simply not call the function on initialization, now it makes
7+
sure the function is not called until the selector's value actually changes
8+
39
## Version 0.14.5
410

511
- test(middleware): add middleware tests

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

redux/autorun.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ def __init__( # noqa: PLR0913
6565
| weakref.ref[Callable[[AutorunOriginalReturnType], Any]]
6666
] = set()
6767

68-
if self._options.initial_run and store._state is not None: # noqa: SLF001
69-
self._check_and_call(store._state) # noqa: SLF001
68+
self._check_and_call(store._state, call=self._options.initial_run) # noqa: SLF001
7069

7170
self.unsubscribe = store.subscribe(self._check_and_call)
7271

@@ -145,6 +144,8 @@ def _check_and_call(
145144
AutorunOriginalReturnType,
146145
],
147146
state: State,
147+
*,
148+
call: bool = True,
148149
) -> None:
149150
try:
150151
selector_result = self._selector(state)
@@ -163,15 +164,16 @@ def _check_and_call(
163164
self._last_comparator_result = comparator_result
164165
func = self._func() if isinstance(self._func, weakref.ref) else self._func
165166
if func:
166-
self._latest_value = self.call_func(
167-
selector_result,
168-
previous_result,
169-
func,
170-
)
171-
create_task = self._store._create_task # noqa: SLF001
172-
if iscoroutine(self._latest_value) and create_task:
173-
create_task(self._latest_value, callback=self._task_callback)
174-
self.inform_subscribers()
167+
if call:
168+
self._latest_value = self.call_func(
169+
selector_result,
170+
previous_result,
171+
func,
172+
)
173+
create_task = self._store._create_task # noqa: SLF001
174+
if iscoroutine(self._latest_value) and create_task:
175+
create_task(self._latest_value, callback=self._task_callback)
176+
self.inform_subscribers()
175177
else:
176178
self.unsubscribe()
177179

0 commit comments

Comments
 (0)