Skip to content

Commit

Permalink
fix: use weakref of event handlers in event_handlers_queue
Browse files Browse the repository at this point in the history
  • Loading branch information
sassanh committed Mar 22, 2024
1 parent 291ff97 commit 17432ed
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Version 0.12.7

- fix: automatically unsubscribe autoruns when the weakref is dead
- fix: use weakref of event handlers in `event_handlers_queue`

## Version 0.12.6

Expand Down
20 changes: 10 additions & 10 deletions redux/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,18 @@ def _run_actions(self: Store[State, Action, Event]) -> None:
def _run_event_handlers(self: Store[State, Action, Event]) -> None:
event = self._events.pop(0)
for event_handler_, options in self._event_handlers[type(event)].copy():
if isinstance(event_handler_, weakref.ref):
event_handler = event_handler_()
if event_handler is None:
self._event_handlers[type(event)].discard(
(event_handler_, options),
)
continue
else:
event_handler = event_handler_
if not options.immediate_run:
self._event_handlers_queue.put((event_handler, event))
self._event_handlers_queue.put((event_handler_, event))
else:
if isinstance(event_handler_, weakref.ref):
event_handler = event_handler_()
if event_handler is None:
self._event_handlers[type(event)].discard(
(event_handler_, options),
)
continue
else:
event_handler = event_handler_
if len(signature(event_handler).parameters) == 1:
result = cast(Callable[[Event], Any], event_handler)(event)
else:
Expand Down
9 changes: 8 additions & 1 deletion redux/side_effect_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import threading
import weakref
from asyncio import iscoroutine
from inspect import signature
from typing import TYPE_CHECKING, Any, Callable, Generic, cast
Expand Down Expand Up @@ -36,7 +37,13 @@ def run(self: SideEffectRunnerThread[Event]) -> None:
break

try:
event_handler, event = task
event_handler_, event = task
if isinstance(event_handler_, weakref.ref):
event_handler = event_handler_()
if event_handler is None:
continue
else:
event_handler = event_handler_
if len(signature(event_handler).parameters) == 1:
result = cast(Callable[[Event], Any], event_handler)(event)
else:
Expand Down

0 comments on commit 17432ed

Please sign in to comment.