From e60a759b46c24be899157819009c28c5ec7ba102 Mon Sep 17 00:00:00 2001 From: Joe Numainville Date: Wed, 17 Apr 2024 15:54:21 -0500 Subject: [PATCH 1/3] wip --- plugins/ui/src/deephaven/ui/hooks/use_table_data.py | 4 ++++ plugins/ui/src/deephaven/ui/hooks/use_table_listener.py | 7 +++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/plugins/ui/src/deephaven/ui/hooks/use_table_data.py b/plugins/ui/src/deephaven/ui/hooks/use_table_data.py index 3f2714612..200fe7049 100644 --- a/plugins/ui/src/deephaven/ui/hooks/use_table_data.py +++ b/plugins/ui/src/deephaven/ui/hooks/use_table_data.py @@ -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), [] ) diff --git a/plugins/ui/src/deephaven/ui/hooks/use_table_listener.py b/plugins/ui/src/deephaven/ui/hooks/use_table_listener.py index a7eef4ad5..8140af24a 100644 --- a/plugins/ui/src/deephaven/ui/hooks/use_table_listener.py +++ b/plugins/ui/src/deephaven/ui/hooks/use_table_listener.py @@ -86,10 +86,6 @@ 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. @@ -97,6 +93,9 @@ def start_listener() -> Callable[[], None]: 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), From 10c796a22c73a0afc5ffb1fa0ffad6b51a86dc50 Mon Sep 17 00:00:00 2001 From: Joe Numainville Date: Thu, 18 Apr 2024 11:36:18 -0500 Subject: [PATCH 2/3] add tests --- plugins/ui/test/deephaven/ui/test_hooks.py | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/plugins/ui/test/deephaven/ui/test_hooks.py b/plugins/ui/test/deephaven/ui/test_hooks.py index dce874bb1..40cdf1900 100644 --- a/plugins/ui/test/deephaven/ui/test_hooks.py +++ b/plugins/ui/test/deephaven/ui/test_hooks.py @@ -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 From 3d536cdddf627eb0f9aa229116b541d105664c0e Mon Sep 17 00:00:00 2001 From: Joe Numainville Date: Thu, 18 Apr 2024 12:03:22 -0500 Subject: [PATCH 3/3] remove types --- plugins/ui/src/deephaven/ui/hooks/use_table_listener.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/ui/src/deephaven/ui/hooks/use_table_listener.py b/plugins/ui/src/deephaven/ui/hooks/use_table_listener.py index 8140af24a..06ab38fc8 100644 --- a/plugins/ui/src/deephaven/ui/hooks/use_table_listener.py +++ b/plugins/ui/src/deephaven/ui/hooks/use_table_listener.py @@ -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