Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix conditional use_effect in use_table_listener #422

Merged
merged 3 commits into from
Apr 18, 2024
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
4 changes: 4 additions & 0 deletions plugins/ui/src/deephaven/ui/hooks/use_table_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ def use_table_data(

table_updated = lambda: _set_new_data(table, sentinel, set_data, set_is_sentinel)

# call table_updated in the case of new table or sentinel
ui.use_effect(table_updated, [table, sentinel])

# call table_updated every time the table updates
ui.use_table_listener(
table, partial(_on_update, ctx, table_updated, executor_name), []
)
Expand Down
9 changes: 4 additions & 5 deletions plugins/ui/src/deephaven/ui/hooks/use_table_listener.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from functools import partial
from typing import Any, Callable, Sequence
from typing import Callable

from deephaven.table import Table
from deephaven.table_listener import listen, TableUpdate, TableListener
Expand Down Expand Up @@ -86,17 +86,16 @@ def use_table_listener(
replay_lock: The lock type used during replay, default is ‘shared’, can also be ‘exclusive’.
"""

if not table.is_refreshing and not do_replay:
# if the table is not refreshing, and is not replaying, there is nothing to listen to
return

def start_listener() -> Callable[[], None]:
"""
Start the listener. Returns a function that can be called to stop the listener by the use_effect hook.

Returns:
A function that can be called to stop the listener by the use_effect hook.
"""
if not table.is_refreshing and not do_replay:
return lambda: None

handle = listen(
table,
wrap_listener(listener),
Expand Down
45 changes: 45 additions & 0 deletions plugins/ui/test/deephaven/ui/test_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,51 @@ def _test_table_data(t=table):

self.assertEqual(result, expected)

def test_swapping_table_data(self):
from deephaven.ui.hooks import use_table_data
from deephaven import new_table
from deephaven.column import int_col
from deephaven import DynamicTableWriter
import deephaven.dtypes as dht

table = new_table(
[
int_col("X", [1, 2, 3]),
int_col("Y", [2, 4, 6]),
]
)

def _test_table_data(t=table):
result = use_table_data(t, sentinel="sentinel")
return result

render_result = render_hook(_test_table_data)

result, rerender = itemgetter("result", "rerender")(render_result)

column_definitions = {"Numbers": dht.int32, "Words": dht.string}

table_writer = DynamicTableWriter(column_definitions)
dynamic_table = table_writer.table

# Need two rerenders because the first one will call set_data, which queues state updates
# that are resolved at the start of the second rerender
# The second rerender will then have the expected state values and return the expected result
rerender(dynamic_table)
result = rerender(dynamic_table)

# the initial render should return the sentinel value since the table is empty
self.assertEqual(result, "sentinel")

self.verify_table_updated(table_writer, dynamic_table, (1, "Testing"))

rerender(dynamic_table)
result = rerender(dynamic_table)

expected = {"Numbers": [1], "Words": ["Testing"]}

self.assertEqual(result, expected)

def test_column_data(self):
from deephaven.ui.hooks import use_column_data
from deephaven import new_table
Expand Down
Loading