Skip to content

Commit ad2cdc2

Browse files
committed
Revert "add schedule_render() method to AbstractComponent"
This reverts commit 6c70a9e.
1 parent 6c70a9e commit ad2cdc2

File tree

4 files changed

+6
-78
lines changed

4 files changed

+6
-78
lines changed

idom/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from .utils import Ref, html_to_vdom
1313

14-
from .core.component import component, Component, AbstractComponent
14+
from .core.component import component, Component
1515
from .core.events import event, Events
1616
from .core.layout import Layout
1717
from .core.vdom import vdom, VdomDict
@@ -64,5 +64,4 @@
6464
"widgets",
6565
"client",
6666
"install",
67-
"AbstractComponent",
6867
]

idom/core/component.py

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
import inspect
33
from functools import wraps
44
from typing import TYPE_CHECKING, Dict, Callable, Any, Tuple, Union
5-
from weakref import ReferenceType
65

7-
from .hooks import LifeCycleHook
86

97
if TYPE_CHECKING: # pragma: no cover
108
from .vdom import VdomDict # noqa
@@ -30,41 +28,16 @@ def constructor(*args: Any, **kwargs: Any) -> Component:
3028

3129

3230
class AbstractComponent(abc.ABC):
33-
"""A base class for all component implementations"""
3431

35-
__slots__ = ["_life_cycle_hook"]
36-
if not hasattr(abc.ABC, "__weakref__"):
37-
__slots__.append("__weakref__")
38-
39-
# When a LifeCyleHook is created it will bind a WeakReference of itself to the its
40-
# component. This is only useful for class-based component implementations. For
41-
# functional components, the LifeCycleHook is accessed by getting the current_hook().
42-
_life_cycle_hook: "ReferenceType[LifeCycleHook]"
32+
__slots__ = [] if hasattr(abc.ABC, "__weakref__") else ["__weakref__"]
4333

4434
@abc.abstractmethod
4535
def render(self) -> "VdomDict":
4636
"""Render the component's :ref:`VDOM <VDOM Mimetype>` model."""
4737

48-
def schedule_render(self):
49-
"""Schedule a re-render of this component
50-
51-
This is only used by class-based component implementations. Most components
52-
should be functional components that use hooks to schedule renders and save
53-
state.
54-
"""
55-
try:
56-
hook = self._life_cycle_hook()
57-
except AttributeError:
58-
raise RuntimeError(
59-
f"Component {self} has no hook. Are you rendering in a layout?"
60-
)
61-
else:
62-
assert hook is not None, f"LifeCycleHook for {self} no longer exists"
63-
hook.schedule_render()
64-
6538

6639
class Component(AbstractComponent):
67-
"""A functional component"""
40+
"""An object for rending component models."""
6841

6942
__slots__ = (
7043
"_function",

idom/core/hooks.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,16 @@
1313
Union,
1414
NamedTuple,
1515
List,
16-
TYPE_CHECKING,
1716
overload,
1817
cast,
1918
)
20-
from weakref import ref
21-
2219
from typing_extensions import Protocol
2320

2421
from loguru import logger
2522

2623
from idom.utils import Ref
2724

28-
if TYPE_CHECKING: # pragma: no cover
29-
from .component import AbstractComponent
25+
from .component import AbstractComponent
3026

3127

3228
__all__ = [
@@ -386,11 +382,10 @@ class LifeCycleHook:
386382

387383
def __init__(
388384
self,
389-
component: "AbstractComponent",
390-
schedule_render: Callable[["AbstractComponent"], None],
385+
component: AbstractComponent,
386+
schedule_render: Callable[[AbstractComponent], None],
391387
) -> None:
392388
self.component = component
393-
component._life_cycle_hook = ref(self)
394389
self._schedule_render_callback = schedule_render
395390
self._schedule_render_later = False
396391
self._is_rendering = False

tests/test_core/test_component.py renamed to tests/test_core/test_element.py

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import pytest
2-
31
import idom
42

53

@@ -70,40 +68,3 @@ def PreFormated():
7068
pre.get_attribute("innerHTML")
7169
== "<span>this<span>is</span>some</span>pre-formated text"
7270
)
73-
74-
75-
def test_class_component(driver, display, driver_wait):
76-
class Counter(idom.AbstractComponent):
77-
def __init__(self):
78-
self.count = 0
79-
80-
def render(self):
81-
return idom.html.button(
82-
{"onClick": lambda event: self._increment_count(), "id": "counter"},
83-
f"Clicked {self.count} times",
84-
)
85-
86-
def _increment_count(self):
87-
self.count += 1
88-
self.schedule_render()
89-
90-
display(Counter)
91-
92-
client_counter = driver.find_element_by_id("counter")
93-
94-
for i in range(5):
95-
driver_wait.until(
96-
lambda d: client_counter.get_attribute("innerHTML") == f"Clicked {i} times"
97-
)
98-
client_counter.click()
99-
100-
101-
def test_class_component_has_no_hook():
102-
class MyComponent(idom.AbstractComponent):
103-
def render(self):
104-
...
105-
106-
component = MyComponent()
107-
108-
with pytest.raises(RuntimeError, match="no hook"):
109-
component.schedule_render()

0 commit comments

Comments
 (0)