feat: DH-22062: add create_global_state and create_user_state shared state hooks#1324
feat: DH-22062: add create_global_state and create_user_state shared state hooks#1324mofojed wants to merge 6 commits intodeephaven:mainfrom
Conversation
…state hooks Add factory functions for creating shared state that synchronizes across all components using the same store: - create_global_state: shared across all users - create_user_state: scoped per effective user (Enterprise), falls back to anonymous single store on Community Includes unit tests (13 tests), e2e test app + Playwright specs, and documentation with multiple examples.
|
|
||
| @ui.component | ||
| def ui_item_list(): | ||
| items, _, clear = use_items() |
There was a problem hiding this comment.
This is one thing I don't particularly like about Python vs. JS. In JS you could destructure this nicely, e.g. const { items, clear } = useItems()
I think closest thing we could do here is NamedTuple:
from typing import NamedTuple
class ItemsState(NamedTuple):
items: list
add: callable
clear: callable
def use_items():
items, set_items = _use_items()
...
return ItemsState(items=items, add=add, clear=clear)
# Access by name (order-independent):
state = use_items()
state.items
state.clear()
# Or still unpack positionally if you want:
items, add, clear = use_items()... I kind of like that a bit better, but then you have to define the ItemState and such, which is more verbose.
Or DataClass:
from dataclasses import dataclass
@dataclass
class ItemsState:
items: list
add: callable
clear: callableThough you still need to declare ItemState.
Or SimpleNamespace
from types import SimpleNamespace
def use_items():
...
return SimpleNamespace(items=items, add=add, clear=clear)
state = use_items()
state.itemsSeems to be the simplest. But doesn't give you any autocomplete/type suggestions. I don't think it's necessary for an example, and AI will be able to figure it out anyways... but should I put that in the example?
@dsmmcken @jnumainville any thoughts/comments about that?
There was a problem hiding this comment.
Basically I think that should just be left up to the user when building their custom hook, and this example is fine for showing you can build a custom hook. AI should be able to figure it out anyways.
There was a problem hiding this comment.
Yeah I wish there was something like destructuring in Python. I don't think it's worth adding any details on that to an example. I think most people with python are so used to ignoring args with _
That said, I think operator.itemgetter is something that gets close.
from operator import itemgetter
d = {"x": 1, "y": 2, "z": 3}
x, y = itemgetter("x", "y")(d)
There was a problem hiding this comment.
You lose the type hints/inference with itemgetter I think
…_user_state Allow passing a callable as initial_value, invoked once at store creation time (matching the useState lazy-initializer pattern). Add unit and e2e tests for the new behavior.
Add factory functions for creating shared state that synchronizes across
all components using the same store:
to anonymous single store on Community
Includes unit tests (13 tests), e2e test app + Playwright specs,
and documentation with multiple examples.