Skip to content

Commit

Permalink
fix: detach on_msg handler on widget/template close
Browse files Browse the repository at this point in the history
This improves performance slightly, since we don't have to wait for garbage collection.
  • Loading branch information
iisakkirotko committed Nov 22, 2024
1 parent 9490c7b commit dfc8bbe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ipyvue/VueTemplateWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ def resolve_ref(value):
else:
getattr(self, "vue_" + event)(data)

def _clear_event_handler(self):
self.on_msg(self._handle_event, remove=True)


def _value_to_json(x, obj):
if inspect.isclass(x):
Expand Down Expand Up @@ -164,5 +167,9 @@ def on_ref_source_change(change):
for traitlet in sync_ref_traitlets:
create_ref_and_observe(traitlet)

def close(self):
self._clear_event_handler()
super().close()


__all__ = ["VueTemplate"]
7 changes: 7 additions & 0 deletions ipyvue/VueWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ def _handle_event(self, _, content, buffers):
data = content.get("data", {})
self._fire_event(event, data)

def _clear_event_handler(self):
self.on_msg(self._handle_event, remove=True)


class VueWidget(DOMWidget, Events):
# we can drop this when https://github.com/jupyter-widgets/ipywidgets/pull/3592
Expand Down Expand Up @@ -192,5 +195,9 @@ def hide(self):

self.class_list.add("d-none")

def close(self):
self._clear_event_handler()
super().close()


__all__ = ["VueWidget"]

0 comments on commit dfc8bbe

Please sign in to comment.