Skip to content

Commit 939087f

Browse files
committedMar 7, 2025
feat: add with_selector, as a decorator to run functions with the result of a selector.
1 parent 1b1e3b7 commit 939087f

File tree

5 files changed

+254
-114
lines changed

5 files changed

+254
-114
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Upcoming
44

55
- 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+
- feat: add `with_selector`, as a decorator to run functions with the result of a selector.
67

78
## Version 0.19.1
89

‎redux/autorun.py

+39-40
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717

1818
from redux.basic_types import (
1919
Action,
20-
AutorunArgs,
20+
Args,
2121
AutorunOptions,
22-
AutorunOriginalReturnType,
2322
ComparatorOutput,
2423
Event,
24+
ReturnType,
2525
SelectorOutput,
2626
State,
2727
)
@@ -58,8 +58,8 @@ class Autorun(
5858
Event,
5959
SelectorOutput,
6060
ComparatorOutput,
61-
AutorunOriginalReturnType,
62-
AutorunArgs,
61+
ReturnType,
62+
Args,
6363
],
6464
):
6565
def __init__(
@@ -69,10 +69,10 @@ def __init__(
6969
selector: Callable[[State], SelectorOutput],
7070
comparator: Callable[[State], Any] | None,
7171
func: Callable[
72-
Concatenate[SelectorOutput, AutorunArgs],
73-
AutorunOriginalReturnType,
72+
Concatenate[SelectorOutput, Args],
73+
ReturnType,
7474
],
75-
options: AutorunOptions[AutorunOriginalReturnType],
75+
options: AutorunOptions[ReturnType],
7676
) -> None:
7777
self.__name__ = func.__name__
7878
self._store = store
@@ -109,10 +109,9 @@ def __init__(
109109
self._latest_value = Future()
110110
self._latest_value.set_result(options.default_value)
111111
else:
112-
self._latest_value: AutorunOriginalReturnType = options.default_value
112+
self._latest_value: ReturnType = options.default_value
113113
self._subscriptions: set[
114-
Callable[[AutorunOriginalReturnType], Any]
115-
| weakref.ref[Callable[[AutorunOriginalReturnType], Any]]
114+
Callable[[ReturnType], Any] | weakref.ref[Callable[[ReturnType], Any]]
116115
] = set()
117116

118117
if self._check(store._state) and self._options.initial_call: # noqa: SLF001
@@ -137,8 +136,8 @@ def inform_subscribers(
137136
Event,
138137
SelectorOutput,
139138
ComparatorOutput,
140-
AutorunOriginalReturnType,
141-
AutorunArgs,
139+
ReturnType,
140+
Args,
142141
],
143142
) -> None:
144143
for subscriber_ in self._subscriptions.copy():
@@ -158,8 +157,8 @@ def _task_callback(
158157
Event,
159158
SelectorOutput,
160159
ComparatorOutput,
161-
AutorunOriginalReturnType,
162-
AutorunArgs,
160+
ReturnType,
161+
Args,
163162
],
164163
task: Task,
165164
*,
@@ -179,8 +178,8 @@ def _check(
179178
Event,
180179
SelectorOutput,
181180
ComparatorOutput,
182-
AutorunOriginalReturnType,
183-
AutorunArgs,
181+
ReturnType,
182+
Args,
184183
],
185184
state: State | None,
186185
) -> bool:
@@ -211,16 +210,16 @@ def _call(
211210
Event,
212211
SelectorOutput,
213212
ComparatorOutput,
214-
AutorunOriginalReturnType,
215-
AutorunArgs,
213+
ReturnType,
214+
Args,
216215
],
217-
*args: AutorunArgs.args,
218-
**kwargs: AutorunArgs.kwargs,
216+
*args: Args.args,
217+
**kwargs: Args.kwargs,
219218
) -> None:
220219
self._should_be_called = False
221220
func = self._func() if isinstance(self._func, weakref.ref) else self._func
222221
if func and self._last_selector_result is not None:
223-
value: AutorunOriginalReturnType = func(
222+
value: ReturnType = func(
224223
self._last_selector_result,
225224
*args,
226225
**kwargs,
@@ -229,7 +228,7 @@ def _call(
229228
if iscoroutine(value) and create_task:
230229
if self._options.auto_await:
231230
future = Future()
232-
self._latest_value = cast(AutorunOriginalReturnType, future)
231+
self._latest_value = cast(ReturnType, future)
233232
create_task(
234233
value,
235234
callback=functools.partial(
@@ -245,7 +244,7 @@ def _call(
245244
):
246245
self._latest_value.close()
247246
self._latest_value = cast(
248-
AutorunOriginalReturnType,
247+
ReturnType,
249248
AwaitableWrapper(value),
250249
)
251250
else:
@@ -259,17 +258,17 @@ def __call__(
259258
Event,
260259
SelectorOutput,
261260
ComparatorOutput,
262-
AutorunOriginalReturnType,
263-
AutorunArgs,
261+
ReturnType,
262+
Args,
264263
],
265-
*args: AutorunArgs.args,
266-
**kwargs: AutorunArgs.kwargs,
267-
) -> AutorunOriginalReturnType:
264+
*args: Args.args,
265+
**kwargs: Args.kwargs,
266+
) -> ReturnType:
268267
state = self._store._state # noqa: SLF001
269268
self._check(state)
270269
if self._should_be_called or args or kwargs or not self._options.memoization:
271270
self._call(*args, **kwargs)
272-
return cast(AutorunOriginalReturnType, self._latest_value)
271+
return cast(ReturnType, self._latest_value)
273272

274273
def __repr__(
275274
self: Autorun[
@@ -278,8 +277,8 @@ def __repr__(
278277
Event,
279278
SelectorOutput,
280279
ComparatorOutput,
281-
AutorunOriginalReturnType,
282-
AutorunArgs,
280+
ReturnType,
281+
Args,
283282
],
284283
) -> str:
285284
return (
@@ -295,11 +294,11 @@ def value(
295294
Event,
296295
SelectorOutput,
297296
ComparatorOutput,
298-
AutorunOriginalReturnType,
299-
AutorunArgs,
297+
ReturnType,
298+
Args,
300299
],
301-
) -> AutorunOriginalReturnType:
302-
return cast(AutorunOriginalReturnType, self._latest_value)
300+
) -> ReturnType:
301+
return cast(ReturnType, self._latest_value)
303302

304303
def subscribe(
305304
self: Autorun[
@@ -308,10 +307,10 @@ def subscribe(
308307
Event,
309308
SelectorOutput,
310309
ComparatorOutput,
311-
AutorunOriginalReturnType,
312-
AutorunArgs,
310+
ReturnType,
311+
Args,
313312
],
314-
callback: Callable[[AutorunOriginalReturnType], Any],
313+
callback: Callable[[ReturnType], Any],
315314
*,
316315
initial_run: bool | None = None,
317316
keep_ref: bool | None = None,
@@ -344,8 +343,8 @@ def __signature__(
344343
Event,
345344
SelectorOutput,
346345
ComparatorOutput,
347-
AutorunOriginalReturnType,
348-
AutorunArgs,
346+
ReturnType,
347+
Args,
349348
],
350349
) -> inspect.Signature:
351350
return self._signature

0 commit comments

Comments
 (0)
Failed to load comments.