Skip to content

Commit f7a59f2

Browse files
committed
EvenHandler should not serialize itself
1 parent 42b2e20 commit f7a59f2

File tree

3 files changed

+30
-37
lines changed

3 files changed

+30
-37
lines changed

src/idom/core/events.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,9 @@ class EventHandler:
159159
"__weakref__",
160160
"_coro_handlers",
161161
"_func_handlers",
162-
"_target_id",
163-
"_prevent_default",
164-
"_stop_propogation",
162+
"target_id",
163+
"prevent_default",
164+
"stop_propogation",
165165
)
166166

167167
def __init__(
@@ -170,16 +170,11 @@ def __init__(
170170
prevent_default: bool = False,
171171
target_id: Optional[str] = None,
172172
) -> None:
173+
self.target_id = target_id or str(id(self))
174+
self.stop_propogation = stop_propagation
175+
self.prevent_default = prevent_default
173176
self._coro_handlers: List[Callable[..., Coroutine[Any, Any, Any]]] = []
174177
self._func_handlers: List[Callable[..., Any]] = []
175-
self._target_id = target_id or str(id(self))
176-
self._stop_propogation = stop_propagation
177-
self._prevent_default = prevent_default
178-
179-
@property
180-
def id(self) -> str:
181-
"""ID of the event handler."""
182-
return self._target_id
183178

184179
def add(self, function: Callable[..., Any]) -> "EventHandler":
185180
"""Add a callback to the event handler.
@@ -207,14 +202,6 @@ def remove(self, function: Callable[..., Any]) -> None:
207202
else:
208203
self._func_handlers.remove(function)
209204

210-
def serialize(self) -> EventTarget:
211-
"""Serialize the event handler."""
212-
return {
213-
"target": self._target_id,
214-
"preventDefault": self._prevent_default,
215-
"stopPropagation": self._stop_propogation,
216-
}
217-
218205
async def __call__(self, data: List[Any]) -> Any:
219206
"""Trigger all callbacks in the event handler."""
220207
if self._coro_handlers:

src/idom/core/layout.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,19 @@ def _render_model_event_targets(
235235
h = attrs.pop(k)
236236
handlers[k] = h
237237

238-
event_handlers_by_id = {h.id: h for h in handlers.values()}
238+
event_handlers_by_id = {h.target_id: h for h in handlers.values()}
239239
component_state.event_handler_ids.clear()
240240
component_state.event_handler_ids.update(event_handlers_by_id)
241241
self._event_handlers.update(event_handlers_by_id)
242242

243-
return {e: h.serialize() for e, h in handlers.items()}
243+
return {
244+
e: {
245+
"target": h.target_id,
246+
"preventDefault": h.prevent_default,
247+
"stopPropagation": h.stop_propogation,
248+
}
249+
for e, h in handlers.items()
250+
}
244251

245252
def _get_component_state(self, component: AbstractComponent) -> ComponentState:
246253
return self._component_states[id(component)]

tests/test_core/test_events.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,21 @@ async def handler_2():
3939
assert calls == [1, 2]
4040

4141

42-
def test_event_handler_serialization():
43-
assert EventHandler(target_id="uuid").serialize() == {
44-
"target": "uuid",
45-
"stopPropagation": False,
46-
"preventDefault": False,
47-
}
48-
assert EventHandler(target_id="uuid", prevent_default=True).serialize() == {
49-
"target": "uuid",
50-
"stopPropagation": False,
51-
"preventDefault": True,
52-
}
53-
assert EventHandler(target_id="uuid", stop_propagation=True).serialize() == {
54-
"target": "uuid",
55-
"stopPropagation": True,
56-
"preventDefault": False,
57-
}
42+
def test_event_handler_props():
43+
handler_0 = EventHandler(target_id="uuid")
44+
assert handler_0.target_id == "uuid"
45+
assert handler_0.stop_propagation is False
46+
assert handler_0.prevent_default is False
47+
48+
handler_1 = EventHandler(target_id="uuid", prevent_default=True)
49+
assert handler_1.target_id == "uuid"
50+
assert handler_1.stop_propagation is False
51+
assert handler_1.prevent_default is True
52+
53+
handler_2 = EventHandler(target_id="uuid", stop_propagation=True)
54+
assert handler_2.target_id == "uuid"
55+
assert handler_2.stop_propagation is True
56+
assert handler_2.prevent_default is False
5857

5958

6059
async def test_multiple_callbacks_per_event_handler():

0 commit comments

Comments
 (0)