From 8187b6b7e889565b3f7e4e96863fb51abce86c53 Mon Sep 17 00:00:00 2001 From: Sassan Haradji Date: Sat, 4 May 2024 16:32:10 +0400 Subject: [PATCH] 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 --- CHANGELOG.md | 6 ++++++ pyproject.toml | 2 +- redux/autorun.py | 24 +++++++++++++----------- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a37d63f..0cee96b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/pyproject.toml b/pyproject.toml index ed55617..6dacbe4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 "] license = "Apache-2.0" diff --git a/redux/autorun.py b/redux/autorun.py index ef27a7a..2dcee39 100644 --- a/redux/autorun.py +++ b/redux/autorun.py @@ -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) @@ -145,6 +144,8 @@ def _check_and_call( AutorunOriginalReturnType, ], state: State, + *, + call: bool = True, ) -> None: try: selector_result = self._selector(state) @@ -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()