Skip to content

Commit 8d89231

Browse files
committed
Remove ContextProviderType, replace with ContextProvider
1 parent 3780568 commit 8d89231

File tree

4 files changed

+29
-38
lines changed

4 files changed

+29
-38
lines changed

src/reactpy/core/_life_cycle_hook.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from anyio import Semaphore
1111

1212
from reactpy.core._thread_local import ThreadLocal
13-
from reactpy.types import Component, Context, ContextProviderType
13+
from reactpy.types import Component, Context, ContextProvider
1414
from reactpy.utils import Singleton
1515

1616
T = TypeVar("T")
@@ -152,7 +152,7 @@ def __init__(
152152
self,
153153
schedule_render: Callable[[], None],
154154
) -> None:
155-
self._context_providers: dict[Context[Any], ContextProviderType[Any]] = {}
155+
self._context_providers: dict[Context[Any], ContextProvider[Any]] = {}
156156
self._schedule_render_callback = schedule_render
157157
self._scheduled_render = False
158158
self._rendered_atleast_once = False
@@ -201,17 +201,15 @@ def add_effect(self, effect_func: EffectFunc) -> None:
201201
"""
202202
self._effect_funcs.append(effect_func)
203203

204-
def set_context_provider(self, provider: ContextProviderType[Any]) -> None:
204+
def set_context_provider(self, provider: ContextProvider[Any]) -> None:
205205
"""Set a context provider for this hook
206206
207207
The context provider will be used to provide state to any child components
208208
of this hook's component which request a context provider of the same type.
209209
"""
210210
self._context_providers[provider.type] = provider
211211

212-
def get_context_provider(
213-
self, context: Context[T]
214-
) -> ContextProviderType[T] | None:
212+
def get_context_provider(self, context: Context[T]) -> ContextProvider[T] | None:
215213
"""Get a context provider for this hook of the given type
216214
217215
The context provider will have been set by a parent component. If no provider

src/reactpy/core/hooks.py

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
from reactpy.types import (
2222
Connection,
2323
Context,
24+
ContextProvider,
2425
Key,
2526
Location,
2627
State,
27-
VdomDict,
2828
)
2929
from reactpy.utils import Ref
3030

@@ -301,8 +301,8 @@ def context(
301301
*children: Any,
302302
value: _Type = default_value,
303303
key: Key | None = None,
304-
) -> _ContextProvider[_Type]:
305-
return _ContextProvider(
304+
) -> ContextProvider[_Type]:
305+
return ContextProvider(
306306
*children,
307307
value=value,
308308
key=key,
@@ -358,27 +358,6 @@ def use_location() -> Location:
358358
return use_connection().location
359359

360360

361-
class _ContextProvider(Generic[_Type]):
362-
def __init__(
363-
self,
364-
*children: Any,
365-
value: _Type,
366-
key: Key | None,
367-
type: Context[_Type],
368-
) -> None:
369-
self.children = children
370-
self.key = key
371-
self.type = type
372-
self.value = value
373-
374-
def render(self) -> VdomDict:
375-
HOOK_STACK.current_hook().set_context_provider(self)
376-
return VdomDict(tagName="", children=self.children)
377-
378-
def __repr__(self) -> str:
379-
return f"ContextProvider({self.type})"
380-
381-
382361
_ActionType = TypeVar("_ActionType")
383362

384363

src/reactpy/core/layout.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from reactpy.types import (
4040
Component,
4141
Context,
42+
ContextProvider,
4243
Event,
4344
EventHandlerDict,
4445
Key,
@@ -69,7 +70,7 @@ class Layout:
6970
if not hasattr(abc.ABC, "__weakref__"): # nocov
7071
__slots__ += ("__weakref__",)
7172

72-
def __init__(self, root: Component | Context[Any]) -> None:
73+
def __init__(self, root: Component | Context[Any] | ContextProvider[Any]) -> None:
7374
super().__init__()
7475
if not isinstance(root, Component):
7576
msg = f"Expected a ComponentType, not {type(root)!r}."

src/reactpy/types.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,17 +1010,30 @@ def __call__(
10101010
*children: Any,
10111011
value: _Type = ...,
10121012
key: Key | None = ...,
1013-
) -> ContextProviderType[_Type]: ...
1013+
) -> ContextProvider[_Type]: ...
10141014

10151015

1016-
class ContextProviderType(Component, Protocol[_Type]):
1017-
"""A component which provides a context value to its children"""
1016+
class ContextProvider(Generic[_Type]):
1017+
def __init__(
1018+
self,
1019+
*children: Any,
1020+
value: _Type,
1021+
key: Key | None,
1022+
type: Context[_Type],
1023+
) -> None:
1024+
self.children = children
1025+
self.key = key
1026+
self.type = type
1027+
self.value = value
10181028

1019-
type: Context[_Type]
1020-
"""The context type"""
1029+
def render(self) -> VdomDict:
1030+
from reactpy.core.hooks import HOOK_STACK
10211031

1022-
@property
1023-
def value(self) -> _Type: ... # Current context value
1032+
HOOK_STACK.current_hook().set_context_provider(self)
1033+
return VdomDict(tagName="", children=self.children)
1034+
1035+
def __repr__(self) -> str:
1036+
return f"ContextProvider({self.type})"
10241037

10251038

10261039
@dataclass

0 commit comments

Comments
 (0)