Open askvortsov1 opened 2 years ago
- Bonsai.Var, for truly global state, since it acts as a mutable container that can feed into computations via Bonsai.Var.value.
- Bonsai.Dynamic_scope, for "semi-global" state which is shared among some computation subgraph.
Is this an idiomatic way of thinking about Bonsai and global state?
Yeah, that's how I think about it at least!
There's a lot to be improved (e.g. functorizing over a fixed variant of routes), but one of the things I noticed is that my approach has a single global Var.t, whereas the "Bonsai web ui url var" implementation provides functions to create separate instances of url vars. Wouldn't the latter approach mean that changes to one url var instance wouldn't propogate to other instances?
Yes, it's undefined behavior to use two Url_var.t. Because of this, we recommend that people make a single instance at the top of their program, and reference it from other components.
Because of this, we recommend that people make a single instance at the top of their program, and reference it from other components.
Out of curiosity, why provide functions for creating Url_var.t
s in that library instead of only offering that single instance?
’a Url_var.t
Is parameterized on the ‘a that the user provides to parse into and out of, so we can’t have a single instance
Ah my bad, I missed that it's parametric. Makes sense, thank you!
When I was first trying to figure out Bonsai, I drew a lot of parallels to React paired with the Recoil state management library. Recoil adds an orthogonal computation DAG, rooted in shared elements of state called "atoms", which can be consumed by React components. Changes to atoms propogate down the computation graph and selectively trigger React components to re-render.
If the understanding I've come to have through this discussion is accurate, it seems that in Bonsai, the equivalent to atoms is:
Bonsai.Var
, for truly global state, since it acts as a mutable container that can feed into computations viaBonsai.Var.value
.Bonsai.Dynamic_scope
, for "semi-global" state which is shared among some computation subgraph.Is this an idiomatic way of thinking about Bonsai and global state?
As an example of something I'm unsure about, I recently had to build a primitive router/link/url system, where components that depended on the current URL (e.g. fetching a query depending on a path argument) would incrementally recompute as the URL changed. A tl;dr of my implementation:
There's a lot to be improved (e.g. functorizing over a fixed variant of routes), but one of the things I noticed is that my approach has a single global
Var.t
, whereas the "Bonsai web ui url var" implementation provides functions to create separate instances of url vars. Wouldn't the latter approach mean that changes to one url var instance wouldn't propogate to other instances?