Skip to content

Commit 1b1e3b7

Browse files
committed
feat: add memoization option to autorun, default is True, compatible with old behavior, if set to False, calling the function explicitly will always run it regardless of the selector's value
1 parent 1bc289e commit 1b1e3b7

File tree

5 files changed

+33
-1
lines changed

5 files changed

+33
-1
lines changed

CHANGELOG.md

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

3+
## Upcoming
4+
5+
- feat: add `memoization` option to `autorun`, default is `True`, compatible with old behavior, if set to `False`, calling the function explicitly will always run it regardless of the selector's value
6+
37
## Version 0.19.1
48

59
- refactor: provide correct signature for the autorun instance based on the function it decorates

redux/autorun.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ def __call__(
266266
**kwargs: AutorunArgs.kwargs,
267267
) -> AutorunOriginalReturnType:
268268
state = self._store._state # noqa: SLF001
269-
if self._check(state) or self._should_be_called or args or kwargs:
269+
self._check(state)
270+
if self._should_be_called or args or kwargs or not self._options.memoization:
270271
self._call(*args, **kwargs)
271272
return cast(AutorunOriginalReturnType, self._latest_value)
272273

redux/basic_types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ class AutorunOptions(Immutable, Generic[AutorunOriginalReturnType]):
134134
auto_await: bool = True
135135
initial_call: bool = True
136136
reactive: bool = True
137+
memoization: bool = True
137138
keep_ref: bool = True
138139
subscribers_initial_run: bool = True
139140
subscribers_keep_ref: bool = True
@@ -207,6 +208,7 @@ def __call__(
207208

208209
class ViewOptions(Immutable, Generic[ViewOriginalReturnType]):
209210
default_value: ViewOriginalReturnType | None = None
211+
memoization: bool = True
210212
keep_ref: bool = True
211213
subscribers_initial_run: bool = True
212214
subscribers_keep_ref: bool = True

redux/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ def decorator(
408408
auto_await=True,
409409
initial_call=False,
410410
reactive=False,
411+
memoization=_options.memoization,
411412
keep_ref=_options.keep_ref,
412413
subscribers_initial_run=_options.subscribers_initial_run,
413414
subscribers_keep_ref=_options.subscribers_keep_ref,

tests/test_autorun.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,29 @@ def render(_: int) -> None: ...
282282
]
283283

284284

285+
def test_task_mode_without_arguments(
286+
store: StoreType,
287+
) -> None:
288+
@store.autorun(
289+
lambda state: state.value,
290+
options=AutorunOptions(
291+
reactive=False,
292+
initial_call=False,
293+
memoization=False,
294+
),
295+
)
296+
def act(value: int) -> int:
297+
assert value == 4, (
298+
'This is expected to be called only after the last action is dispatched'
299+
)
300+
return value
301+
302+
def check() -> None:
303+
assert act() == 4
304+
305+
store.subscribe_event(FinishEvent, check)
306+
307+
285308
def test_view_mode_with_arguments_autorun(
286309
store: StoreType,
287310
) -> None:
@@ -290,6 +313,7 @@ def test_view_mode_with_arguments_autorun(
290313
options=AutorunOptions(
291314
reactive=False,
292315
initial_call=False,
316+
memoization=True,
293317
default_value=0,
294318
),
295319
)

0 commit comments

Comments
 (0)