26
26
from idom .utils import Ref
27
27
28
28
from ._thread_local import ThreadLocal
29
- from .types import ComponentType , Key , VdomDict
29
+ from .types import ComponentType , Key , State , VdomDict
30
30
from .vdom import vdom
31
31
32
32
46
46
47
47
logger = getLogger (__name__ )
48
48
49
- _StateType = TypeVar ("_StateType " )
49
+ _Type = TypeVar ("_Type " )
50
50
51
51
52
52
@overload
53
- def use_state (
54
- initial_value : Callable [[], _StateType ],
55
- ) -> Tuple [
56
- _StateType ,
57
- Callable [[_StateType | Callable [[_StateType ], _StateType ]], None ],
58
- ]:
53
+ def use_state (initial_value : Callable [[], _Type ]) -> State [_Type ]:
59
54
...
60
55
61
56
62
57
@overload
63
- def use_state (
64
- initial_value : _StateType ,
65
- ) -> Tuple [
66
- _StateType ,
67
- Callable [[_StateType | Callable [[_StateType ], _StateType ]], None ],
68
- ]:
58
+ def use_state (initial_value : _Type ) -> State [_Type ]:
69
59
...
70
60
71
61
72
- def use_state (
73
- initial_value : _StateType | Callable [[], _StateType ],
74
- ) -> Tuple [
75
- _StateType ,
76
- Callable [[_StateType | Callable [[_StateType ], _StateType ]], None ],
77
- ]:
62
+ def use_state (initial_value : _Type | Callable [[], _Type ]) -> State [_Type ]:
78
63
"""See the full :ref:`Use State` docs for details
79
64
80
65
Parameters:
@@ -87,16 +72,16 @@ def use_state(
87
72
A tuple containing the current state and a function to update it.
88
73
"""
89
74
current_state = _use_const (lambda : _CurrentState (initial_value ))
90
- return current_state .value , current_state .dispatch
75
+ return State ( current_state .value , current_state .dispatch )
91
76
92
77
93
- class _CurrentState (Generic [_StateType ]):
78
+ class _CurrentState (Generic [_Type ]):
94
79
95
80
__slots__ = "value" , "dispatch"
96
81
97
82
def __init__ (
98
83
self ,
99
- initial_value : Union [_StateType , Callable [[], _StateType ]],
84
+ initial_value : Union [_Type , Callable [[], _Type ]],
100
85
) -> None :
101
86
if callable (initial_value ):
102
87
self .value = initial_value ()
@@ -105,9 +90,7 @@ def __init__(
105
90
106
91
hook = current_hook ()
107
92
108
- def dispatch (
109
- new : Union [_StateType , Callable [[_StateType ], _StateType ]]
110
- ) -> None :
93
+ def dispatch (new : Union [_Type , Callable [[_Type ], _Type ]]) -> None :
111
94
if callable (new ):
112
95
next_value = new (self .value )
113
96
else :
@@ -234,14 +217,14 @@ def use_debug_value(
234
217
logger .debug (f"{ current_hook ().component } { new } " )
235
218
236
219
237
- def create_context (default_value : _StateType ) -> Context [_StateType ]:
220
+ def create_context (default_value : _Type ) -> Context [_Type ]:
238
221
"""Return a new context type for use in :func:`use_context`"""
239
222
240
223
def context (
241
224
* children : Any ,
242
- value : _StateType = default_value ,
225
+ value : _Type = default_value ,
243
226
key : Key | None = None ,
244
- ) -> ContextProvider [_StateType ]:
227
+ ) -> ContextProvider [_Type ]:
245
228
return ContextProvider (
246
229
* children ,
247
230
value = value ,
@@ -254,19 +237,19 @@ def context(
254
237
return context
255
238
256
239
257
- class Context (Protocol [_StateType ]):
240
+ class Context (Protocol [_Type ]):
258
241
"""Returns a :class:`ContextProvider` component"""
259
242
260
243
def __call__ (
261
244
self ,
262
245
* children : Any ,
263
- value : _StateType = ...,
246
+ value : _Type = ...,
264
247
key : Key | None = ...,
265
- ) -> ContextProvider [_StateType ]:
248
+ ) -> ContextProvider [_Type ]:
266
249
...
267
250
268
251
269
- def use_context (context : Context [_StateType ]) -> _StateType :
252
+ def use_context (context : Context [_Type ]) -> _Type :
270
253
"""Get the current value for the given context type.
271
254
272
255
See the full :ref:`Use Context` docs for more information.
@@ -282,7 +265,7 @@ def use_context(context: Context[_StateType]) -> _StateType:
282
265
# lastly check that 'value' kwarg exists
283
266
assert "value" in context .__kwdefaults__ , f"{ context } has no 'value' kwarg"
284
267
# then we can safely access the context's default value
285
- return cast (_StateType , context .__kwdefaults__ ["value" ])
268
+ return cast (_Type , context .__kwdefaults__ ["value" ])
286
269
287
270
subscribers = provider ._subscribers
288
271
@@ -294,13 +277,13 @@ def subscribe_to_context_change() -> Callable[[], None]:
294
277
return provider ._value
295
278
296
279
297
- class ContextProvider (Generic [_StateType ]):
280
+ class ContextProvider (Generic [_Type ]):
298
281
def __init__ (
299
282
self ,
300
283
* children : Any ,
301
- value : _StateType ,
284
+ value : _Type ,
302
285
key : Key | None ,
303
- type : Context [_StateType ],
286
+ type : Context [_Type ],
304
287
) -> None :
305
288
self .children = children
306
289
self .key = key
@@ -312,7 +295,7 @@ def render(self) -> VdomDict:
312
295
current_hook ().set_context_provider (self )
313
296
return vdom ("" , * self .children )
314
297
315
- def should_render (self , new : ContextProvider [_StateType ]) -> bool :
298
+ def should_render (self , new : ContextProvider [_Type ]) -> bool :
316
299
if not strictly_equal (self ._value , new ._value ):
317
300
for hook in self ._subscribers :
318
301
hook .set_context_provider (new )
@@ -328,9 +311,9 @@ def __repr__(self) -> str:
328
311
329
312
330
313
def use_reducer (
331
- reducer : Callable [[_StateType , _ActionType ], _StateType ],
332
- initial_value : _StateType ,
333
- ) -> Tuple [_StateType , Callable [[_ActionType ], None ]]:
314
+ reducer : Callable [[_Type , _ActionType ], _Type ],
315
+ initial_value : _Type ,
316
+ ) -> Tuple [_Type , Callable [[_ActionType ], None ]]:
334
317
"""See the full :ref:`Use Reducer` docs for details
335
318
336
319
Parameters:
@@ -348,8 +331,8 @@ def use_reducer(
348
331
349
332
350
333
def _create_dispatcher (
351
- reducer : Callable [[_StateType , _ActionType ], _StateType ],
352
- set_state : Callable [[Callable [[_StateType ], _StateType ]], None ],
334
+ reducer : Callable [[_Type , _ActionType ], _Type ],
335
+ set_state : Callable [[Callable [[_Type ], _Type ]], None ],
353
336
) -> Callable [[_ActionType ], None ]:
354
337
def dispatch (action : _ActionType ) -> None :
355
338
set_state (lambda last_state : reducer (last_state , action ))
@@ -409,7 +392,7 @@ def setup(function: _CallbackFunc) -> _CallbackFunc:
409
392
class _LambdaCaller (Protocol ):
410
393
"""MyPy doesn't know how to deal with TypeVars only used in function return"""
411
394
412
- def __call__ (self , func : Callable [[], _StateType ]) -> _StateType :
395
+ def __call__ (self , func : Callable [[], _Type ]) -> _Type :
413
396
...
414
397
415
398
@@ -423,16 +406,16 @@ def use_memo(
423
406
424
407
@overload
425
408
def use_memo (
426
- function : Callable [[], _StateType ],
409
+ function : Callable [[], _Type ],
427
410
dependencies : Sequence [Any ] | ellipsis | None = ...,
428
- ) -> _StateType :
411
+ ) -> _Type :
429
412
...
430
413
431
414
432
415
def use_memo (
433
- function : Optional [Callable [[], _StateType ]] = None ,
416
+ function : Optional [Callable [[], _Type ]] = None ,
434
417
dependencies : Sequence [Any ] | ellipsis | None = ...,
435
- ) -> Union [_StateType , Callable [[Callable [[], _StateType ]], _StateType ]]:
418
+ ) -> Union [_Type , Callable [[Callable [[], _Type ]], _Type ]]:
436
419
"""See the full :ref:`Use Memo` docs for details
437
420
438
421
Parameters:
@@ -449,7 +432,7 @@ def use_memo(
449
432
"""
450
433
dependencies = _try_to_infer_closure_values (function , dependencies )
451
434
452
- memo : _Memo [_StateType ] = _use_const (_Memo )
435
+ memo : _Memo [_Type ] = _use_const (_Memo )
453
436
454
437
if memo .empty ():
455
438
# we need to initialize on the first run
@@ -471,17 +454,17 @@ def use_memo(
471
454
else :
472
455
changed = False
473
456
474
- setup : Callable [[Callable [[], _StateType ]], _StateType ]
457
+ setup : Callable [[Callable [[], _Type ]], _Type ]
475
458
476
459
if changed :
477
460
478
- def setup (function : Callable [[], _StateType ]) -> _StateType :
461
+ def setup (function : Callable [[], _Type ]) -> _Type :
479
462
current_value = memo .value = function ()
480
463
return current_value
481
464
482
465
else :
483
466
484
- def setup (function : Callable [[], _StateType ]) -> _StateType :
467
+ def setup (function : Callable [[], _Type ]) -> _Type :
485
468
return memo .value
486
469
487
470
if function is not None :
@@ -490,12 +473,12 @@ def setup(function: Callable[[], _StateType]) -> _StateType:
490
473
return setup
491
474
492
475
493
- class _Memo (Generic [_StateType ]):
476
+ class _Memo (Generic [_Type ]):
494
477
"""Simple object for storing memoization data"""
495
478
496
479
__slots__ = "value" , "deps"
497
480
498
- value : _StateType
481
+ value : _Type
499
482
deps : Sequence [Any ]
500
483
501
484
def empty (self ) -> bool :
@@ -507,7 +490,7 @@ def empty(self) -> bool:
507
490
return False
508
491
509
492
510
- def use_ref (initial_value : _StateType ) -> Ref [_StateType ]:
493
+ def use_ref (initial_value : _Type ) -> Ref [_Type ]:
511
494
"""See the full :ref:`Use State` docs for details
512
495
513
496
Parameters:
@@ -519,7 +502,7 @@ def use_ref(initial_value: _StateType) -> Ref[_StateType]:
519
502
return _use_const (lambda : Ref (initial_value ))
520
503
521
504
522
- def _use_const (function : Callable [[], _StateType ]) -> _StateType :
505
+ def _use_const (function : Callable [[], _Type ]) -> _Type :
523
506
return current_hook ().use_state (function )
524
507
525
508
@@ -670,7 +653,7 @@ def schedule_render(self) -> None:
670
653
self ._schedule_render ()
671
654
return None
672
655
673
- def use_state (self , function : Callable [[], _StateType ]) -> _StateType :
656
+ def use_state (self , function : Callable [[], _Type ]) -> _Type :
674
657
if not self ._rendered_atleast_once :
675
658
# since we're not intialized yet we're just appending state
676
659
result = function ()
@@ -689,8 +672,8 @@ def set_context_provider(self, provider: ContextProvider[Any]) -> None:
689
672
self ._context_providers [provider .type ] = provider
690
673
691
674
def get_context_provider (
692
- self , context : Context [_StateType ]
693
- ) -> ContextProvider [_StateType ] | None :
675
+ self , context : Context [_Type ]
676
+ ) -> ContextProvider [_Type ] | None :
694
677
return self ._context_providers .get (context )
695
678
696
679
def affect_component_will_render (self , component : ComponentType ) -> None :
0 commit comments