1
- # ruff: noqa: D100, D101, D102, D103, D104, D105, D107
1
+ """Redux store for managing state and side effects."""
2
2
from __future__ import annotations
3
3
4
4
import dataclasses
@@ -74,13 +74,14 @@ def run(self: _SideEffectRunnerThread[Event]) -> None:
74
74
75
75
76
76
class Store (Generic [State , Action , Event ]):
77
- custom_serializer = None
77
+ """Redux store for managing state and side effects."""
78
78
79
79
def __init__ (
80
80
self : Store [State , Action , Event ],
81
81
reducer : ReducerType [State , Action , Event ],
82
82
options : CreateStoreOptions | None = None ,
83
83
) -> None :
84
+ """Create a new store."""
84
85
self .store_options = options or CreateStoreOptions ()
85
86
self .reducer = reducer
86
87
self ._create_task : Callable [[Coroutine ], Any ] = (
@@ -175,6 +176,7 @@ def _run_event_handlers(self: Store[State, Action, Event]) -> None:
175
176
cast (Callable [[], Any ], event_handler )()
176
177
177
178
def run (self : Store [State , Action , Event ]) -> None :
179
+ """Run the store."""
178
180
with self ._is_running :
179
181
while len (self ._actions ) > 0 or len (self ._events ) > 0 :
180
182
if len (self ._actions ) > 0 :
@@ -189,6 +191,7 @@ def dispatch(
189
191
with_state : Callable [[State | None ], DispatchParameters [Action , Event ]]
190
192
| None = None ,
191
193
) -> None :
194
+ """Dispatch actions and/or events."""
192
195
if with_state is not None :
193
196
self .dispatch (with_state (self ._state ))
194
197
@@ -217,6 +220,7 @@ def subscribe(
217
220
* ,
218
221
keep_ref : bool = True ,
219
222
) -> Callable [[], None ]:
223
+ """Subscribe to state changes."""
220
224
if keep_ref :
221
225
listener_ref = listener
222
226
elif inspect .ismethod (listener ):
@@ -234,6 +238,7 @@ def subscribe_event(
234
238
* ,
235
239
options : EventSubscriptionOptions | None = None ,
236
240
) -> Callable [[], None ]:
241
+ """Subscribe to events."""
237
242
subscription_options = (
238
243
EventSubscriptionOptions () if options is None else options
239
244
)
@@ -271,6 +276,8 @@ def autorun(
271
276
SelectorOutput ,
272
277
AutorunOriginalReturnType ,
273
278
]:
279
+ """Create a new autorun, reflecting on state changes."""
280
+
274
281
def decorator (
275
282
func : Callable [[SelectorOutput ], AutorunOriginalReturnType ]
276
283
| Callable [[SelectorOutput , SelectorOutput ], AutorunOriginalReturnType ],
@@ -285,26 +292,19 @@ def decorator(
285
292
286
293
return decorator
287
294
288
- def set_custom_serializer (
289
- self : Store ,
290
- serializer : Callable [[object | type ], SnapshotAtom ],
291
- ) -> None :
292
- """Set a custom serializer for the store snapshot."""
293
- self .custom_serializer = serializer
294
-
295
295
@property
296
296
def snapshot (self : Store [State , Action , Event ]) -> SnapshotAtom :
297
- return self ._serialize_value (self ._state )
297
+ """Return a snapshot of the current state of the store."""
298
+ return self .serialize_value (self ._state )
298
299
299
- def _serialize_value (self : Store , obj : object | type ) -> SnapshotAtom :
300
- if self .custom_serializer :
301
- return self .custom_serializer (obj )
300
+ def serialize_value (self : Store , obj : object | type ) -> SnapshotAtom :
301
+ """Serialize a value to a snapshot atom."""
302
302
if is_immutable (obj ):
303
303
return self ._serialize_dataclass_to_dict (obj )
304
304
if isinstance (obj , (list , tuple )):
305
- return [self ._serialize_value (i ) for i in obj ]
305
+ return [self .serialize_value (i ) for i in obj ]
306
306
if callable (obj ):
307
- return self ._serialize_value (obj ())
307
+ return self .serialize_value (obj ())
308
308
if isinstance (obj , StrEnum ):
309
309
return str (obj )
310
310
if isinstance (obj , IntEnum ):
@@ -320,6 +320,6 @@ def _serialize_dataclass_to_dict(
320
320
) -> dict [str , Any ]:
321
321
result = {}
322
322
for field in dataclasses .fields (obj ):
323
- value = self ._serialize_value (getattr (obj , field .name ))
323
+ value = self .serialize_value (getattr (obj , field .name ))
324
324
result [field .name ] = value
325
325
return result
0 commit comments