Skip to content

Commit

Permalink
fix(ui): Wrap renderer in liveness scope instead of function element
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrunyon committed Nov 21, 2023
1 parent 9acb671 commit 168509a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
22 changes: 1 addition & 21 deletions plugins/ui/src/deephaven/ui/elements/FunctionElement.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations
import logging
from typing import Callable
from deephaven.liveness_scope import LivenessScope
from .Element import Element
from .._internal import RenderContext, get_context, set_context

Expand All @@ -19,22 +18,11 @@ def __init__(self, name: str, render: Callable[[], list[Element]]):
"""
self._name = name
self._render = render
self._liveness_scope = LivenessScope()

@property
def name(self):
return self._name

def release_liveness_scope(self):
try: # May not have an active liveness scope or already been released
self._liveness_scope.release()
self._liveness_scope = None
except:
pass

def __del__(self):
self.release_liveness_scope()

def render(self, context: RenderContext) -> list[Element]:
"""
Render the component. Should only be called when actually rendering the component, e.g. exporting it to the client.
Expand All @@ -50,17 +38,9 @@ def render(self, context: RenderContext) -> list[Element]:

set_context(context)

new_liveness_scope = LivenessScope()

with context, new_liveness_scope.open():
with context:
children = self._render()

# Release after in case of memoized tables
# Ref count goes 1 -> 2 -> 1 by releasing after
# instead of 1 -> 0 -> 1 which would release the table
self.release_liveness_scope()
self._liveness_scope = new_liveness_scope

logger.debug("Resetting to old context %s", old_context)
set_context(old_context)

Expand Down
24 changes: 23 additions & 1 deletion plugins/ui/src/deephaven/ui/renderer/Renderer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from typing import Any
from deephaven.liveness_scope import LivenessScope
from .._internal import RenderContext
from ..elements import Element
from .RenderedNode import RenderedNode
Expand Down Expand Up @@ -57,6 +58,27 @@ def render_child(child: Any, child_context: RenderContext):
class Renderer:
def __init__(self, context: RenderContext = RenderContext()):
self._context = context
self._liveness_scope = LivenessScope()

def __del__(self):
self.release_liveness_scope()

def release_liveness_scope(self):
try: # May not have an active liveness scope or already been released
self._liveness_scope.release()
self._liveness_scope = None
except:
pass

def render(self, element: Element):
return _render(self._context, element)
new_liveness_scope = LivenessScope()
with new_liveness_scope.open():
render_res = _render(self._context, element)

# Release after in case of memoized tables
# Ref count goes 1 -> 2 -> 1 by releasing after
# instead of 1 -> 0 -> 1 which would release the table
self.release_liveness_scope()
self._liveness_scope = new_liveness_scope

return render_res

0 comments on commit 168509a

Please sign in to comment.