Do Signals replace RxJS Observables?
Вставка
- Опубліковано 9 чер 2024
- 👉 Post: js2brain.com/blog/rxjs-observ...
Signals recently gained popularity in frontend web development. They are implemented by modern web frameworks like Qwik and SolidJS. Also, Angular adopted them. But what exactly are they, and do they replace RxJS Observables altogether?
🎓 Website: js2brain.com
🎵 Music and SFX: ref.audiio.com/kmx6s7zy
🕐 Timestamps:
00:00 - Intro
00:43 - What is a Signal?
02:43 - What can we do with Signals?
03:32 - Fine-grained Reactivity
03:51 - Comparison
Amazing video showcasing signals and their difference with observables
Btw signals are good for local state management for simple services and RxJS's observables are good for global state management (with NgRx for instance) and HTTP requests
Thanks for your feedback. :)
And I think you are right.
Sometimes the HTTP stuff may be overkill though: When you never intend to abort the request, a simple fetch() with Promise does the job.
What is an example of more complex problems that require RxJS?
One often mentioned example is an auto completion search. I showed another example over here: ua-cam.com/video/PejF6PhDrT4/v-deo.html&si=nD-K_13i8-G0a1Hs
Basically everywhere a lot of subscription and unsubscriptions is going on.
Basically anything that requires syncronuous effects or complex data transformations including integration with old angular api's like router, forms etc.
I think using merely promises and signals together you can do anything without need for observables.
Sure, you don't even need promises and signals. JavaScript was already turing complete pre-ES6. ;)
So a signal is basically an rxjs subject?
It's similar to the BehaviorSubject, I think. What's different is the subscription mechanism with createEffect(), and Signals are more integrated into the web framework for reactive rendering.
@@js2brain No a signal is not quite like an observable because signals are synchronous, meaning they provide a value from the moment they are created, where as you will probably have to wait for the first value from an observable. You can use toSignal() to go from observable to signal but then you have to initialise it with an arbitrary first value, which may have unexpected side effects.
Yeah, as mentioned before, a Signal is more comparable to a BehaviorSubject: initial value, readable, writable, reactive.
Repeat after me: Signals & Observables, not Signals vs Observables