Skip to content

Commit

Permalink
fix: Fix conditional use_effect in use_table_listener (#422)
Browse files Browse the repository at this point in the history
fixes #384 

Moves the refreshing table check to ensure the `use_effect` is always
called.
Also added a `use_effect` to `use_table_data` to ensure that when a
table is swapped it updates the resulting data

Here is an example of flipping between ticking and static tables

```
import deephaven.ui as ui
from deephaven.table import Table
from deephaven import time_table, empty_table

empty_t = empty_table(0)
time_t = time_table("PT1S").tail(1)

@ui.component
def test_component():
    empty, set_empty = ui.use_state(True)
    val = ui.use_table_data(empty_t if empty else time_t)
    button = ui.action_button(str(empty), on_press=lambda: set_empty(not empty))

    return [button, str(val)]

data = test_component()
```
  • Loading branch information
jnumainville authored Apr 18, 2024
1 parent 6ead733 commit 5f4f238
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
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

0 comments on commit 5f4f238

Please sign in to comment.