Skip to content

Commit 30a8bf6

Browse files
committed
EventHandlerType -> BaseEventHandler
1 parent 058e3c6 commit 30a8bf6

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

src/reactpy/core/events.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from anyio import create_task_group
99

10-
from reactpy.types import EventHandlerFunc, EventHandlerType
10+
from reactpy.types import BaseEventHandler, EventHandlerFunc
1111

1212

1313
@overload
@@ -73,7 +73,7 @@ def setup(function: Callable[..., Any]) -> EventHandler:
7373
return setup(function) if function is not None else setup
7474

7575

76-
class EventHandler:
76+
class EventHandler(BaseEventHandler):
7777
"""Turn a function or coroutine into an event handler
7878
7979
Parameters:
@@ -87,14 +87,6 @@ class EventHandler:
8787
A unique identifier for this event handler (auto-generated by default)
8888
"""
8989

90-
__slots__ = (
91-
"__weakref__",
92-
"function",
93-
"prevent_default",
94-
"stop_propagation",
95-
"target",
96-
)
97-
9890
def __init__(
9991
self,
10092
function: EventHandlerFunc,
@@ -194,8 +186,8 @@ async def wrapper(data: Sequence[Any]) -> None:
194186

195187

196188
def merge_event_handlers(
197-
event_handlers: Sequence[EventHandlerType],
198-
) -> EventHandlerType:
189+
event_handlers: Sequence[BaseEventHandler],
190+
) -> BaseEventHandler:
199191
"""Merge multiple event handlers into one
200192
201193
Raises a ValueError if any handlers have conflicting

src/reactpy/core/vdom.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
from reactpy.core._f_back import f_module_name
1818
from reactpy.core.events import EventHandler, to_event_handler_function
1919
from reactpy.types import (
20+
BaseEventHandler,
2021
Component,
2122
CustomVdomConstructor,
2223
EllipsisRepr,
2324
EventHandlerDict,
24-
EventHandlerType,
2525
ImportSourceDict,
2626
InlineJavaScript,
2727
InlineJavaScriptDict,
@@ -231,13 +231,13 @@ def separate_attributes_handlers_and_inline_javascript(
231231
attributes: Mapping[str, Any],
232232
) -> tuple[VdomAttributes, EventHandlerDict, InlineJavaScriptDict]:
233233
_attributes: VdomAttributes = {}
234-
_event_handlers: dict[str, EventHandlerType] = {}
234+
_event_handlers: dict[str, BaseEventHandler] = {}
235235
_inline_javascript: dict[str, InlineJavaScript] = {}
236236

237237
for k, v in attributes.items():
238238
if callable(v):
239239
_event_handlers[k] = EventHandler(to_event_handler_function(v))
240-
elif isinstance(v, EventHandler):
240+
elif isinstance(v, BaseEventHandler):
241241
_event_handlers[k] = v
242242
elif EVENT_ATTRIBUTE_PATTERN.match(k) and isinstance(v, str):
243243
_inline_javascript[k] = InlineJavaScript(v)

src/reactpy/types.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -926,19 +926,26 @@ class EventHandlerFunc(Protocol):
926926
async def __call__(self, data: Sequence[Any]) -> None: ...
927927

928928

929-
@runtime_checkable
930-
class EventHandlerType(Protocol):
929+
class BaseEventHandler:
931930
"""Defines a handler for some event"""
932931

932+
__slots__ = (
933+
"__weakref__",
934+
"function",
935+
"prevent_default",
936+
"stop_propagation",
937+
"target",
938+
)
939+
940+
function: EventHandlerFunc
941+
"""A coroutine which can respond to an event and its data"""
942+
933943
prevent_default: bool
934944
"""Whether to block the event from propagating further up the DOM"""
935945

936946
stop_propagation: bool
937947
"""Stops the default action associate with the event from taking place."""
938948

939-
function: EventHandlerFunc
940-
"""A coroutine which can respond to an event and its data"""
941-
942949
target: str | None
943950
"""Typically left as ``None`` except when a static target is useful.
944951
@@ -951,10 +958,10 @@ class EventHandlerType(Protocol):
951958
"""
952959

953960

954-
EventHandlerMapping = Mapping[str, EventHandlerType]
961+
EventHandlerMapping = Mapping[str, BaseEventHandler]
955962
"""A generic mapping between event names to their handlers"""
956963

957-
EventHandlerDict: TypeAlias = dict[str, EventHandlerType]
964+
EventHandlerDict: TypeAlias = dict[str, BaseEventHandler]
958965
"""A dict mapping between event names to their handlers"""
959966

960967
InlineJavaScriptMapping = Mapping[str, InlineJavaScript]

0 commit comments

Comments
 (0)