Skip to content

Commit 8f145d8

Browse files
committed
feat: all subscriptions/listeners with keep_ref, now use WeakMethod for methods
1 parent 93fa2ba commit 8f145d8

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
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+
## Version 0.9.25
4+
5+
- feat: all subscriptions/listeners with `keep_ref`, now use `WeakMethod` for methods
6+
37
## Version 0.9.24
48

59
- refactor: no error if an unsubscription function is called multiple times

redux/combine_reducers.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from __future__ import annotations
33

44
import copy
5+
import functools
6+
import operator
57
import uuid
68
from dataclasses import asdict, make_dataclass
79
from typing import TYPE_CHECKING, Any, TypeVar, cast
@@ -152,14 +154,16 @@ def combined_reducer(
152154
for key, result in reducers_results.items()
153155
},
154156
)
155-
result_actions += sum(
157+
result_actions += functools.reduce(
158+
operator.iadd,
156159
[
157160
result.actions or [] if is_reducer_result(result) else []
158161
for result in reducers_results.values()
159162
],
160163
[],
161164
)
162-
result_events += sum(
165+
result_events += functools.reduce(
166+
operator.iadd,
163167
[
164168
result.events or [] if is_reducer_result(result) else []
165169
for result in reducers_results.values()

redux/main.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from collections import defaultdict
88
from inspect import signature
99
from threading import Lock
10+
from types import MethodType
1011
from typing import Any, Callable, Generic, cast
1112

1213
from .basic_types import (
@@ -162,7 +163,12 @@ def subscribe(
162163
*,
163164
keep_ref: bool = True,
164165
) -> Callable[[], None]:
165-
listener_ref = listener if keep_ref else weakref.ref(listener)
166+
if keep_ref:
167+
listener_ref = listener
168+
elif isinstance(listener, MethodType):
169+
listener_ref = weakref.WeakMethod(listener)
170+
else:
171+
listener_ref = weakref.ref(listener)
166172

167173
listeners.add(listener_ref)
168174
return lambda: listeners.remove(listener_ref)
@@ -177,7 +183,12 @@ def subscribe_event(
177183
EventSubscriptionOptions() if options is None else options
178184
)
179185

180-
handler_ref = handler if subscription_options.keep_ref else weakref.ref(handler)
186+
if subscription_options.keep_ref:
187+
handler_ref = handler
188+
elif isinstance(handler, MethodType):
189+
handler_ref = weakref.WeakMethod(handler)
190+
else:
191+
handler_ref = weakref.ref(handler)
181192

182193
event_handlers[cast(type[Event], event_type)].add(
183194
(handler_ref, subscription_options),
@@ -284,7 +295,12 @@ def subscribe(
284295
| None = autorun_options.subscribers_immediate_run,
285296
keep_ref: bool | None = autorun_options.subscribers_keep_ref,
286297
) -> Callable[[], None]:
287-
callback_ref = callback if keep_ref else weakref.ref(callback)
298+
if keep_ref:
299+
callback_ref = callback
300+
elif isinstance(callback, MethodType):
301+
callback_ref = weakref.WeakMethod(callback)
302+
else:
303+
callback_ref = weakref.ref(callback)
288304
subscriptions.add(callback_ref)
289305

290306
if immediate_run:

0 commit comments

Comments
 (0)