john_helt
john_helt•3w ago

Handling race conditions

What's the best way to handle race conditions? For instance, if I have a component A that B depends on, but C depends on A and a state that is set in B. How can I ensure that B is updated before C is updated, since the state update seems to hide reactivity
5 Replies
Hall
Hall•3w ago
Someone will reply to you shortly. In the meantime, this might help:
Akshay
Akshay•3w ago
The best thing to do is to minimize state usage 🙂 Recently refactored a user's notebook to remove all on_change handlers and instances of mo.state() — instead, we updated the code to pass UI elements values through the dataflow graph (built-in reactivity) and constructed new objects from existing ones, instead of mutating existing ones. In this case, instead of setting a state in B, can you compute a new value, and have C depend on that new value as well as A? (Barring removing mutations, you'd need to manually insert dependencies across cells, but this can be error prone and difficult to maintain)
john_helt
john_heltOP•3w ago
Thanks! That's what I thought as well. So I've refactored to work with mo.ui.dictionary, and I realized I would love to be able to use nested dictionaries for this though, because I want to pass some metadata about the components as well
Akshay
Akshay•3w ago
mo.ui.dictionary should be able to hold other mo.ui.dictionary objects, if that’s what you mean?
john_helt
john_heltOP•3w ago
not exactly, they may hold other marimo objects, yes, and I didn't know that so thanks for pointing that out 🙂 but metadata could include stuff like "id", "tooltip" strings etc, that may be passed to factory methods, i.e. things that aren't marimo objects

Did you find this page helpful?