Skip to content

Commit 0877b64

Browse files
committed
refactor: in _wait_for_store_to_finish, instead of waiting with asyncio.sleep, runs the store event loop when conditions are not satisfied
1 parent 55f8032 commit 0877b64

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Upcoming
44

55
- test: make sure pytest exits completely after running async tests
6+
- refactor: in `_wait_for_store_to_finish`, instead of waiting with `asyncio.sleep`, run the store event loop when conditions are not satisfied
67

78
## Version 0.21.1
89

redux/autorun.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,18 @@ def __init__(
128128
self._call()
129129

130130
if self._options.reactive:
131-
self._unsubscribe = store._subscribe( # noqa: SLF001
132-
lambda state: self._call() if self._check(state) else None,
133-
)
131+
self._unsubscribe = store._subscribe(self._react) # noqa: SLF001
134132
else:
135133
self._unsubscribe = None
136134

135+
def _react(
136+
self: Autorun,
137+
state: State,
138+
) -> None:
139+
"""React to state changes in the store."""
140+
if self._options.reactive and self._check(state):
141+
self._call()
142+
137143
def unsubscribe(
138144
self: Autorun[
139145
State,

redux/side_effect_runner.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Redux store for managing state and side effects."""
1+
"""Side effect runner thread for Redux."""
22

33
from __future__ import annotations
44

@@ -28,6 +28,7 @@ def __init__(
2828
) -> None:
2929
"""Initialize the side effect runner thread."""
3030
super().__init__()
31+
self.name = 'Side Effect Runner'
3132
self.task_queue = task_queue
3233
self.loop = asyncio.get_event_loop()
3334
self._handles: set[Handle] = set()

tests/test_scheduler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ async def call_callback(
8989
if self.stopped:
9090
return
9191
self.tasks.add(self.loop.create_task(asyncio.to_thread(callback)))
92-
await asyncio.sleep(0.01)
9392
if interval:
93+
await asyncio.sleep(0.01)
9494
self.tasks.add(
9595
self.loop.create_task(self.call_callback(callback, interval=interval)),
9696
)

0 commit comments

Comments
 (0)