Observables instead of signals #2389
ChocolateLoverRaj
started this conversation in
Ideas
Replies: 1 comment
-
This seems related and interesting: https://github.com/wishawa/async_ui?tab=readme-ov-file |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have an idea. Instead of "signals", what if there were "observables"?
What is an "observable"
I used to use React a lot, and one of the issues I had with React is managing complex data that can change with just
useState
anduseEffect
. One of the issues is handling Promises. Another issue is handling data that comes from IO API calls, such as requesting a camera and listening for events from a WebSocket. So I created a thing which I call an "observable". This is how I coded an observable in TypeScript:https://github.com/ChocolateLoverRaj/observables/blob/15a4ac3ffc5ca22575980ee1a3e7b4af068a0f8c/lib/Observable.ts#L5-L8
With an observable, you can
Differences between an observable and a signal
Observables will remove the need for
useEffect
in React andcreate_effect
in Leptos (I think)Observables can run effects when they become observed. They can run cleanup functions when they stop being observed.
Implementing observables in Rust
I'm not sure about how to do this. Maybe have a
Stream
for when an observable updates and poll the stream if it's observed. I will have to learn more about how futures and polling works in Rust.Observables demo
GitHub: https://github.com/ChocolateLoverRaj/observables
Demo: https://observables.netlify.app/
Why do I think observables have the potential to be better than Signals
Cool things I've done with "observables"
Set
andMap
. You can observe changes to specific keys instead of being notified of every change to the map.Things to think about / explore / consider
How to implement observables?
What is a Rust way of implementing observables? What are the different options? How do they compare in terms of safeness, performance, and ease of use?
Should observables be single-use?
When an observable is updated, should it be destroyed and recreated? Or should an observable continuously provide more updates?
How to deal with observables depending on other observables?
How to manage the memory dropping / not dropping of observables? How do you create observables that depend on a possibly temporary resource such as a camera input or BLE connection?
Explicitly tracked or automatically tracked?
One thing that I don't like about Mobx (and I think this applies to Leptos too) is that you don't explicitly observe changes to things. You just call a
get()
function in Leptos or access a getter in Mobx, which I think can be confusing. People can accidentally call theget()
function outside of a function that is being automatically monitored about what it tracks and things will not be reactive.Beta Was this translation helpful? Give feedback.
All reactions