DEV Community

Cover image for Angular Tips: Transforming Observables into Signals for Enhanced Reactivity

Angular Tips: Transforming Observables into Signals for Enhanced Reactivity

deji adesoga on February 01, 2024

Unlock Your Angular Signals Knowledge with my New Course: Demystifying Reactivity with Angular Signals Introduction There’s always be...
Collapse
 
pbouillon profile image
Pierre Bouillon • Edited

Angular's new control flows cannot be used like you did, the correct syntax of a @for block is:

@for(product of products; track product.id) {
  <!-- Component code here -->
}
Enter fullscreen mode Exit fullscreen mode

Also, I'm not sure that using toSignal on observables might result in any performance improvements: as you said, it will eagerly subscribe to all observables, leading to more active subscriptions and pipeline being executed.

Collapse
 
desoga profile image
deji adesoga

Thank you for the correction on the syntax, I've made the correction. Cheers!!!

As for the toSignal interop API, there's a potential performance improvement, since we now have access to the different Signal methods and API.

Collapse
 
spock123 profile image
Lars Rye Jeppesen

I can't wait to ditch ZoneJS. It will unlock a lot of awesome improvements (top level await for example), and insane speeds with local change detection. Signals unlock so much good stuff.

I tried removing it (now that Angular 17.1 has an experimental flag that makes it a noOp), but sadly we're relying on Angular/Fire - and that package throws up without ZoneJS.

Great article, cheers

Collapse
 
desoga profile image
deji adesoga

Good to know you're receptive to the concept of Signals. Not many are receptive to it in the community.

As for Angular fire, I hope they provide a Signal compatible update soon. Thank you for reading cheers!

Collapse
 
spock123 profile image
Lars Rye Jeppesen • Edited

I just went zoneless with one of our apps, as the opt-out of zonejs is now possible with Angular 17.1. Small hickups (3rd party stuff) but in general it works well.

I found a workaround for Angular/Fire - just keep zonejs in angular as a polyfill, but opt out in the configuration. This will keep Angular/Fire and Angular/Components happy.

All components have now migrated to input(), it is really beautiful and developer friend as well. Loving how this is going. When we get signal-based components, (hopefully in V18) , performance gains will be on fire, I'm tellin' ya :)

Thread Thread
 
desoga profile image
deji adesoga

I love your enthusiasm towards the adoption of the new updates. Believe me, not many want Signals to get integrated based on most of the interactions I've seen online so far.

I haven't touched Firebase in a long time, but is there a way for you to share the work around. I believe this will be beneficial to many in the community. Perhaps if you could share your integration on Stackblitz.

I'd like to create some content around it. Kindly let me know if you're open to it. Thanks.

Thread Thread
 
spock123 profile image
Lars Rye Jeppesen • Edited

Hi!
It's very simple: do not remove zonejs from the package.json or angular.json of your project.
Just disable zoneJS in the angular app.config file:


import { ɵprovideZonelessChangeDetection as provideZonelessChangeDetection} from '@angular/core';

... in the configuration, add

export const appConfig: ApplicationConfig = {
providers: [
...other providers,
provideZonelessChangeDetection()

Thread Thread
 
desoga profile image
deji adesoga

Awesome. I'll do well to try this out soon. Thank you.

Collapse
 
jangelodev profile image
João Angelo

Deji Adesoga
Nice Tips
Thanks for sharing

Collapse
 
desoga profile image
deji adesoga • Edited

You're welcome! Thank you, João.

Collapse
 
teej107 profile image
teej107

Signals are the hot new Angular feature but I think having the mindset that Observables are too complex and Signals are the fix is a dangerous one. I don't think either is a replacement for the other.

The blanket statement that is relying on Observables in templates reduce potential performance issues is misleading. I'm not sure what Signal specific optimizations Angular is performing under the hood but (to my understanding) a Signal and Observable will only be as performant as their source.

It is also important to know that toSignal may potentially lead to memory leaks depending on if you are using it in a root service or elsewhere since it relies on the injector's context to unsubscribe from the source. This article does a much better job at explaining: Navigating the Nuances of toSignal in Angular: What to Know

Collapse
 
desoga profile image
deji adesoga

Yes Signals have not come replace Observables. However I disagree with the notion that Observables are not complex. They are complex and require a steep learning curve and take time to master, compare to how other frameworks, work.