From cdfaccf235482f4fef391e43ff704306ea638760 Mon Sep 17 00:00:00 2001 From: Sassan Haradji Date: Tue, 20 Feb 2024 00:25:10 +0400 Subject: [PATCH] fix: autorun now correctly updates its value when the store is updated feat: add `__repr__` to `Autorun` class --- CHANGELOG.md | 5 +++++ pyproject.toml | 2 +- redux/autorun.py | 33 +++++++++++++++++++++++---------- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ace6ba7..2fc790c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## Version 0.10.7 + +- fix: autorun now correctly updates its value when the store is updated +- feat: add `__repr__` to `Autorun` class + ## Version 0.10.6 - chore: improve github workflow caching diff --git a/pyproject.toml b/pyproject.toml index 0e2eb06..3e6ef73 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "python-redux" -version = "0.10.6" +version = "0.10.7" description = "Redux implementation for Python" authors = ["Sassan Haradji "] license = "Apache-2.0" diff --git a/redux/autorun.py b/redux/autorun.py index dadeadd..e198566 100644 --- a/redux/autorun.py +++ b/redux/autorun.py @@ -44,20 +44,20 @@ def __init__( # noqa: PLR0913 self._selector = selector self._comparator = comparator self._func = func - self.options = options + self._options = options self._last_selector_result: SelectorOutput | None = None self._last_comparator_result: ComparatorOutput = cast( ComparatorOutput, object(), ) - self._last_value: AutorunOriginalReturnType | None = options.default_value + self._latest_value: AutorunOriginalReturnType | None = options.default_value self._subscriptions: set[ Callable[[AutorunOriginalReturnType], Any] | weakref.ref[Callable[[AutorunOriginalReturnType], Any]] ] = set() - if self.options.initial_run and store._state is not None: # noqa: SLF001 + if self._options.initial_run and store._state is not None: # noqa: SLF001 self.check_and_call(store._state) # noqa: SLF001 store.subscribe(self.check_and_call) @@ -76,12 +76,12 @@ def check_and_call(self: Autorun, state: State) -> None: self._last_selector_result = selector_result self._last_comparator_result = comparator_result if len(signature(self._func).parameters) == 1: - last_value = cast( + self._latest_value = cast( Callable[[SelectorOutput], AutorunOriginalReturnType], self._func, )(selector_result) else: - last_value = cast( + self._latest_value = cast( Callable[ [SelectorOutput, SelectorOutput | None], AutorunOriginalReturnType, @@ -99,7 +99,7 @@ def check_and_call(self: Autorun, state: State) -> None: continue else: subscriber = subscriber_ - subscriber(last_value) + subscriber(self._latest_value) def __call__( self: Autorun[ @@ -113,7 +113,20 @@ def __call__( ) -> AutorunOriginalReturnType: if self._store._state is not None: # noqa: SLF001 self.check_and_call(self._store._state) # noqa: SLF001 - return cast(AutorunOriginalReturnType, self._last_value) + return cast(AutorunOriginalReturnType, self._latest_value) + + def __repr__( + self: Autorun[ + State, + Action, + Event, + SelectorOutput, + ComparatorOutput, + AutorunOriginalReturnType, + ], + ) -> str: + return f"""{super().__repr__()}(func: {self._func}, last_value: { + self._latest_value})""" @property def value( @@ -126,7 +139,7 @@ def value( AutorunOriginalReturnType, ], ) -> AutorunOriginalReturnType: - return cast(AutorunOriginalReturnType, self._last_value) + return cast(AutorunOriginalReturnType, self._latest_value) def subscribe( self: Autorun[ @@ -143,9 +156,9 @@ def subscribe( keep_ref: bool | None = None, ) -> Callable[[], None]: if immediate_run is None: - immediate_run = self.options.subscribers_immediate_run + immediate_run = self._options.subscribers_immediate_run if keep_ref is None: - keep_ref = self.options.subscribers_keep_ref + keep_ref = self._options.subscribers_keep_ref if keep_ref: callback_ref = callback elif isinstance(callback, MethodType):