Skip to content

Commit

Permalink
fix: autorun now correctly updates its value when the store is updated
Browse files Browse the repository at this point in the history
feat: add `__repr__` to `Autorun` class
  • Loading branch information
sassanh committed Feb 19, 2024
1 parent 9abb1db commit cdfaccf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
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.10.6"
version = "0.10.7"
description = "Redux implementation for Python"
authors = ["Sassan Haradji <[email protected]>"]
license = "Apache-2.0"
Expand Down
33 changes: 23 additions & 10 deletions redux/autorun.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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[
Expand All @@ -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(
Expand All @@ -126,7 +139,7 @@ def value(
AutorunOriginalReturnType,
],
) -> AutorunOriginalReturnType:
return cast(AutorunOriginalReturnType, self._last_value)
return cast(AutorunOriginalReturnType, self._latest_value)

def subscribe(
self: Autorun[
Expand All @@ -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):
Expand Down

0 comments on commit cdfaccf

Please sign in to comment.