Skip to content

Commit

Permalink
refactor(autorun)!: setting initial_run option of autorun to `False…
Browse files Browse the repository at this point in the history
…` 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
  • Loading branch information
sassanh committed May 4, 2024
1 parent 286dbba commit 8187b6b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Version 0.15.0

- 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

## Version 0.14.5

- test(middleware): add middleware tests
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "python-redux"
version = "0.14.5"
version = "0.15.0"
description = "Redux implementation for Python"
authors = ["Sassan Haradji <sassanh@gmail.com>"]
license = "Apache-2.0"
Expand Down
24 changes: 13 additions & 11 deletions redux/autorun.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ def __init__( # noqa: PLR0913
| weakref.ref[Callable[[AutorunOriginalReturnType], Any]]
] = set()

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

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

Expand Down Expand Up @@ -145,6 +144,8 @@ def _check_and_call(
AutorunOriginalReturnType,
],
state: State,
*,
call: bool = True,
) -> None:
try:
selector_result = self._selector(state)
Expand All @@ -163,15 +164,16 @@ def _check_and_call(
self._last_comparator_result = comparator_result
func = self._func() if isinstance(self._func, weakref.ref) else self._func
if func:
self._latest_value = self.call_func(
selector_result,
previous_result,
func,
)
create_task = self._store._create_task # noqa: SLF001
if iscoroutine(self._latest_value) and create_task:
create_task(self._latest_value, callback=self._task_callback)
self.inform_subscribers()
if call:
self._latest_value = self.call_func(
selector_result,
previous_result,
func,
)
create_task = self._store._create_task # noqa: SLF001
if iscoroutine(self._latest_value) and create_task:
create_task(self._latest_value, callback=self._task_callback)
self.inform_subscribers()
else:
self.unsubscribe()

Expand Down

0 comments on commit 8187b6b

Please sign in to comment.