<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Doguhan Uluca</title>
    <description>The latest articles on Forem by Doguhan Uluca (@duluca).</description>
    <link>https://forem.com/duluca</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1239561%2F660598ec-51d4-4d20-8e28-de43481ccda5.jpeg</url>
      <title>Forem: Doguhan Uluca</title>
      <link>https://forem.com/duluca</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/duluca"/>
    <language>en</language>
    <item>
      <title>How to Rewrite Angular Apps to be Nearly Observable and Subscribe-Free</title>
      <dc:creator>Doguhan Uluca</dc:creator>
      <pubDate>Sun, 21 Jan 2024 18:28:44 +0000</pubDate>
      <link>https://forem.com/duluca/how-to-rewrite-angular-apps-to-be-nearly-observable-and-subscribe-free-53h7</link>
      <guid>https://forem.com/duluca/how-to-rewrite-angular-apps-to-be-nearly-observable-and-subscribe-free-53h7</guid>
      <description>&lt;p&gt;Managing subscriptions is one of the trickiest parts of working with RxJS and Observables in Angular. Even with helpers like the async pipe, takeUntilDestroyed, and auto-unsubscribe, it's easy to run into bugs and memory leaks. The new &lt;a href="https://angular.io/guide/signals"&gt;Signals&lt;/a&gt; feature in Angular aims to solve this problem by introducing a simpler, subscription-less reactive programming model.&lt;/p&gt;

&lt;p&gt;In combination with the powerful new NgRx &lt;a href="https://ngrx.io/guide/signals/signal-store"&gt;SignalStore&lt;/a&gt; state management library, we can rewrite Angular apps to be nearly Observable and subscribe-free. I recently did this with the sample &lt;a href="https://localcastweather.app"&gt;LocalCast Weather&lt;/a&gt; app from my book &lt;a href="https://angularforenterprise.com"&gt;Angular for Enterprise Applications&lt;/a&gt; and I want to share an overview.&lt;/p&gt;

&lt;h2&gt;
  
  
  Replacing Observables with Promises and Signals
&lt;/h2&gt;

&lt;p&gt;The first key is to replace Observable HTTP responses and &lt;a href="https://rxjs.dev/api/index/class/BehaviorSubject"&gt;BehaviorSubject&lt;/a&gt; data anchors with Promise-based APIs instead. NgRx SignalStore uses signals under the hood, which manage their own subscriptions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async getCurrentWeather(searchText, country) {
  return promiseBasedWeatherApiCall(); 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can bridge remaining Observables with helpers like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {toSignal} from '@angular/core'

const searchTerms$ = toSignal(termsObservable)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We also need to set the change detection strategy to &lt;code&gt;OnPush&lt;/code&gt; so components only update when their inputs change:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Component({
  //...
  changeDetection: ChangeDetectionStrategy.OnPush
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Migrating to SignalStore State Management
&lt;/h2&gt;

&lt;p&gt;NgRx SignalStore offers a simple API for state management using signals:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const store = signalStore(
  withState({
    currentWeather: initialWeather,
  }),
  withMethods((
    store, 
    weatherService = inject(WeatherService)
  ) =&amp;gt; ({
    async updateWeather(search) =&amp;gt; {
      const weather = await apiCall(search)
      patchState(store, {currentWeather: weather})
    }
  })
)

@Component({
  template: `
    {{store.currentWeather | json}}
  `
})
export class WeatherComponent {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So rather than loading data in services and pushing changes through subjects, component store methods retrieve the data and update signals. This encapsulates data fetching cleanly without needing intermediary streams. Components simply bind to the relevant state signals.&lt;/p&gt;

&lt;p&gt;The result is much leaner code without requiring explicit subscription handling. We get great ergonomics leveraging signals, while still centralizing state management logic with SignalStore.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ready for Angular's Signal-based Future
&lt;/h2&gt;

&lt;p&gt;While Observables and RxJS offer tremendous declarative reactive power, they also entail complexity. Signals aim to simplify reactive Angular programming. Combined with state management options like NgRx SignalStore, we can eliminate nearly all subscriptions from our apps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I've shared a full SignalStore migration of the LocalCast Weather app from &lt;a href="https://angularforenterprise.com"&gt;Angular for Enterprise Applications&lt;/a&gt; on &lt;a href="https://github.com/duluca/local-weather-app/tree/main/projects/signal-store"&gt;GitHub&lt;/a&gt;. This book covers architectural patterns like this to build robust, real-world Angular applications.&lt;/p&gt;

&lt;p&gt;The updated 3rd edition with Angular 17.1 will be released on January 31st, 2024. Check it out at &lt;a href="https://angularforenterprise.com"&gt;AngularForEnterprise.com&lt;/a&gt; to master advanced Angular development!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>angular</category>
      <category>typescript</category>
      <category>rxjs</category>
    </item>
  </channel>
</rss>
