Skip to content

Commit cdfaccf

Browse files
committed
fix: autorun now correctly updates its value when the store is updated
feat: add `__repr__` to `Autorun` class
1 parent 9abb1db commit cdfaccf

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

CHANGELOG.md

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

3+
## Version 0.10.7
4+
5+
- fix: autorun now correctly updates its value when the store is updated
6+
- feat: add `__repr__` to `Autorun` class
7+
38
## Version 0.10.6
49

510
- chore: improve github workflow caching

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.10.6"
3+
version = "0.10.7"
44
description = "Redux implementation for Python"
55
authors = ["Sassan Haradji <[email protected]>"]
66
license = "Apache-2.0"

redux/autorun.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,20 @@ def __init__( # noqa: PLR0913
4444
self._selector = selector
4545
self._comparator = comparator
4646
self._func = func
47-
self.options = options
47+
self._options = options
4848

4949
self._last_selector_result: SelectorOutput | None = None
5050
self._last_comparator_result: ComparatorOutput = cast(
5151
ComparatorOutput,
5252
object(),
5353
)
54-
self._last_value: AutorunOriginalReturnType | None = options.default_value
54+
self._latest_value: AutorunOriginalReturnType | None = options.default_value
5555
self._subscriptions: set[
5656
Callable[[AutorunOriginalReturnType], Any]
5757
| weakref.ref[Callable[[AutorunOriginalReturnType], Any]]
5858
] = set()
5959

60-
if self.options.initial_run and store._state is not None: # noqa: SLF001
60+
if self._options.initial_run and store._state is not None: # noqa: SLF001
6161
self.check_and_call(store._state) # noqa: SLF001
6262

6363
store.subscribe(self.check_and_call)
@@ -76,12 +76,12 @@ def check_and_call(self: Autorun, state: State) -> None:
7676
self._last_selector_result = selector_result
7777
self._last_comparator_result = comparator_result
7878
if len(signature(self._func).parameters) == 1:
79-
last_value = cast(
79+
self._latest_value = cast(
8080
Callable[[SelectorOutput], AutorunOriginalReturnType],
8181
self._func,
8282
)(selector_result)
8383
else:
84-
last_value = cast(
84+
self._latest_value = cast(
8585
Callable[
8686
[SelectorOutput, SelectorOutput | None],
8787
AutorunOriginalReturnType,
@@ -99,7 +99,7 @@ def check_and_call(self: Autorun, state: State) -> None:
9999
continue
100100
else:
101101
subscriber = subscriber_
102-
subscriber(last_value)
102+
subscriber(self._latest_value)
103103

104104
def __call__(
105105
self: Autorun[
@@ -113,7 +113,20 @@ def __call__(
113113
) -> AutorunOriginalReturnType:
114114
if self._store._state is not None: # noqa: SLF001
115115
self.check_and_call(self._store._state) # noqa: SLF001
116-
return cast(AutorunOriginalReturnType, self._last_value)
116+
return cast(AutorunOriginalReturnType, self._latest_value)
117+
118+
def __repr__(
119+
self: Autorun[
120+
State,
121+
Action,
122+
Event,
123+
SelectorOutput,
124+
ComparatorOutput,
125+
AutorunOriginalReturnType,
126+
],
127+
) -> str:
128+
return f"""{super().__repr__()}(func: {self._func}, last_value: {
129+
self._latest_value})"""
117130

118131
@property
119132
def value(
@@ -126,7 +139,7 @@ def value(
126139
AutorunOriginalReturnType,
127140
],
128141
) -> AutorunOriginalReturnType:
129-
return cast(AutorunOriginalReturnType, self._last_value)
142+
return cast(AutorunOriginalReturnType, self._latest_value)
130143

131144
def subscribe(
132145
self: Autorun[
@@ -143,9 +156,9 @@ def subscribe(
143156
keep_ref: bool | None = None,
144157
) -> Callable[[], None]:
145158
if immediate_run is None:
146-
immediate_run = self.options.subscribers_immediate_run
159+
immediate_run = self._options.subscribers_immediate_run
147160
if keep_ref is None:
148-
keep_ref = self.options.subscribers_keep_ref
161+
keep_ref = self._options.subscribers_keep_ref
149162
if keep_ref:
150163
callback_ref = callback
151164
elif isinstance(callback, MethodType):

0 commit comments

Comments
 (0)