Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions collagraph/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ def inject(self, key, default=None):
def emit(self, event, *args, **kwargs):
"""Call event handlers for the given event. Any args and kwargs will be passed
on to the registered handlers."""
for handler in self._event_handlers[event].copy():
handler(*args, **kwargs)
if handlers := self._event_handlers.get(event):
for handler in tuple(handlers):
handler(*args, **kwargs)

def add_event_handler(self, event, handler):
"""Adds an event handler for the given event."""
Expand Down
15 changes: 1 addition & 14 deletions collagraph/fragment.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,20 +809,7 @@ def mount(self, target: Any, anchor: Any | None = None):
child.mount(target, anchor)

if self.component:
from collections import deque

# Use fifo data structure (double-ended queue)
lookup = deque(self.children)
try:
while True:
child = lookup.popleft()
if element := child.element:
self.component._element = element
lookup.clear()
break
lookup.extend(child.children)
except IndexError:
pass
self.component._element = self.first()

# Register static component ref after component is mounted
# (dynamic refs are handled by the watcher created in create())
Expand Down
7 changes: 1 addition & 6 deletions collagraph/weak.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from functools import wraps
from inspect import signature
from weakref import ref


Expand Down Expand Up @@ -29,11 +28,7 @@ def callback(self):
weak_obj = ref(obj)

def wrapper(method):
sig = signature(method)
non_default_parameters = [
par for par in sig.parameters.values() if par.default is par.empty
]
nr_arguments = len(non_default_parameters)
nr_arguments = method.__code__.co_argcount - len(method.__defaults__ or ())

if nr_arguments == 0:
raise TypeError(
Expand Down
33 changes: 33 additions & 0 deletions tests/test_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,36 @@ def mounted(self):
assert Example.component is not None
assert isinstance(Example.component, Example)
assert Example.component.element is container["children"][0]


def test_component_element_behind_control_flow_wrapper(parse_source):
"""
A component whose render root is itself a wrapper (e.g. v-if) around a
single element should still resolve `component.element` to that nested
element (regression test for ComponentFragment.mount() using
`self.first()` instead of a manual BFS over `self.children`).
"""
Example, _ = parse_source(
"""
<el v-if="show" />

<script>
import collagraph as cg

class Example(cg.Component):
component = None

def mounted(self):
Example.component = self
</script>
"""
)

gui = cg.Collagraph(cg.DictRenderer(), event_loop_type=cg.EventLoopType.SYNC)
container = {"type": "root"}
state = reactive({"show": True})
gui.render(Example, container, state=state)

assert Example.component is not None
assert isinstance(Example.component, Example)
assert Example.component.element is container["children"][0]
Loading