diff --git a/CHANGELOG.md b/CHANGELOG.md index fb22075..6a7ba90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/redux/main.py b/redux/main.py index d440917..06abba4 100644 --- a/redux/main.py +++ b/redux/main.py @@ -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: diff --git a/redux/side_effect_runner.py b/redux/side_effect_runner.py index d30008a..6826635 100644 --- a/redux/side_effect_runner.py +++ b/redux/side_effect_runner.py @@ -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 @@ -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: