<?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>DEV Community: earthdmitriy</title>
    <description>The latest articles on DEV Community by earthdmitriy (@earthdmitriy).</description>
    <link>https://dev.to/earthdmitriy</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3818221%2Fde96ee6a-356e-420c-b3ff-a70a291d9e17.jpg</url>
      <title>DEV Community: earthdmitriy</title>
      <link>https://dev.to/earthdmitriy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/earthdmitriy"/>
    <language>en</language>
    <item>
      <title>Stateful Observable: loading, errors and combined data in RxJS without the boilerplate</title>
      <dc:creator>earthdmitriy</dc:creator>
      <pubDate>Sat, 21 Nov 2026 11:22:25 +0000</pubDate>
      <link>https://dev.to/earthdmitriy/stateful-observable-loading-errors-and-combined-data-in-rxjs-without-the-boilerplate-52ak</link>
      <guid>https://dev.to/earthdmitriy/stateful-observable-loading-errors-and-combined-data-in-rxjs-without-the-boilerplate-52ak</guid>
      <description>&lt;p&gt;&lt;strong&gt;Why I stopped writing &lt;code&gt;tap(() =&amp;gt; this.loading = true)&lt;/code&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Every Angular codebase I have worked on has the same few lines somewhere, usually in many places: a &lt;code&gt;loading&lt;/code&gt; flag set from a &lt;code&gt;tap&lt;/code&gt;, an &lt;code&gt;error&lt;/code&gt; field set from a &lt;code&gt;catchError&lt;/code&gt;, and a &lt;code&gt;shareReplay&lt;/code&gt; somewhere in between.&lt;/p&gt;

&lt;p&gt;It works for the happy path. Then someone adds a second async pipe to the template and the request goes out twice. A failure path forgets to reset &lt;code&gt;loading&lt;/code&gt;, and the spinner never goes away. Someone combines this stream with another one, and now there are two &lt;code&gt;loading&lt;/code&gt; flags and two &lt;code&gt;error&lt;/code&gt; fields to merge by hand. A user logs out, and a &lt;code&gt;shareReplay(1)&lt;/code&gt; three services away still replays the previous user's data.&lt;/p&gt;

&lt;p&gt;When I searched GitHub for &lt;code&gt;tap(() =&amp;gt; this.loading&lt;/code&gt;, I got about 12 thousand files. That number has no denominator, but it does show that this pattern is everywhere.&lt;/p&gt;

&lt;p&gt;This article is about &lt;strong&gt;&lt;a href="https://github.com/earthdmitriy/stateful-observable" rel="noopener noreferrer"&gt;Stateful Observable&lt;/a&gt;&lt;/strong&gt; (&lt;a href="https://www.npmjs.com/package/@rx-evo/stateful-observable" rel="noopener noreferrer"&gt;&lt;code&gt;@rx-evo/stateful-observable&lt;/code&gt;&lt;/a&gt; on npm), a small library I built on top of RxJS to replace that boilerplate. A &lt;em&gt;stateful observable&lt;/em&gt; is an observable that carries its own loading, error and inactive state, survives errors, shares itself by default and combines with other stateful observables. On top of that it gives you a dataflow graph that you can recover from any node.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm install @rx-evo/stateful-observable
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The library depends only on RxJS. My examples are written for Angular because that is where the library started, but it isn't tied to Angular. It runs in Node.js too, more on that below.&lt;/p&gt;

&lt;h2&gt;
  
  
  It looks like a small thing. It isn't.
&lt;/h2&gt;

&lt;p&gt;One &lt;code&gt;tap&lt;/code&gt; for a loading flag or one &lt;code&gt;shareReplay&lt;/code&gt; doesn't look like much. The trouble is that they don't come alone. Here is the data part of a component based on a real one from my &lt;a href="https://github.com/earthdmitriy/rx-evo" rel="noopener noreferrer"&gt;demo app&lt;/a&gt;. It shows a client and the client's bucket enriched with a product catalog:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StreamComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;clientId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;required&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;clientId&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;toObservable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;clientLoading&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BehaviorSubject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;bucketLoading&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BehaviorSubject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;productsLoading&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BehaviorSubject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;clientId&lt;/span&gt;$&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nf"&gt;tap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;clientLoading&lt;/span&gt;$&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;switchMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;clientsApi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getClient&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;&lt;span class="n"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;tap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;clientLoading&lt;/span&gt;$&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;bucket&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;clientId&lt;/span&gt;$&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nf"&gt;tap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bucketLoading&lt;/span&gt;$&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;switchMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bucketApi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getClientBucket&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;&lt;span class="n"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;tap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bucketLoading&lt;/span&gt;$&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;productsApi&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;allProducts&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;tap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;productsLoading&lt;/span&gt;$&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;populatedBucket&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;combineLatest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bucket&lt;/span&gt;$&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;products&lt;/span&gt;$&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;prepareBucket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;shareReplay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;combinedLoading&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;combineLatest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bucketLoading&lt;/span&gt;$&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;productsLoading&lt;/span&gt;$&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The business logic here is three API calls and one &lt;code&gt;prepareBucket&lt;/code&gt;. Everything else is plumbing: three mutable loading subjects, five &lt;code&gt;tap&lt;/code&gt;s that write to them, a hand-merged &lt;code&gt;combinedLoading$&lt;/code&gt;, and a &lt;code&gt;shareReplay&lt;/code&gt; on one stream but not on the others. Each of those is a decision someone had to get right.&lt;/p&gt;

&lt;p&gt;And this version has &lt;strong&gt;no error handling at all&lt;/strong&gt;. If the bucket request fails, &lt;code&gt;bucket$&lt;/code&gt; ends, &lt;code&gt;populatedBucket$&lt;/code&gt; ends with it, &lt;code&gt;bucketLoading$&lt;/code&gt; stays &lt;code&gt;true&lt;/code&gt;, and the spinner spins forever. A new &lt;code&gt;clientId&lt;/code&gt; doesn't help, because the stream is already dead. Adding error handling means a &lt;code&gt;catchError&lt;/code&gt; and an error subject per stream, and one more hand-merged &lt;code&gt;combinedError$&lt;/code&gt;. The noise roughly doubles.&lt;/p&gt;

&lt;p&gt;The same component with Stateful Observable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;clientId&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;statefulObservable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;toObservable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;allProducts&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;allProductsToken&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// shared, app-wide&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;clientId&lt;/span&gt;$&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipeValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;switchMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;clientsApi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getClient&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;bucket&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;clientId&lt;/span&gt;$&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipeValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;switchMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bucketApi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getClientBucket&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;populatedBucket&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;combineStatefulObservables&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bucket&lt;/span&gt;$&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;allProducts&lt;/span&gt;$&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;prepareBucket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What's left is the business logic. Loading, errors, recovery after a failure and sharing are all there, and you can read each of them from any node: &lt;code&gt;populatedBucket$.pending$&lt;/code&gt;, &lt;code&gt;populatedBucket$.error$&lt;/code&gt;, &lt;code&gt;populatedBucket$.reload()&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I wanted to fix
&lt;/h2&gt;

&lt;p&gt;I started with a list of problems I kept running into on real projects:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Common RxJS misuses that cost requests.&lt;/strong&gt; The best-known one is a missing &lt;code&gt;shareReplay&lt;/code&gt;, which turns every extra subscriber into another HTTP request.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Loading flags set by hand.&lt;/strong&gt; No more &lt;code&gt;tap(() =&amp;gt; this.loading.set(true))&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Errors that end the stream.&lt;/strong&gt; I wanted error handling and recovery built in, not written by hand for each stream.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Combining several data sources&lt;/strong&gt;, each with its own loading and error state, in a way that behaves correctly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A dataflow graph that costs nothing to build.&lt;/strong&gt; Once 1–4 are solved, that graph is what you get.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I'll go through them in that order.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Shared by default: no accidental extra requests
&lt;/h2&gt;

&lt;p&gt;A plain observable is cold. Every subscription runs the whole pipeline again. If you bind one stream in a template three times (value, pending flag, error), you send three requests. In a measured run with a hand-written state operator, three subscriptions made three requests, and adding &lt;code&gt;shareReplay&lt;/code&gt; brought it down to one.&lt;/p&gt;

&lt;p&gt;Adding &lt;code&gt;shareReplay&lt;/code&gt; is easy to forget. Once you remember it, you have more choices to make: &lt;code&gt;refCount&lt;/code&gt; or not, and how to reset the buffer later (&lt;code&gt;shareReplay&lt;/code&gt; has no reset, which matters at logout).&lt;/p&gt;

&lt;p&gt;A stateful observable is shared by default. Each node caches its raw stream with &lt;code&gt;shareReplay({ bufferSize: 1, refCount: true })&lt;/code&gt;, so all subscribers share one request and the source is released when the last one leaves:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;statefulObservable&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"@rx-evo/stateful-observable"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;statefulObservable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;clientId&lt;/span&gt;$&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;loader&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// value$, pending$ and error$ together: one request&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Data that should live for the whole app is one option away:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;statefulObservable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;loader&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;allProducts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;refCount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// keep it without subscribers&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is still lazy. Nothing happens until something subscribes, so a section rendered under &lt;code&gt;@if&lt;/code&gt; starts its request when it is expanded and releases it when it collapses. You don't write any code for that.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Loading state without &lt;code&gt;tap&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;A stateful observable exposes three derived streams next to the values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;value$&lt;/code&gt;: successful values only&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pending$&lt;/code&gt;: &lt;code&gt;true&lt;/code&gt; while a change was started and has no result yet&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;error$&lt;/code&gt;: errors, as values&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All three are derived from one cached &lt;code&gt;raw$&lt;/code&gt; stream, so they always agree with each other. Nothing in your code writes to them. Loading is derived from the loader, not set from a &lt;code&gt;tap&lt;/code&gt;. In a template, using the &lt;code&gt;client$&lt;/code&gt; from the component above:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;[class.loading]&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"client$.pending$ | async"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  @if (client$.error$ | async; as error) {
    &lt;span class="nt"&gt;&amp;lt;app-error&lt;/span&gt; &lt;span class="na"&gt;[error]&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"error"&lt;/span&gt; &lt;span class="na"&gt;(retry)&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"client$.reload()"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  } @else {
    &lt;span class="nt"&gt;&amp;lt;app-client&lt;/span&gt; &lt;span class="na"&gt;[client]&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"client$.value$ | async"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  }
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two details took me several attempts to get right:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The previous value stays on screen while new data loads.&lt;/strong&gt; A new &lt;code&gt;pending&lt;/code&gt; event does not clear the last value. Only a new value or a new error replaces it. Many "wrap the response" helpers emit &lt;code&gt;{ loading: true, value: null }&lt;/code&gt; on every request, which wipes the content and makes the layout jump. I didn't want that.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Loading is emitted only for work that is actually asynchronous.&lt;/strong&gt; A cache hit or a synchronous result arrives without a loading event, so &lt;code&gt;pending$&lt;/code&gt; doesn't flicker.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pipeValue&lt;/code&gt; and &lt;code&gt;pipeError&lt;/code&gt; take ordinary RxJS operators. You don't need special operators that know about wrapped values or &lt;code&gt;if (isLoading(x)) return x&lt;/code&gt; branches. &lt;code&gt;switchMap&lt;/code&gt; switches, &lt;code&gt;mergeMap&lt;/code&gt; runs everything in parallel, and &lt;code&gt;scan&lt;/code&gt; and &lt;code&gt;distinctUntilChanged&lt;/code&gt; keep their state as they do in plain RxJS.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why three pipes instead of one
&lt;/h3&gt;

&lt;p&gt;A single &lt;code&gt;pipe&lt;/code&gt; over wrapped events would make every operator deal with the state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// one pipe for everything&lt;/span&gt;
&lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;mergeMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;isLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// bypass&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;isError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// bypass&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// and concatMap in that place behaves incorrectly&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The other way out is a set of helper operators (&lt;code&gt;onlyValue(...)&lt;/code&gt; and friends). That's one more API to learn, and it fits badly with operators that weren't written for it. Separate pipes keep ordinary operators working, including higher-order ones:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;statefulObservable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;formValue&lt;/span&gt;$&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipeValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;switchMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// values only&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;toTypedError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                 &lt;span class="c1"&gt;// errors only&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;takeUntilDestroyed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                  &lt;span class="c1"&gt;// the whole stream&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Plain &lt;code&gt;pipe&lt;/code&gt; is for operators that apply to the whole stream: &lt;code&gt;takeUntilDestroyed&lt;/code&gt;, &lt;code&gt;shareReplay&lt;/code&gt;, &lt;code&gt;debounceTime&lt;/code&gt;. And because &lt;code&gt;takeUntilDestroyed&lt;/code&gt; goes in through &lt;code&gt;pipe&lt;/code&gt;, the library doesn't need Angular to clean up after itself. The full reasoning is in the &lt;a href="https://github.com/earthdmitriy/stateful-observable/blob/main/docs/adr.md#chaining-and-pure-rxjs-interoperability" rel="noopener noreferrer"&gt;ADR&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Errors are data, and the stream survives them
&lt;/h2&gt;

&lt;p&gt;In RxJS an error ends the stream. In a &lt;code&gt;combineLatest&lt;/code&gt;, one failing branch ends the combined stream and everything below it. Recovering takes &lt;code&gt;catchError&lt;/code&gt; in the right place, at every level.&lt;/p&gt;

&lt;p&gt;In a stateful observable, an error is one of the states. An error thrown in any pipe is caught at that node and sent on as an error event, and the stream goes on to handle the next input:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;statefulObservable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;$&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipeValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;switchMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// fails for one query...&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// ...and still delivers the result for the next one&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Errors can be typed. You narrow them once at the source, and the type carries through later pipes and combinations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;statefulObservable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;loader&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;allProducts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"allProducts"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;toProductsError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// error$ is now Observable&amp;lt;ProductsError&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every node has &lt;code&gt;reload()&lt;/code&gt;, so a retry button needs no extra Subject:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;(click)&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"client$.reload()"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Retry&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Errors are also hard to lose. If nothing subscribes to &lt;code&gt;error$&lt;/code&gt;, the failure is logged to the console with the node's name and its position in the chain (&lt;code&gt;Unhandled error in statefulObservable 'clientStream #1'&lt;/code&gt;). When you subscribe, the message goes away. A stateful observable doesn't swallow errors without a trace.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Combining data sources properly
&lt;/h2&gt;

&lt;p&gt;This is the part that tends to go wrong. A bucket enriched with a product catalog looks like this in plain RxJS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;populatedBucket&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;combineLatest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;bucket&lt;/span&gt;$&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt;$&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;prepareBucket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's fine until you need a spinner and an error view. Then you need a rule: is the combination pending if &lt;em&gt;any&lt;/em&gt; source is pending? Which error wins? Which source failed? Each team writes this rule a bit differently in each combination.&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;combineStatefulObservables&lt;/code&gt; the rule is part of the library:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;combineStatefulObservables&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"@rx-evo/stateful-observable"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;populatedBucket&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;combineStatefulObservables&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;bucket&lt;/span&gt;$&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt;$&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;prepareBucket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Values come in as a typed tuple (&lt;code&gt;[Bucket, Product[]]&lt;/code&gt;), inferred from the sources.&lt;/li&gt;
&lt;li&gt;The error is a tuple too, with one entry per source: &lt;code&gt;[BucketError | false, ProductsError | false]&lt;/code&gt;. You can see exactly which source failed.&lt;/li&gt;
&lt;li&gt;The priority is the same at every combination: &lt;strong&gt;inactive, then pending, then error, then value.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;An error is emitted only once every source has settled. An error event therefore describes the full state. It is never partial and never extended later, so "retry what failed" reaches everything that failed.&lt;/li&gt;
&lt;li&gt;While a source is pending, the combination emits pending instead of combining that source's old value with another source's new one. With asynchronous sources this removes the outdated combinations that plain &lt;code&gt;combineLatest&lt;/code&gt; briefly emits.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The cost of that rule: an error waits for the slowest source in the same combination. The reasoning and the alternatives I considered are in the &lt;a href="https://github.com/earthdmitriy/stateful-observable/blob/main/docs/adr.md#priority-of-states-in-a-combination" rel="noopener noreferrer"&gt;ADR&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Combinations nest, and a combined node is a stateful observable like any other. You can pipe it, combine it again and reload it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Chained requests: where plain RxJS gets tricky
&lt;/h3&gt;

&lt;p&gt;A very common case: load an entity by id, then load a related entity by its foreign key, and show both together.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;entity&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;$&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;switchMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchEntity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nf"&gt;shareReplay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;relatedEntity&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;entity&lt;/span&gt;$&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;switchMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;foreignKey&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchRelatedEntity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;foreignKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;combined&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;combineLatest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;entity&lt;/span&gt;$&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;relatedEntity&lt;/span&gt;$&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is where loading tracking and stale data get difficult:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Stale mix.&lt;/strong&gt; When the id changes, &lt;code&gt;entity$&lt;/code&gt; gets the new entity first, while &lt;code&gt;relatedEntity$&lt;/code&gt; still holds the related entity of the old one. &lt;code&gt;combineLatest&lt;/code&gt; emits that pair: the new entity with the old related data, a combination that never existed. In a quick run with two delayed requests and the id going from 1 to 2, the vanilla version emitted &lt;code&gt;1/10&lt;/code&gt;, then &lt;strong&gt;&lt;code&gt;2/10&lt;/code&gt;&lt;/strong&gt;, then &lt;code&gt;2/20&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Loading.&lt;/strong&gt; The related entity is out of date as soon as the id changes, not when the new entity arrives. So a hand-written &lt;code&gt;relatedLoading&lt;/code&gt; flag has to start on the id, not on its own input. Get that wrong, and the old related data is shown as current while the new entity loads.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Errors.&lt;/strong&gt; If either request fails, the whole chain ends, as in the component at the start of this article.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The same with Stateful Observable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;statefulObservable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;valueChanges&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;entity&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;$&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipeValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;switchMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchEntity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;relatedEntity&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;entity&lt;/span&gt;$&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipeValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;switchMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;foreignKey&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchRelatedEntity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;foreignKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;combined&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;combineStatefulObservables&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;entity&lt;/span&gt;$&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;relatedEntity&lt;/span&gt;$&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;entity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;related&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;entity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;related&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pending travels down the chain, so &lt;code&gt;relatedEntity$&lt;/code&gt; is pending as soon as &lt;code&gt;entity$&lt;/code&gt; is, and the combination emits pending instead of pairing new data with old. In the same run, this version emitted &lt;code&gt;1/10&lt;/code&gt;, then pending, then &lt;code&gt;2/20&lt;/code&gt;. The mix was never emitted. Each step has its own &lt;code&gt;pending$&lt;/code&gt; and &lt;code&gt;error$&lt;/code&gt;, a failure in the second request leaves the first one on screen, and &lt;code&gt;combined$.reload()&lt;/code&gt; reloads the chain.&lt;/p&gt;

&lt;h3&gt;
  
  
  Split, chain, combine
&lt;/h3&gt;

&lt;p&gt;The library is built for the shapes real dataflows take. A stream gets &lt;strong&gt;split&lt;/strong&gt; into several branches. A branch &lt;strong&gt;chains&lt;/strong&gt; requests, each one depending on the previous. Branches get &lt;strong&gt;combined&lt;/strong&gt; again, either from separate roots (a client's bucket with the app-wide catalog) or by recombining a stream with something derived from it (an entity with its related entity). Each of those shapes has its own edge cases: stale mixes, loading that starts at the wrong moment, errors that end everything. In a stateful observable they're handled the same way at every node. So a reliable dataflow graph takes no extra effort.&lt;/p&gt;

&lt;p&gt;In most apps I've seen, this graph is never built. Handling it properly means a lot of boilerplate and edge cases, so the usual shortcut is to merge everything together: one &lt;code&gt;forkJoin&lt;/code&gt;, one big request, one store update, one spinner for the whole page. It works, but the cost is real. Nothing renders until the slowest request answers, so there's no progressive loading. One failed endpoint takes down the whole page, so there's no graceful degradation. And the error handling is a single "something went wrong" with no way to retry only the part that failed.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. The result: a dataflow graph you get for free
&lt;/h2&gt;

&lt;p&gt;Go back to the stateful version of the component at the start of this article. The root token behind &lt;code&gt;allProducts$&lt;/code&gt; is one more declaration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;allProductsToken&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;InjectionToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"allProducts"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;providedIn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"root"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;factory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ProductsApiService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;statefulObservable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;loader&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;allProducts&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"allProducts"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;refCount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// app-wide data: keep it without subscribers&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Put together, the pieces form a graph:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzu1hcjl9qf57iept7puw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzu1hcjl9qf57iept7puw.png" alt="Stateful Observable dataflow graph: clientId feeds clientId$, which feeds client$ and bucket$; bucket$ and the app-wide allProducts$ combine into populatedBucket$; client$ and populatedBucket$ feed the template" width="800" height="310"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every node has the same handle: &lt;code&gt;value$&lt;/code&gt;, &lt;code&gt;pending$&lt;/code&gt;, &lt;code&gt;error$&lt;/code&gt;, &lt;code&gt;reload()&lt;/code&gt;. That brings a few things with it:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Graceful degradation follows from the shape of the graph.&lt;/strong&gt; Each piece of UI is a leaf that depends only on the sources it needs. A failure blocks only the nodes that depend on it, and every other leaf still renders. If the catalog endpoint is down, the client header still shows.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Progressive rendering is the default.&lt;/strong&gt; Streams are lazy and each region subscribes to its own sources, so each region renders as soon as those sources answer, without waiting for the rest of the page.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Recovery works from any handle.&lt;/strong&gt; A template often holds only the final node. &lt;code&gt;reload()&lt;/code&gt; on that node reloads every source behind it, through every layer of pipes and combinations. If you add a source to the combination later, the retry button doesn't need to change. You can narrow it down:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="n"&gt;populatedBucket&lt;/span&gt;$&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                                    &lt;span class="c1"&gt;// everything behind it&lt;/span&gt;
&lt;span class="n"&gt;populatedBucket&lt;/span&gt;$&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;onlyFailed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                &lt;span class="c1"&gt;// only what failed&lt;/span&gt;
&lt;span class="n"&gt;populatedBucket&lt;/span&gt;$&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"allProducts"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// by name or meta&lt;/span&gt;
&lt;span class="n"&gt;populatedBucket&lt;/span&gt;$&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                       &lt;span class="c1"&gt;// by position, as in the error tuple&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You don't need a global registry or query keys, and the caller doesn't need to know what the sources are.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The dataflow can be read straight from the code.&lt;/strong&gt; Derived nodes have no write API, so a value has exactly one producer, and you find where it comes from by following references. The diagram above is redrawn from what &lt;a href="https://github.com/earthdmitriy/angular-mermaider" rel="noopener noreferrer"&gt;angular-mermaider&lt;/a&gt; produced for the component. It's a static analyser I wrote as a proof of concept, and it draws this graph without running the app. That only works because the graph is pure.&lt;/p&gt;

&lt;p&gt;None of this needs plumbing per layer. In plain RxJS you can build the same thing, but you build it yourself: &lt;code&gt;{ data$, reload }&lt;/code&gt; pairs from every layer, a combining helper with a rule, a reload method that knows every source. A stateful observable gives every node all of that.&lt;/p&gt;

&lt;p&gt;On top of the core, the library has a few tools for real-app problems: &lt;code&gt;active&lt;/code&gt; to drop user data at logout, a result cache per input, and factories for lookups by id, with batching. &lt;a href="https://github.com/earthdmitriy/stateful-observable/blob/main/docs/articles/four-data-layers.md" rel="noopener noreferrer"&gt;Part 2&lt;/a&gt; shows them at work on a complex page. The &lt;a href="https://github.com/earthdmitriy/stateful-observable/blob/main/docs/QoL.md" rel="noopener noreferrer"&gt;quality-of-life guide&lt;/a&gt; describes them in detail.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why not signals, resources or a store?
&lt;/h2&gt;

&lt;p&gt;I tried signals first. Signals are synchronous by nature, which makes laziness hard. In my experiments the requests kept running after the UI that needed them was hidden. By default, Angular's &lt;code&gt;resource&lt;/code&gt; resets its value when a new request starts, so large content jumps; keeping it takes a documented extra pattern. Combining several resources needs your own helper, and that helper again has to decide which status wins and which source failed. My notes from those attempts are in the &lt;a href="https://github.com/earthdmitriy/stateful-observable/blob/main/docs/adr.md" rel="noopener noreferrer"&gt;ADR&lt;/a&gt;. Signals do work well as the presentation layer on top of stateful streams (&lt;code&gt;toSignal(stream.value$)&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Stores solve a different problem: shared &lt;em&gt;mutable&lt;/em&gt; state. I deliberately made Stateful Observable without a write path. A value changes only because an input went through the loader. My premise is that a frontend owns no source of truth except user input, and everything else is a projection of an API. If your problem really needs many writers patching shared state, a store is the right tool. A stateful observable isn't.&lt;/p&gt;

&lt;h3&gt;
  
  
  Looks like a resource, doesn't it?
&lt;/h3&gt;

&lt;p&gt;Yes and no. A resource is fine in a component that projects a single data source: one read, owned by that component, with loading, error and reload built in. It starts to need workarounds when you combine several sources, or when the data is held in a shared service: a cache shared between sections, invalidation after a write, cancelling a read nobody waits for, dropping user data at logout. In the contest in part 2, the resources entry needed per-entity registries and a version counter for invalidation, and it still failed 14 scenarios.&lt;/p&gt;

&lt;p&gt;A stateful observable is one solution that works the same way everywhere: in a component, in a shared service or token, and in a combination of both. The contest entry in part 2 used the same handle for all of them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Not only Angular
&lt;/h2&gt;

&lt;p&gt;The library started in Angular apps, but it depends only on RxJS. It has no framework code, no browser globals and no timers of its own. The Angular parts in my examples (&lt;code&gt;toObservable&lt;/code&gt;, &lt;code&gt;inject&lt;/code&gt;, the async pipe) are glue on the consumer side.&lt;/p&gt;

&lt;p&gt;So it runs in Node.js too. My other project, &lt;a href="https://github.com/earthdmitriy/langflower" rel="noopener noreferrer"&gt;langflower&lt;/a&gt;, is a local visual workflow tool: a node graph that runs agents, tools, checks and human gates on a project folder. Its runtime, compiler and node SDK use Stateful Observable under the hood. That's a good fit, because an agentic workflow is the same problem as a page full of data: a graph of steps where each step can be in progress, fail or succeed, where a failure should stay local, and where a retry should rerun only the part that needs it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Limitations
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;It's young, but not new.&lt;/strong&gt; Version 0.8.0, one maintainer, a small user base. It didn't appear overnight, though. The first version went to npm in October 2025, and 16 releases have followed since. The design grew out of earlier iterations described in the &lt;a href="https://github.com/earthdmitriy/stateful-observable/blob/main/docs/adr.md" rel="noopener noreferrer"&gt;ADR&lt;/a&gt; (a wrapping operator, then a small store), and today it has a suite of 733 tests. It's also a thin layer over RxJS, so what you rely on underneath (&lt;code&gt;switchMap&lt;/code&gt;, &lt;code&gt;shareReplay&lt;/code&gt;, &lt;code&gt;combineLatest&lt;/code&gt;) is mature.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;There's no write path, by design.&lt;/strong&gt; You can't patch cached data. Saves are requests whose result you show (through the input or a separate save stream). Optimistic UI is possible as an overlay you write yourself.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Two things to know about pipes.&lt;/strong&gt; Both come with features plain RxJS doesn't have. Pending can't tell a value dropped by &lt;code&gt;filter&lt;/code&gt; &lt;em&gt;inside&lt;/em&gt; a pipe from a request still running, so the node stays pending; validation belongs on the input. And the stream survives an error, where plain RxJS would stop, but operator state such as &lt;code&gt;scan&lt;/code&gt; starts over after it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No runtime devtools yet.&lt;/strong&gt; A prototype exists: an operator that sends every event to the Redux DevTools extension. It isn't shipped, because a proper integration has to be on in development builds and absent from production, and that switch ties it to each framework's build tools. The core stays framework-agnostic, so devtools would mean a separate package per framework. Until then, the graph topology makes tracing cheap: a &lt;code&gt;log&lt;/code&gt; callback on a root traces every node derived from it, and nodes carry names and &lt;code&gt;meta&lt;/code&gt;. If you'd use devtools, &lt;a href="https://github.com/earthdmitriy/stateful-observable/issues" rel="noopener noreferrer"&gt;tell me in an issue&lt;/a&gt;: that decides whether the packages get built.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No time-based cache policy out of the box.&lt;/strong&gt; A TTL or polling is composed from RxJS operators and &lt;code&gt;reload()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Events are plain RxJS work.&lt;/strong&gt; RxJS is good at managing events as it is: debounce, merges, sequences, time windows. The library doesn't replace that. It closes the gap that RxJS leaves for async data that can fail.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Recipes for people and agents
&lt;/h2&gt;

&lt;p&gt;The library ships &lt;a href="https://github.com/earthdmitriy/stateful-observable/blob/main/docs/recipes.md" rel="noopener noreferrer"&gt;recipes&lt;/a&gt;, a &lt;a href="https://github.com/earthdmitriy/stateful-observable/blob/main/docs/QoL.md" rel="noopener noreferrer"&gt;quality-of-life guide&lt;/a&gt; and &lt;a href="https://github.com/earthdmitriy/stateful-observable/blob/main/docs/adr.md" rel="noopener noreferrer"&gt;design decisions&lt;/a&gt;, written for both people and AI agents: where a source lives, how to invalidate the cached values of a subtree (for example on logout), how to batch a lookup, how to refresh what a write changed, and why the library works the way it does. In a check on the page from part 2, an agent given the recipes built a slice of it (a session, a batched lookup per row, a polled section, a write that refreshes another view) and followed the intended pattern on every point, with no steering.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm install @rx-evo/stateful-observable
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;statefulObservable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;combineStatefulObservables&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"@rx-evo/stateful-observable"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;$ &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;statefulObservable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt;$&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;loader&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;user&lt;/span&gt;$&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;pending&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isLoading&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"loading:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;isLoading&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"user:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"error:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Source and docs: &lt;a href="https://github.com/earthdmitriy/stateful-observable" rel="noopener noreferrer"&gt;github.com/earthdmitriy/stateful-observable&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;npm: &lt;a href="https://www.npmjs.com/package/@rx-evo/stateful-observable" rel="noopener noreferrer"&gt;@rx-evo/stateful-observable&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Recipes: &lt;a href="https://github.com/earthdmitriy/stateful-observable/blob/main/docs/recipes.md" rel="noopener noreferrer"&gt;docs/recipes.md&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Quality-of-life guide: &lt;a href="https://github.com/earthdmitriy/stateful-observable/blob/main/docs/QoL.md" rel="noopener noreferrer"&gt;docs/QoL.md&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Design decisions: &lt;a href="https://github.com/earthdmitriy/stateful-observable/blob/main/docs/adr.md" rel="noopener noreferrer"&gt;docs/adr.md&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Does it hold up on a real page?&lt;/strong&gt; In &lt;a href="https://github.com/earthdmitriy/stateful-observable/blob/main/docs/articles/four-data-layers.md" rel="noopener noreferrer"&gt;part 2&lt;/a&gt;, I build one busy page four ways (plain RxJS, Stateful Observable, Angular signals with resources, NgRx SignalStore), judge all of them with the same test suite, and count the code.&lt;/p&gt;

&lt;p&gt;If you've written &lt;code&gt;tap(() =&amp;gt; this.loading = true)&lt;/code&gt; more than once this year, try a stateful observable on one screen. If it saved you some plumbing, star the &lt;a href="https://github.com/earthdmitriy/stateful-observable" rel="noopener noreferrer"&gt;repo&lt;/a&gt;. If it didn't, &lt;a href="https://github.com/earthdmitriy/stateful-observable/issues" rel="noopener noreferrer"&gt;open an issue&lt;/a&gt; and tell me why.&lt;/p&gt;

</description>
      <category>rxjs</category>
      <category>angular</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Functional programming in Angular</title>
      <dc:creator>earthdmitriy</dc:creator>
      <pubDate>Wed, 11 Mar 2026 17:06:33 +0000</pubDate>
      <link>https://dev.to/earthdmitriy/functional-programming-in-angular-57f8</link>
      <guid>https://dev.to/earthdmitriy/functional-programming-in-angular-57f8</guid>
      <description>&lt;h2&gt;
  
  
  Chapter 1: The Problem with Mutable State in Angular
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Introduction: Why Functional Programming?
&lt;/h3&gt;

&lt;p&gt;Imagine joining a new project. You open the codebase, and this is what greets you:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;bucket&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Bucket&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;ready&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;displayData&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ClientWithBucket&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A class of 5000 lines. Fields declared with the &lt;code&gt;!&lt;/code&gt; assertion, promising TypeScript that they'll be initialized later — but when? Before you can safely read &lt;code&gt;displayData&lt;/code&gt;, you must trace through the component's lifecycle to discover &lt;strong&gt;when&lt;/strong&gt; each field gets its value. Before you write to any field, you must check what reads from it and which dependent methods need to be called afterward.&lt;/p&gt;

&lt;p&gt;This is the reality of traditional OOP in Angular. And it comes with a hidden cognitive cost.&lt;/p&gt;

&lt;h3&gt;
  
  
  1.1 The Cognitive Load Problem
&lt;/h3&gt;

&lt;p&gt;Psychologists have discovered that humans can hold only &lt;strong&gt;7±2&lt;/strong&gt; distinct contexts in working memory at once. Every mutable variable adds one context. Every read of that variable adds another. Soon, your mental buffer overflows, and your brain starts taking shortcuts — implicit optimizations that lead to assumptions rather than verified facts.&lt;/p&gt;

&lt;p&gt;This leads to a dangerous development pattern: &lt;em&gt;"I'll change this and see what happens."&lt;/em&gt; The optimistic scenario gets implemented, but edge cases — unusual combinations of permissions or rare entity states — slip through.&lt;/p&gt;

&lt;p&gt;What about tests? The monsters that cause production issues are rarely covered by tests. And covering them is incredibly difficult, because many error states arise from sequences of method calls. The code needed to set up the state can be many times larger than the code that actually checks the state.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"I'll fix it later if something goes wrong"&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  1.2 The Core Problem: Implicit Dependencies
&lt;/h3&gt;

&lt;p&gt;The fundamental issue is that dependencies between fields are &lt;strong&gt;implicit&lt;/strong&gt;. They exist in time, but they're not expressed in code. You have to hold the entire component lifecycle in your head to understand when data becomes available and how changes propagate.&lt;/p&gt;

&lt;p&gt;What we really need is a way to &lt;strong&gt;declaratively describe all dependencies in time&lt;/strong&gt; — to make the data flow explicit, visible, and verifiable by the compiler.&lt;/p&gt;

&lt;h3&gt;
  
  
  1.3 LLM Coding Assistants Have a Similar Problem
&lt;/h3&gt;

&lt;p&gt;There’s an emerging reason to adopt these principles that goes beyond human cognition: &lt;strong&gt;LLM coding assistants (like GitHub Copilot, Cursor, and others) struggle with the same implicit dependencies that confuse humans.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you ask an LLM to modify a piece of code, it needs to understand the context — what variables exist, how they relate, and what effects changes might have. Every mutable field, every implicit dependency, every piece of state that can change in unexpected ways forces the LLM to load more tokens into its context window.&lt;/p&gt;

&lt;h3&gt;
  
  
  1.4 The Context Window Problem
&lt;/h3&gt;

&lt;p&gt;LLMs have limited context windows. While modern models boast impressive token limits, every line of code you make them read is a line they can’t use for reasoning about your actual problem. When faced with a 5000 - line class full of mutable state and implicit dependencies, the LLM must:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Understand when each field is initialized&lt;/li&gt;
&lt;li&gt;Track which methods read from which fields&lt;/li&gt;
&lt;li&gt;Infer the implicit data flow that the code doesn’t declare&lt;/li&gt;
&lt;li&gt;Hold all these relationships in its limited attention mechanism&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is exactly the 7±2 problem, scaled up. The LLM’s “working memory” fills with the same clutter that overloads human cognition.&lt;/p&gt;

&lt;h3&gt;
  
  
  1.5 More Context = More Hallucinations
&lt;/h3&gt;

&lt;p&gt;Just as humans start taking shortcuts when their mental buffer overflows, LLMs start hallucinating. When the context becomes too complex, they:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Invent variables that don’t exist&lt;/li&gt;
&lt;li&gt;Make incorrect assumptions about data flow&lt;/li&gt;
&lt;li&gt;Suggest changes that break implicit dependencies&lt;/li&gt;
&lt;li&gt;Generate code that looks plausible but fails at runtime&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These hallucinations aren’t malice — they’re the model’s attempt to cope with ambiguity. When the true data flow isn’t explicit, the LLM fills in the gaps with its best guess. And in a mutable, imperative codebase, those guesses are often wrong.&lt;/p&gt;

&lt;h3&gt;
  
  
  1.6 The Threshold of Unfixability
&lt;/h3&gt;

&lt;p&gt;There comes a point where the complexity exceeds not just human comprehension, but the LLM’s ability to generate correct fixes. After a certain threshold, LLMs become &lt;strong&gt;unable to fix the code&lt;/strong&gt; — not because they lack capability, but because the context required to understand the problem exceeds their context window or reasoning capacity.&lt;/p&gt;

&lt;p&gt;This manifests as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The model making the same mistake repeatedly&lt;/li&gt;
&lt;li&gt;Fixing one bug but introducing two more&lt;/li&gt;
&lt;li&gt;Giving up and suggesting a complete rewrite&lt;/li&gt;
&lt;li&gt;Producing increasingly nonsensical code&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  1.7 The Goal: Compile-Time Error Detection
&lt;/h3&gt;

&lt;p&gt;This brings us to the practical goal of applying functional programming principles in Angular:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Using FP principles and TypeScript to catch obviously incorrect code at compile time.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Why is this so important? Because compile-time errors are the &lt;strong&gt;cheapest&lt;/strong&gt; bugs to fix:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Compile-time error&lt;/strong&gt;: nearly free&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unit test failure&lt;/strong&gt;: tens of minutes&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;QA finding&lt;/strong&gt;: hours&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Production incident&lt;/strong&gt;: days&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Compiler time is dramatically cheaper than programmer time — or even AI agent time. And with tools like &lt;code&gt;tsgo&lt;/code&gt; on the horizon, the compiler is only getting faster and smarter.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"You won't have compilation errors without compilation"&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is the foundation: to leverage the compiler as a bug-detection tool, we must give it enough information to do its job. That means strict typing, explicit contracts, and pure transformations — allowing the compiler to verify that our code is correct before it ever runs.&lt;/p&gt;

&lt;p&gt;In traditional object-oriented programming, controlling state through &lt;code&gt;this.*&lt;/code&gt; mutations is so deeply ingrained that it's rarely questioned. But this practice is fundamentally at odds with compiler-assisted reliability. In functional programming languages, developers often observe that when code compiles, it works — not because functional programmers are smarter, but because the compiler has enough information to prove correctness. The question is: how do we achieve this level of confidence in Angular? The answer lies in a paradigm shift: treating components not as containers of mutable state, but as pipelines that transform immutable data flows. The rest of this article shows exactly how to make that shift.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqtvm4sfrxyh0zpskxdei.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqtvm4sfrxyh0zpskxdei.png" alt=" " width="800" height="532"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Chapter 2: Building a Foundation with Strict Types and Contracts
&lt;/h2&gt;

&lt;h3&gt;
  
  
  2.1 First Principle: Strict Typing
&lt;/h3&gt;

&lt;p&gt;If you can't enable TypeScript's &lt;code&gt;strict&lt;/code&gt; mode immediately, at minimum enforce these two rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;No &lt;code&gt;any&lt;/code&gt; types&lt;/strong&gt; – ever&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Strict null checks&lt;/strong&gt; – so &lt;code&gt;undefined&lt;/code&gt; and &lt;code&gt;null&lt;/code&gt; must be handled explicitly&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These alone eliminate an entire category of runtime errors. But typing is just the beginning — we need to ensure that the data flowing through our application is not just typed, but &lt;strong&gt;correct by construction&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  2.2 Second Principle: Contracts
&lt;/h3&gt;

&lt;p&gt;The second pillar of compile-time safety is establishing clear &lt;strong&gt;contracts&lt;/strong&gt; — especially between the frontend and backend.&lt;/p&gt;

&lt;p&gt;What's the point of using TypeScript if data from the server arrives as "who knows what"?&lt;/p&gt;

&lt;h4&gt;
  
  
  The Approach
&lt;/h4&gt;

&lt;p&gt;Key things to build robust contracts are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Identify nullable variables&lt;/strong&gt; – know what can and cannot be &lt;code&gt;null&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Define all types explicitly&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;union types&lt;/strong&gt; where appropriate&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;enums&lt;/strong&gt; for fixed sets of values&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No dynamic types&lt;/strong&gt; – if you encounter them, use type guards&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;If your backend uses a strictly typed language – consider yourself lucky
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ClientStatus&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;active&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;deleted&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;all&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Client&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ClientStatus&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;birthDate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ISO date from backend&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2.3 Where to Place Contracts
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Define contracts in a separate layer&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;API services&lt;/strong&gt; should contain only calls to the backend

&lt;ul&gt;
&lt;li&gt;Do not mix business logic with API calls&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;The goal is to use TypeScript's type system to prevent:

&lt;ul&gt;
&lt;li&gt;Receiving invalid data&lt;/li&gt;
&lt;li&gt;Making invalid requests&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Keep data types in the same file as the API service&lt;/li&gt;

&lt;li&gt;Make all entity fields &lt;code&gt;readonly&lt;/code&gt; where possible using TypeScript utilities&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  2.4 Should You Create Your Own Frontend Models?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Without significant reasons – no.&lt;/strong&gt; The source of truth is on the backend.&lt;/p&gt;

&lt;p&gt;Creating your own models introduces unnecessary mappings and additional layers of complexity:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ClientDto&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;birth_date&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// backend format&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="c1"&gt;// ❌ Avoid when possible&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Client&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;birthDate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// frontend format&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dto&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ClientDto&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;dto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;dto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;dto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;birthDate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;birth_date&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every mapping is a potential source of bugs and maintenance overhead.&lt;/p&gt;

&lt;p&gt;Many articles consider haveing separate model at frontend as good practice. Agrumention it like&lt;/p&gt;

&lt;h4&gt;
  
  
  Expectation
&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;if contract changes we'll need to update only mapping while keepeing out business logic intact&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Reality
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;At start we had DTO mapped to Model and used by Consumer&lt;/li&gt;
&lt;li&gt;Then DTO has been changed and, instead of &lt;code&gt;DTO_v2 =&amp;gt; Model =&amp;gt; Consumer&lt;/code&gt;, we got &lt;code&gt;DTO_v2 =&amp;gt; Model_v2 =&amp;gt; Consumer_v2&lt;/code&gt; with additional mapping &lt;code&gt;Model_v2 =&amp;gt; Model&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;DTO has been changed again and new chain appeared &lt;code&gt;DTO_v3 =&amp;gt; Model_v3 =&amp;gt; Consumer_v3&lt;/code&gt; with additional mapping &lt;code&gt;Model_v3 =&amp;gt; Model_v2 =&amp;gt; Model&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The expectation that separate frontend models protect you from contract changes is a pleasant myth. In reality, you don't get DTO_v2 → Model → Consumer — you get DTO_v2 → Model_v2 → Consumer_v2, plus a new mapping from Model_v2 → Model to preserve backward compatibility. Repeat this a few times and you have a tangled mess of adapters, transformers, and version adapters that all need maintenance. The source of truth was always the backend contract. By mapping directly from DTO to what the UI needs — with pure functions and no intermediate models — you eliminate an entire layer of complexity. Trust the contract, not the additional abstraction.&lt;/p&gt;

&lt;h4&gt;
  
  
  A Better Approach: Inversion of Control
&lt;/h4&gt;

&lt;p&gt;If you want to avoid depending on specific backend endpoints, think about it differently — by analogy with the &lt;strong&gt;Dependency Inversion Principle&lt;/strong&gt; from SOLID:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Before&lt;/strong&gt;: Class A depends on Class B&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;After&lt;/strong&gt;: Classes A and B both depend on interface IB&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Applied to frontend-backend communication:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Before&lt;/strong&gt;: Frontend depends on specific backend endpoints&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;After&lt;/strong&gt;: Both frontend and backend depend on a contract (for example, in OpenAPI format)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The backend returns data according to the contract, and the frontend expects data according to the same contract. This decouples both sides and makes the system more maintainable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;In this chapter, we've established the foundation:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Strict typing&lt;/strong&gt; with no &lt;code&gt;any&lt;/code&gt; and null checks enabled&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explicit contracts&lt;/strong&gt; between frontend and backend&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Separate API layer&lt;/strong&gt; with types colocated&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Avoid unnecessary frontend models&lt;/strong&gt; — trust the backend contract&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use OpenAPI&lt;/strong&gt; as a shared contract when possible&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Chapter 3: Pure Functions and Reactive Data Flow
&lt;/h2&gt;

&lt;h3&gt;
  
  
  3.1 Architecture and System Design
&lt;/h3&gt;

&lt;p&gt;Now that we've established strict typing and contracts, let's look at how data actually flows through an application. The architecture turns out to be &lt;strong&gt;simpler than it seems&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;At its core, the flow is straightforward:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Backend Response → UI
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But between these two points, we need to transform the data into exactly what the UI needs.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.2 It's Simpler Than It Seems
&lt;/h3&gt;

&lt;p&gt;Let's start with the raw material — the types that come from the backend:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Product&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;material&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we need to transform this raw data into something more useful for our UI. This is where &lt;strong&gt;pure functions&lt;/strong&gt; enter the picture:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;PrepareBucketFnc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Bucket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;PopulatedBucket&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function signature tells us everything we need to know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input&lt;/strong&gt;: a Bucket and an array of Products&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output&lt;/strong&gt;: a PopulatedBucket ready for display&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pure&lt;/strong&gt;: no side effects, no hidden dependencies&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3.3 What Makes a Function Pure?
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;pure function&lt;/strong&gt; has two essential characteristics:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;For the same input, it always returns the same output&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It never changes external state&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Pure function example&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;prepareBucket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Bucket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;PopulatedBucket&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// transformation logic only&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;products&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="p"&gt;})),&lt;/span&gt;
    &lt;span class="na"&gt;total&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;calculateTotal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Why Purity Matters
&lt;/h4&gt;

&lt;p&gt;The benefits are profound:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Cognitive load&lt;/strong&gt;: you only need to hold two contexts in your head — input and output&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Testing&lt;/strong&gt;: no side effects to mock or verify, just check the result&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Caching&lt;/strong&gt;: pure functions are trivially memoizable&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3.4 But Can a Stream Be Pure?
&lt;/h3&gt;

&lt;p&gt;This is a crucial question when working with RxJS in Angular. Streams involve time, asynchronous operations, and external data sources — all of which seem to violate purity at first glance.&lt;/p&gt;

&lt;p&gt;Technically, any stream that includes an asynchronous operation — like an HTTP request inside &lt;code&gt;switchMap&lt;/code&gt; — &lt;strong&gt;cannot be pure&lt;/strong&gt; in the strictest sense. Its output depends on network latency, server state, and the exact timing of events. The stream's behavior is not deterministic in the way a pure function's is.&lt;/p&gt;

&lt;p&gt;However, in a frontend application, we operate under a necessary assumption: &lt;strong&gt;the backend is our source of truth&lt;/strong&gt;. We must trust that the data returned by the backend is correct and that the API contract is honored. Given this trust, we can treat the &lt;em&gt;content&lt;/em&gt; of the stream as reliable, even if the &lt;em&gt;timing&lt;/em&gt; is not.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.5 Pure Streams? A Pragmatic View
&lt;/h3&gt;

&lt;p&gt;This leads to a pragmatic definition: a stream can be considered &lt;strong&gt;conditionally pure&lt;/strong&gt; if:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;All synchronous transformations within the stream are pure functions&lt;/li&gt;
&lt;li&gt;The asynchronous sources are trusted to provide data that conforms to established contracts&lt;/li&gt;
&lt;li&gt;Side effects are isolated and explicitly marked (e.g., with &lt;code&gt;tap&lt;/code&gt; for logging)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In practice, this means we build our streams as pipelines where the only impure boundaries are at the edges — the API calls themselves — and everything in between is composed of pure transformations.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.6 Complete Data Stream Example
&lt;/h3&gt;

&lt;p&gt;Here's a typical reactive data flow in Angular:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;bucket$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clientId$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;switchMap&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bucketApi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getClientBucket&lt;/span&gt;&lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;products$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;productsApi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;allProducts&lt;/span&gt;&lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;populatedBucket$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;combineLatest&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bucket$&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;products$&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(([&lt;/span&gt;&lt;span class="nx"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;prepareBucket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
  &lt;span class="nf"&gt;shareReplay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's break down what's happening:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;bucket$&lt;/code&gt; reacts to changes in &lt;code&gt;clientId$&lt;/code&gt;, calling the API each time&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;products$&lt;/code&gt; is a static stream of all products&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;combineLatest&lt;/code&gt; waits for both streams to emit&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;map&lt;/code&gt; applies our pure &lt;code&gt;prepareBucket&lt;/code&gt; function&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;shareReplay(1)&lt;/code&gt; caches the last result for future subscribers&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Conclusion: The Stream as a Source of Trust
&lt;/h3&gt;

&lt;p&gt;Technically, a stream involving asynchronous operations cannot be pure. But in the context of frontend development, purity is not an end in itself — it's a means to achieve &lt;strong&gt;reliability, maintainability, and compile-time safety&lt;/strong&gt;. By trusting the backend as our source of truth and isolating impurities at the edges, we gain a data flow that is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Predictable&lt;/strong&gt; – transformations are pure and testable&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Type-safe&lt;/strong&gt; – contracts ensure data shape at every step&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Composable&lt;/strong&gt; – streams can be combined and transformed declaratively&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reliable&lt;/strong&gt; – the compiler catches mismatches before they reach production&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The total stream, while not mathematically pure, becomes a &lt;strong&gt;trustworthy source of data&lt;/strong&gt; that we can depend on throughout our application. This pragmatic blend of functional principles with real-world constraints is what makes the approach both practical and powerful.&lt;/p&gt;

&lt;h2&gt;
  
  
  Chapter 4: Dirty Common Practices
&lt;/h2&gt;

&lt;p&gt;For years, Angular documentation and articles suggested controlling state through mutations — assigning values in ngOnInit, patching component properties in subscriptions, and letting data flow implicitly through lifecycle hooks. This approach, built on traditional object-oriented patterns, was presented as standard practice. But as applications grew, developers discovered that this advice was deeply flawed: mutations create hidden dependencies, break predictability, and make code progressively harder to reason about. Let's look at a concrete example of how this plays out — and why the "standard" approach to state management has been messing up your code for years.&lt;/p&gt;

&lt;h3&gt;
  
  
  4.1 Let's Talk About Dirty Transformations
&lt;/h3&gt;

&lt;p&gt;Consider this seemingly innocent chain of transformations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;plusOne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;initialValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;one&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;plusOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;initialValue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;two&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;plusOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;one&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;three&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;plusOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;two&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first glance, this appears perfectly deterministic. Each value is derived from the previous one through a pure function. The relationship is clear: &lt;code&gt;three&lt;/code&gt; depends on &lt;code&gt;two&lt;/code&gt;, which depends on &lt;code&gt;one&lt;/code&gt;, which depends on &lt;code&gt;initialValue&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But appearances can be deceiving.&lt;/p&gt;

&lt;h3&gt;
  
  
  4.2 The Hidden Mutation
&lt;/h3&gt;

&lt;p&gt;The problem becomes apparent when we introduce Angular's lifecycle hooks (or other this.* mutatations in side-effects):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;ngOnInit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;two&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;69&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 💥 Breaking the chain!&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Suddenly, our beautiful deterministic chain is broken. &lt;code&gt;two&lt;/code&gt; no longer equals &lt;code&gt;plusOne(one)&lt;/code&gt;. It's been arbitrarily reassigned to &lt;code&gt;69&lt;/code&gt;. And &lt;code&gt;three&lt;/code&gt;? It still holds &lt;code&gt;plusOne(two)&lt;/code&gt; based on the &lt;em&gt;original&lt;/em&gt; chain, but now &lt;code&gt;two&lt;/code&gt; has a different value. The relationship is severed, and the code becomes unpredictable.&lt;/p&gt;

&lt;p&gt;Is this chain still deterministic? &lt;strong&gt;Absolutely not.&lt;/strong&gt; The output now depends on the order of operations — whether &lt;code&gt;ngOnInit&lt;/code&gt; runs before or after the chain is evaluated — and on the programmer's ability to remember not to mutate derived state.&lt;/p&gt;

&lt;h3&gt;
  
  
  4.3 The Power of &lt;code&gt;readonly&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;TypeScript gives us a simple tool to prevent this entire class of bugs: the &lt;code&gt;readonly&lt;/code&gt; modifier.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;initialValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;one&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;plusOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;initialValue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;two&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;plusOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;one&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;three&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;plusOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;two&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;ngOnInit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;two&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;69&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ❌ Compile error: cannot assign to readonly property&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the compiler stops us from breaking the chain. The error appears at compile time, not runtime. The intent is clear: these values are derived, not meant to be reassigned.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;No &lt;code&gt;readonly&lt;/code&gt; is a red flag.&lt;/strong&gt; If you see a class with derived data that isn't marked &lt;code&gt;readonly&lt;/code&gt;, assume it's a source of potential bugs until proven otherwise.&lt;/p&gt;

&lt;h3&gt;
  
  
  4.4 The Pure Stream Alternative
&lt;/h3&gt;

&lt;p&gt;The same pattern applies beautifully to RxJS streams:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;initialValue$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;one$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;initialValue$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;plusOne&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;two$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;one$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;plusOne&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;three$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;two$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;plusOne&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="c1"&gt;// Or more concisely:&lt;/span&gt;
&lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;three$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;initialValue$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;plusOne&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;plusOne&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;plusOne&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the dependencies are explicit and immutable by nature. Each stream is a read-only declaration of how data flows. There's no way to accidentally mutate &lt;code&gt;two$&lt;/code&gt; — it's an observable, not a writable property.&lt;/p&gt;

&lt;h3&gt;
  
  
  4.5 The Signal Alternative
&lt;/h3&gt;

&lt;p&gt;With Angular signals, we get similar guarantees through &lt;code&gt;computed&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;initialValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;one&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;plusOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;initialValue&lt;/span&gt;&lt;span class="p"&gt;()));&lt;/span&gt;
&lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;two&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;plusOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;one&lt;/span&gt;&lt;span class="p"&gt;()));&lt;/span&gt;
&lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;three&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;plusOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;two&lt;/span&gt;&lt;span class="p"&gt;()));&lt;/span&gt;

&lt;span class="nf"&gt;ngOnInit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;two&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;69&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ❌ Error: 'set' does not exist on type Signal&amp;lt;number&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Computed signals are read-only by design. You cannot assign to them — you can only react to their changes.&lt;/p&gt;

&lt;h3&gt;
  
  
  4.6 &lt;code&gt;linkedSignal&lt;/code&gt;: Use Only When Necessary
&lt;/h3&gt;

&lt;p&gt;Angular 17+ introduces &lt;code&gt;linkedSignal&lt;/code&gt; for cases where you need a writable signal that stays synchronized with another signal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;initialValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;one&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;linkedSignal&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;plusOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;initialValue&lt;/span&gt;&lt;span class="p"&gt;()));&lt;/span&gt;
&lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;two&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;linkedSignal&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;plusOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;one&lt;/span&gt;&lt;span class="p"&gt;()));&lt;/span&gt;
&lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;three&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;linkedSignal&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;plusOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;two&lt;/span&gt;&lt;span class="p"&gt;()));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unlike &lt;code&gt;computed&lt;/code&gt;, &lt;code&gt;linkedSignal&lt;/code&gt; creates a writable signal. This means you &lt;em&gt;can&lt;/em&gt; mutate it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;ngOnInit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;two&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;69&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ⚠️ This is allowed!&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While there are legitimate use cases for &lt;code&gt;linkedSignal&lt;/code&gt;, it should be used &lt;strong&gt;only when necessary&lt;/strong&gt;. By default, prefer &lt;code&gt;computed&lt;/code&gt; for derived state. Every writable signal is an opportunity for mutation bugs to creep back into your application.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion: The Cost of Dirty Practices
&lt;/h3&gt;

&lt;p&gt;The dirty practices we've examined — mutable derived state, assignments in lifecycle hooks, and writable signals used unnecessarily — all share a common problem: they &lt;strong&gt;break the explicit dependency chain&lt;/strong&gt; and &lt;strong&gt;increase cognitive load&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Each mutation adds one more context to keep in your head. Each reassignment creates a potential race condition between when the value should be computed and when it actually gets set. Over time, these small dirtinesses accumulate into an untraceable mess.&lt;/p&gt;

&lt;p&gt;The solutions are simple but powerful:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mark everything &lt;code&gt;readonly&lt;/code&gt; by default&lt;/li&gt;
&lt;li&gt;Use pure streams for derived data&lt;/li&gt;
&lt;li&gt;Prefer &lt;code&gt;computed&lt;/code&gt; over &lt;code&gt;linkedSignal&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Let the compiler enforce your intentions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you see code that violates these principles, treat it as a code smell — a warning that the data flow is no longer reliable and bugs are waiting to happen.&lt;/p&gt;

&lt;h4&gt;
  
  
  RxJS vs. Signals: Choosing the Right Tool
&lt;/h4&gt;

&lt;p&gt;Both RxJS observables and Angular signals have their place in a functional, reactive architecture. Understanding their strengths helps you choose the right tool for each job.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RxJS Observables: The Universal Solution&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Universal data flow tool&lt;/strong&gt; – works for everything from API calls to user interactions&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pipeable&lt;/strong&gt; – the &lt;code&gt;pipe&lt;/code&gt; method with operators like &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;switchMap&lt;/code&gt; provides a clean, composable syntax for transformations&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lazy by default&lt;/strong&gt; – observables are cold until subscribed, enabling powerful patterns like lazy loading for dropdowns, accordions, and other conditional UI&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Time-aware&lt;/strong&gt; – built to handle asynchronous operations, debouncing, throttling, and complex event coordination&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cancellable&lt;/strong&gt; – subscriptions can be unsubscribed, and operators like &lt;code&gt;switchMap&lt;/code&gt; cancel pending requests&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Angular Signals: The Final Stage&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Synchronous and predictable&lt;/strong&gt; – signals provide synchronous access to values, making them ideal for the final stages of a data pipeline&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Best for loaded data&lt;/strong&gt; – due to their synchronous nature, signals should primarily be used when all data is already loaded, such as the final mapping step in a component&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fine-grained reactivity&lt;/strong&gt; – signals excel at driving Angular's change detection efficiently&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Limited transformation syntax&lt;/strong&gt; – signals lack a built-in pipe/chain syntax; complex transformations are less elegant than RxJS pipelines&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No lazy loading&lt;/strong&gt; – signals are eager by nature, making them unsuitable for scenarios where you want to defer work until needed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The Pragmatic Approach&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use RxJS for the heavy lifting: data fetching, transformation pipelines, event coordination, and any scenario where laziness or asynchronicity is valuable. Then, at the boundary where data enters your component and is ready for display, materialize it using &lt;code&gt;| async&lt;/code&gt; pipe and pass inside dumb component as input signal:&lt;/p&gt;

&lt;p&gt;This gives you the best of both worlds: the power and flexibility of RxJS for data flow, with the simplicity and performance of signals for template rendering.&lt;/p&gt;

&lt;h2&gt;
  
  
  Chapter 5: Building Safe Data Flows with RxJS
&lt;/h2&gt;

&lt;h3&gt;
  
  
  5.1 The Core Principles: Immutability, Pure Functions, Reactivity
&lt;/h3&gt;

&lt;p&gt;Before diving into code, let's restate the three pillars that make our approach work:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Immutability&lt;/strong&gt; – data never changes after creation; transformations produce new data instead of modifying existing structures.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pure Functions&lt;/strong&gt; – transformations are deterministic and side-effect-free.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reactivity&lt;/strong&gt; – data flows are explicitly described as streams that react to changes over time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These three concepts work together to create predictable, maintainable applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  5.2 The Typical Data Flow Pattern
&lt;/h3&gt;

&lt;p&gt;In an Angular application using RxJS, most data flows follow a simple, repeatable pattern:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9b2p2cdtj2c1upupvk4v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9b2p2cdtj2c1upupvk4v.png" alt=" " width="800" height="82"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Event Source&lt;/strong&gt;: A user action (input change, button click), a route parameter, or even another stream triggers the flow.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Async Mapping&lt;/strong&gt;: An asynchronous operation — typically an HTTP request via &lt;code&gt;switchMap&lt;/code&gt; or &lt;code&gt;mergeMap&lt;/code&gt; — fetches or persists data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sync Mapping&lt;/strong&gt;: One or more pure functions transform the data synchronously using &lt;code&gt;map&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Consumer&lt;/strong&gt;: The resulting stream is consumed, often in the template via &lt;code&gt;async&lt;/code&gt; pipe, or fed into another stream.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This pattern is simple, but it scales remarkably well.&lt;/p&gt;

&lt;h3&gt;
  
  
  5.3 The Real Project Reality
&lt;/h3&gt;

&lt;p&gt;In a real application, the chain of transformations can become long and involve multiple sources:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frow6v3dha10nahp26e12.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frow6v3dha10nahp26e12.png" alt=" " width="800" height="882"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What makes this manageable?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sequences&lt;/strong&gt; are built using &lt;code&gt;pipe&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Asynchronous operations&lt;/strong&gt; are handled by higher-order operators (&lt;code&gt;switchMap&lt;/code&gt;, &lt;code&gt;mergeMap&lt;/code&gt;, etc.).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data combinations&lt;/strong&gt; use operators like &lt;code&gt;combineLatest&lt;/code&gt;, &lt;code&gt;forkJoin&lt;/code&gt;, or &lt;code&gt;withLatestFrom&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  5.4 Why This Approach Is Safe
&lt;/h3&gt;

&lt;p&gt;When your data flow is built this way, you gain several safety guarantees:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Traceability to System Design&lt;/strong&gt;: If your project has a system design document, the data flow diagrams map almost one-to-one to the RxJS streams in your code. This makes the implementation transparent and reviewable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Type Safety at Every Step&lt;/strong&gt;: Each transformation is typed. The compiler ensures that the output of one operation matches the expected input of the next.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reduced Cognitive Load&lt;/strong&gt;: When working with pure functions inside &lt;code&gt;map&lt;/code&gt;, you only need to understand the input and output of that specific transformation. The rest of the pipeline can be treated as a black box.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Easy Modifications&lt;/strong&gt;: Need to change something?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Find the relevant part of the transformation chain.&lt;/li&gt;
&lt;li&gt;Trace up or down to locate the right spot for your change.&lt;/li&gt;
&lt;li&gt;Make the change (e.g., add a new &lt;code&gt;map&lt;/code&gt; step).&lt;/li&gt;
&lt;li&gt;Fix any compilation errors that appear downstream.&lt;/li&gt;
&lt;li&gt;Fix any failing tests.&lt;/li&gt;
&lt;li&gt;...&lt;/li&gt;
&lt;li&gt;Profit!&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because the compiler checks types throughout the pipeline, you can refactor with confidence. If you break something, TypeScript will tell you immediately.&lt;/p&gt;

&lt;h3&gt;
  
  
  5.5 Summary
&lt;/h3&gt;

&lt;p&gt;The combination of immutability, pure functions, and reactivity expressed through RxJS streams creates a robust foundation for Angular applications. The patterns shown here scale from simple component data needs to complex enterprise-grade data flows. By adhering to this structure, you make your codebase easier to understand, safer to change, and more reliable in production.&lt;/p&gt;

&lt;h2&gt;
  
  
  Chapter 6: Practical Examples: From Dirty to Clean
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Theory is essential, but nothing beats concrete examples. This chapter presents common Angular patterns that often lead to bugs and shows how to refactor them using the functional, reactive approach we've discussed throughout this article.&lt;/p&gt;

&lt;p&gt;Each example follows the same structure:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;❌ &lt;strong&gt;Bad&lt;/strong&gt;: The mutable, imperative approach with hidden bugs&lt;/li&gt;
&lt;li&gt;✅ &lt;strong&gt;Good&lt;/strong&gt;: The reactive, type-safe alternative&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  6.1 Mutating Subscriptions
&lt;/h3&gt;

&lt;p&gt;One of the most common anti-patterns in Angular is manually subscribing to observables and assigning results to component properties.&lt;/p&gt;

&lt;h4&gt;
  
  
  ❌ Bad: Manual Subscription with Mutation
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;fetchProducts&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;productsApi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;allProducts&lt;/span&gt;&lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;products&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 💥 Mutation!&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What's wrong here?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Manual subscription requires manual unsubscription (memory leaks)&lt;/li&gt;
&lt;li&gt;The assignment mutates component state&lt;/li&gt;
&lt;li&gt;The timing is unclear — when exactly does &lt;code&gt;products&lt;/code&gt; become available?&lt;/li&gt;
&lt;li&gt;No reactive updates if the data changes later&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Unfortunately, this was the &lt;strong&gt;common pattern&lt;/strong&gt; for handling async data in Angular for many years. It appears in countless documentation examples, tutorials, and articles. Despite its prevalence, it's &lt;strong&gt;misleading&lt;/strong&gt; and leads to the very problems we're trying to solve — mutable state, implicit dependencies, and potential memory leaks. If you're still writing code this way, you're not alone, but it's time to move on.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  ✅ Good: Declarative Stream
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;products$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;productsApi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;allProducts&lt;/span&gt;&lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why this is better:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No subscription, no memory leak&lt;/li&gt;
&lt;li&gt;Immutable by design&lt;/li&gt;
&lt;li&gt;Clear data source&lt;/li&gt;
&lt;li&gt;Can be consumed directly in template with &lt;code&gt;async&lt;/code&gt; pipe
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;@if (products$ | async; as products) {
&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"product-list"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  @for (product of products; track product.id) {
  &lt;span class="nt"&gt;&amp;lt;app-product-card&lt;/span&gt; &lt;span class="na"&gt;[product]=&lt;/span&gt;&lt;span class="s"&gt;"product"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  }
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
} @else {
&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;Loading...&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  6.2 Initialization at Declaration vs. Lifecycle Hooks
&lt;/h3&gt;

&lt;p&gt;Another common pattern is declaring fields with the &lt;code&gt;!&lt;/code&gt; assertion and initializing them in &lt;code&gt;ngOnInit&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  ❌ Bad: Delayed Initialization
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;products$&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;ngOnInit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;products$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;productsApi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;allProducts&lt;/span&gt;&lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What's wrong here?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;!&lt;/code&gt; assertion tells TypeScript "trust me, this will be set" — but there's no guarantee&lt;/li&gt;
&lt;li&gt;If someone accesses &lt;code&gt;products$&lt;/code&gt; before &lt;code&gt;ngOnInit&lt;/code&gt;, it crashes at runtime&lt;/li&gt;
&lt;li&gt;The dependency is implicit, not declarative&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Using the &lt;code&gt;!&lt;/code&gt; assertion effectively &lt;strong&gt;disables TypeScript's &lt;code&gt;strictNullChecks&lt;/code&gt;&lt;/strong&gt; for that variable. You're telling the compiler to ignore the possibility that the value might be &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;. This is a very bad practice because it removes the very safety net we're trying to build. Always prefer initializing at declaration so TypeScript can verify correctness.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  ✅ Good: Initialize at Declaration
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;products$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;productsApi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;allProducts&lt;/span&gt;&lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why this is better:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TypeScript can verify the value exists&lt;/li&gt;
&lt;li&gt;No risk of accessing before initialization&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;readonly&lt;/code&gt; modifier prevents reassignment&lt;/li&gt;
&lt;li&gt;Dependencies are explicit&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  6.3 Reactive Inputs
&lt;/h3&gt;

&lt;p&gt;Input properties are a classic source of imperative complexity. Let's see how to make them reactive.&lt;/p&gt;

&lt;h4&gt;
  
  
  ❌ Bad: Imperative Input Handling
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;clientId&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;ngOnChanges&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;changes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;SimpleChanges&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;changes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;clientId&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loadClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;loadClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clientApi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getClient&lt;/span&gt;&lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What's wrong here?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple moving parts: &lt;code&gt;ngOnChanges&lt;/code&gt;, manual subscription, mutation&lt;/li&gt;
&lt;li&gt;Easy to miss edge cases (what if &lt;code&gt;clientId&lt;/code&gt; changes during an ongoing request?)&lt;/li&gt;
&lt;li&gt;No easy way to compose with other streams&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  ✅ Good: Reactive Input with Subject (Pre-Signals)
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;clientId$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ReplaySubject&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;set&lt;/span&gt; &lt;span class="nf"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clientId$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;client$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clientId$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;switchMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clientApi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getClient&lt;/span&gt;&lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
  &lt;span class="nf"&gt;shareReplay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why this is better:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Input changes become a stream&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;switchMap&lt;/code&gt; cancels pending requests when new values arrive&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;shareReplay&lt;/code&gt; caches the result for future subscribers&lt;/li&gt;
&lt;li&gt;No manual subscriptions&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  ✅ Even Better: Reactive Input with Signals
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;clientId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;required&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;clientId$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;toObservable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;client$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clientId$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;switchMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clientApi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getClient&lt;/span&gt;&lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
  &lt;span class="nf"&gt;shareReplay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With Angular signals, the input itself is reactive, eliminating the need for a manual subject.&lt;/p&gt;

&lt;h4&gt;
  
  
  ✅ The Same Pattern Works for ViewChild
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;viewport$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ReplaySubject&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;CdkVirtualScrollViewport&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;ViewChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;viewport&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;static&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="kd"&gt;set&lt;/span&gt; &lt;span class="nf"&gt;viewportRef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;CdkVirtualScrollViewport&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;viewport$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;visiblePages$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;viewport$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;switchMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;renderedRangeStream&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(({&lt;/span&gt; &lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;end&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pageSize&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ceil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pageSize&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;]),&lt;/span&gt;
  &lt;span class="nf"&gt;mergeMap&lt;/span&gt;&lt;span class="p"&gt;(([&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;))),&lt;/span&gt;
  &lt;span class="nf"&gt;startWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  6.4 Infinite Streams of Events
&lt;/h3&gt;

&lt;p&gt;Sometimes you need to react to events that occur multiple times. The imperative approach quickly becomes messy.&lt;/p&gt;

&lt;h4&gt;
  
  
  ❌ Bad: Imperative Event Handling
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;ngOnInit&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchClient&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;fetchClient&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clientsApi&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getClient&lt;/span&gt;&lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What's wrong here?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Only fetches once (doesn't react to changes)&lt;/li&gt;
&lt;li&gt;Manual subscription&lt;/li&gt;
&lt;li&gt;Mutation&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  ✅ Good: Reactive Event Stream
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;client$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clientId$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;switchMap&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clientsApi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getClient&lt;/span&gt;&lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
  &lt;span class="nf"&gt;shareReplay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why this is better:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reacts to every new &lt;code&gt;clientId&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Clean, declarative pipeline&lt;/li&gt;
&lt;li&gt;Automatically handles unsubscription&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  6.5 Side Effects in Dedicated Places
&lt;/h3&gt;

&lt;p&gt;Side effects (logging, navigation, notifications) are necessary but should be isolated from pure transformations.&lt;/p&gt;

&lt;h4&gt;
  
  
  ❌ Bad: Side Effects Inside Map
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;populatedBucket$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;combineLatest&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bucket$&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;products$&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(([&lt;/span&gt;&lt;span class="nx"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;prepareBucket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;logRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 💥 Side effect inside map!&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}),&lt;/span&gt;
  &lt;span class="nf"&gt;shareReplay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What's wrong here?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;map&lt;/code&gt; should be pure — side effects break predictability&lt;/li&gt;
&lt;li&gt;The logging affects the return value? No, but it's hidden&lt;/li&gt;
&lt;li&gt;Testing becomes harder&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  ✅ Good: Isolate Side Effects with Tap
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;populatedBucket$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;combineLatest&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bucket$&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;products$&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(([&lt;/span&gt;&lt;span class="nx"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;prepareBucket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
  &lt;span class="nf"&gt;tap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;logRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt; &lt;span class="c1"&gt;// ✅ Side effect clearly marked&lt;/span&gt;
  &lt;span class="nf"&gt;shareReplay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why this is better:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;tap&lt;/code&gt; explicitly signals "something happens here, but data continues unchanged"&lt;/li&gt;
&lt;li&gt;The transformation (&lt;code&gt;map&lt;/code&gt;) remains pure&lt;/li&gt;
&lt;li&gt;Clear separation of concerns&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  6.6 Non-Nullable Forms with Async Data
&lt;/h3&gt;

&lt;p&gt;Forms combined with async data are notoriously tricky. A common pain point is that a form often &lt;strong&gt;cannot be fully initialized until the asynchronous data arrives&lt;/strong&gt; — yet the template needs a form to bind to. The typical imperative approach leads to race conditions, temporary invalid states, and manual subscriptions.&lt;/p&gt;

&lt;h4&gt;
  
  
  ❌ Bad: Initialize Empty, Then Patch
&lt;/h4&gt;

&lt;p&gt;A frequent pattern is to create the form immediately with empty/default values, then fetch data and &lt;code&gt;patchValue&lt;/code&gt; when it arrives:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;editForm&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;FormGroup&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;ngOnInit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Initialize form with empty values&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;editForm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;group&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Validators&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;required&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Validators&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;required&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Validators&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="c1"&gt;// Then fetch data and patch values&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clientApi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getClient&lt;/span&gt;&lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;editForm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;patchValue&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;editForm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;valid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clientApi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;save&lt;/span&gt;&lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;editForm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(...);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What's wrong here?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The form initially shows empty fields — what if the user starts typing before data arrives?&lt;/li&gt;
&lt;li&gt;Validation may incorrectly mark fields as invalid while waiting for real data&lt;/li&gt;
&lt;li&gt;If the request fails, the form remains in an empty state with no clear indication&lt;/li&gt;
&lt;li&gt;Manual subscription requires cleanup&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;patchValue&lt;/code&gt; is a mutation that doesn't play well with reactive flows&lt;/li&gt;
&lt;li&gt;No easy way to show a loading state while data is being fetched&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The core issue: &lt;strong&gt;the form is created before its data is ready&lt;/strong&gt;, forcing us to "patch" it later.&lt;/p&gt;

&lt;h4&gt;
  
  
  ✅ Good: Create the Form in a Reactive Stream
&lt;/h4&gt;

&lt;p&gt;The solution is to &lt;strong&gt;treat the form itself as something that emerges from the data flow&lt;/strong&gt;. Create the form only when the data arrives, using a reactive stream. This way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The form is emitted exactly when data is available&lt;/li&gt;
&lt;li&gt;The template can show a loading state while waiting&lt;/li&gt;
&lt;li&gt;No patching, no mutations — the form is built correctly from the start&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;First, create a reusable helper to observe form value and validity:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getFormValueAndValidity$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;control&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;AbstractControl&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nx"&gt;control&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;valueChanges&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nf"&gt;startWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;control&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;valid&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;control&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;valid&lt;/span&gt; &lt;span class="p"&gt;})),&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then use it in your component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;editForm$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clientApi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getClient&lt;/span&gt;&lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;group&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Validators&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;required&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Validators&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;required&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Validators&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
  &lt;span class="p"&gt;})),&lt;/span&gt;
  &lt;span class="nf"&gt;shareReplay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;formValue$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;editForm$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;switchMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;form&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;getFormValueAndValidity&lt;/span&gt;&lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
  &lt;span class="nf"&gt;shareReplay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;save$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Subject&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;saveResult$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;save$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;switchMap&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;formValue$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;take&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))),&lt;/span&gt;
  &lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(({&lt;/span&gt; &lt;span class="nx"&gt;valid&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;valid&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(({&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;prepareClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
  &lt;span class="nf"&gt;switchMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clientApi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;saveClient&lt;/span&gt;&lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Template:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;@if (editForm$ | async; as form) {
&lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;[formGroup]=&lt;/span&gt;&lt;span class="s"&gt;"form"&lt;/span&gt; &lt;span class="na"&gt;(ngSubmit)=&lt;/span&gt;&lt;span class="s"&gt;"save$.next()"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;formControlName=&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt; &lt;span class="na"&gt;placeholder=&lt;/span&gt;&lt;span class="s"&gt;"Name"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  @if (form.get('name')?.invalid &lt;span class="err"&gt;&amp;amp;&amp;amp;&lt;/span&gt; form.get('name')?.touched) {
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"error"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Name is required&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  }

  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;formControlName=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt; &lt;span class="na"&gt;placeholder=&lt;/span&gt;&lt;span class="s"&gt;"Email"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  @if (form.get('email')?.invalid &lt;span class="err"&gt;&amp;amp;&amp;amp;&lt;/span&gt; form.get('email')?.touched) {
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"error"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Valid email is required&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  }

  &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt; &lt;span class="na"&gt;[disabled]=&lt;/span&gt;&lt;span class="s"&gt;"form.invalid"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Save&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
} @else {
&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;Loading client data...&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why this is better:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Everything is a stream — no manual subscriptions, no mutations&lt;/li&gt;
&lt;li&gt;Form initialization happens reactively when client data arrives; the form is &lt;strong&gt;created only when its data is ready&lt;/strong&gt;, solving the "cannot initialize until async data arrives" problem&lt;/li&gt;
&lt;li&gt;Validation is integrated into the stream pipeline&lt;/li&gt;
&lt;li&gt;Save action is triggered by a subject and composed with the latest valid form value&lt;/li&gt;
&lt;li&gt;The template elegantly handles the loading state with &lt;code&gt;@else&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;No risk of displaying an empty or partially filled form&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;Each of these examples demonstrates the same principle: &lt;strong&gt;move from imperative, mutable code to declarative, reactive streams&lt;/strong&gt;. The benefits compound:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fewer bugs at runtime&lt;/li&gt;
&lt;li&gt;Clearer data dependencies&lt;/li&gt;
&lt;li&gt;Easier testing&lt;/li&gt;
&lt;li&gt;Better performance through proper subscription management&lt;/li&gt;
&lt;li&gt;Type safety throughout&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The patterns shown here aren't theoretical — they're battle-tested in production applications and have proven to reduce bugs dramatically. Apply them consistently, and your codebase will become more reliable and maintainable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Chapter 7: Adopting the Approach: Strategy, Metrics, and Cultural Change
&lt;/h2&gt;

&lt;h3&gt;
  
  
  7.1 Start Small: Proof of Concept
&lt;/h3&gt;

&lt;p&gt;You've seen the principles, studied the patterns, and you're convinced. But how do you introduce this into an existing team or codebase without causing friction?&lt;/p&gt;

&lt;p&gt;The answer is simple: &lt;strong&gt;start small&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Start with a small piece of functionality to test the concept. Describe the data access layer with contracts, perform the mapping to UI without mutations and subscriptions. Show the result to colleagues.&lt;/p&gt;

&lt;p&gt;Pick a self-contained feature — a new component, a refactoring of a small existing one — and implement it using the principles from this article:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Strict contracts with the backend in a dedicated API service&lt;/li&gt;
&lt;li&gt;Pure functions for data transformations&lt;/li&gt;
&lt;li&gt;Reactive streams with RxJS, no manual subscriptions&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;readonly&lt;/code&gt; fields and immutable data flow&lt;/li&gt;
&lt;li&gt;Template consumption via &lt;code&gt;async&lt;/code&gt; pipe&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Make it clean, simple, and demonstrably better than the old way. Then share it.&lt;/p&gt;

&lt;h3&gt;
  
  
  7.2 Reading the Room: Observe Colleagues' Reactions
&lt;/h3&gt;

&lt;p&gt;Once you've built your proof of concept, it's time to show it. But be prepared for varied reactions.&lt;/p&gt;

&lt;p&gt;Some will be curious, some skeptical, some outright resistant. This is normal. Change is hard, and people have different comfort levels with new paradigms.&lt;/p&gt;

&lt;p&gt;Your goal at this stage is not to convert everyone overnight. It's to &lt;strong&gt;plant a seed&lt;/strong&gt;. Let the code speak for itself. When colleagues see:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No more guessing when data is available&lt;/li&gt;
&lt;li&gt;No more memory leaks from forgotten subscriptions&lt;/li&gt;
&lt;li&gt;Compiler errors catching mistakes instead of runtime crashes&lt;/li&gt;
&lt;li&gt;Easier testing and refactoring&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;...some will start asking questions. That's your opening.&lt;/p&gt;

&lt;h3&gt;
  
  
  7.3 Plan B: Incremental Adoption
&lt;/h3&gt;

&lt;p&gt;If the team isn't ready to embrace the full reactive functional approach, don't force it. Have a &lt;strong&gt;Plan B&lt;/strong&gt; — a set of smaller, less disruptive steps that move the codebase in the right direction without requiring a complete mindset shift.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 1: Enforce &lt;code&gt;readonly&lt;/code&gt; in Code Reviews
&lt;/h4&gt;

&lt;p&gt;Start by requiring that all fields that should not be reassigned are marked &lt;code&gt;readonly&lt;/code&gt;. This is a low-friction change that immediately raises awareness about mutation. It's analogous to using &lt;code&gt;const&lt;/code&gt; instead of &lt;code&gt;let&lt;/code&gt; — a practice most developers already accept.&lt;/p&gt;

&lt;p&gt;Colleagues start thinking about mutations in &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 2: Isolate the Data Access Layer
&lt;/h4&gt;

&lt;p&gt;Discuss the need to isolate the data access layer — move calls and contracts into separate services.&lt;/p&gt;

&lt;p&gt;Introduce a clear separation between API calls and business logic. Create dedicated service files that contain only the HTTP calls and their associated types. This doesn't require rewriting everything; just start moving new API interactions into this pattern.&lt;/p&gt;

&lt;p&gt;This change shouldn't be offensive because the convergence between DAL and business logic is a common source of unnecessary complexity and leaking abstractions. Separating them is a universally recognized good practice, regardless of programming paradigm.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 3: Enable Functional Style in Your Own Code
&lt;/h4&gt;

&lt;p&gt;Get the opportunity to write code in your area of responsibility in a functional style.&lt;/p&gt;

&lt;p&gt;Even if the rest of the team isn't ready, you can adopt the principles in the features you own. Demonstrate the benefits through working code, not lectures.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 4: Return to the Conversation Later
&lt;/h4&gt;

&lt;p&gt;Return to the issue when colleagues have moved past the denial stage.&lt;/p&gt;

&lt;p&gt;Once the team has experienced the pain of debugging mutable state a few more times, or seen your functional code survive a refactoring unscathed, they'll be more receptive. Circle back and suggest a wider adoption.&lt;/p&gt;

&lt;h3&gt;
  
  
  7.4 Measuring Success: The Metrics Problem
&lt;/h3&gt;

&lt;p&gt;How do you prove that this approach actually reduces bugs? This is notoriously difficult.&lt;/p&gt;

&lt;p&gt;Metrics are difficult — you can't track the number of potential errors caught by typing, because no one commits code with errors.&lt;/p&gt;

&lt;p&gt;The very nature of compile-time error prevention means the errors never make it into the codebase. You can't count bugs that didn't happen. Traditional metrics like "number of bugs found in QA" will go down, but correlating that with the new practices is hard.&lt;/p&gt;

&lt;p&gt;However, there is one metric that matters most: &lt;strong&gt;production incidents&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  7.5 Real-World Results
&lt;/h3&gt;

&lt;p&gt;Overall, after locally implementing this approach, the number of production errors dropped to nearly zero. Time horizon — about two years.&lt;/p&gt;

&lt;p&gt;This is the ultimate validation. Over a two-year period, a team that adopted these principles saw production bugs virtually disappear. Not "reduced by 20%" — &lt;strong&gt;nearly zero&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This isn't a coincidence. When you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make all dependencies explicit through streams&lt;/li&gt;
&lt;li&gt;Eliminate manual subscriptions and mutations&lt;/li&gt;
&lt;li&gt;Let the compiler verify contracts and transformations&lt;/li&gt;
&lt;li&gt;Isolate side effects&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;...you remove the vast majority of places where bugs can hide. The code becomes self-verifying. What remains are logical errors in requirements, not implementation mistakes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion: Culture Eats Strategy for Breakfast
&lt;/h3&gt;

&lt;p&gt;Adopting functional, reactive principles in an Angular codebase is as much a &lt;strong&gt;cultural change&lt;/strong&gt; as a technical one. It requires patience, diplomacy, and a willingness to start small. But the results speak for themselves: fewer bugs, easier maintenance, and happier developers.&lt;/p&gt;

&lt;p&gt;Whether you're the lone advocate or part of a team ready to transform, remember: &lt;strong&gt;every journey begins with a single step&lt;/strong&gt;. Start with a small feature, show the results, and let the code convince the skeptics. The compiler will thank you—and so will your future self.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Journey from Dirty to Deterministic
&lt;/h2&gt;

&lt;p&gt;We began this article with a familiar scene: a 5000-line class, fields peppered with &lt;code&gt;!&lt;/code&gt; assertions, and the nagging feeling that data appears and disappears through some inscrutable magic. We identified the root problem — &lt;strong&gt;implicit dependencies and mutable state&lt;/strong&gt; — and the cognitive cost it imposes on every developer who touches the code.&lt;/p&gt;

&lt;p&gt;Then we charted a path forward, not through abstract theory, but through practical, incremental changes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Chapter 1&lt;/strong&gt; established the goal: catch errors at compile time, where they're cheapest to fix.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chapter 2&lt;/strong&gt; laid the foundation with strict typing and explicit contracts between frontend and backend.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chapter 3&lt;/strong&gt; introduced pure functions and showed how to build reactive data flows that are predictable and testable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chapter 4&lt;/strong&gt; exposed the dirty practices that undermine these efforts and showed how &lt;code&gt;readonly&lt;/code&gt;, streams, and signals prevent them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chapter 5&lt;/strong&gt; demonstrated how RxJS enables safe, composable data flows that scale from simple components to complex enterprise features.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chapter 6&lt;/strong&gt; provided concrete, before-and-after examples of common Angular patterns, refactored to eliminate mutations and manual subscriptions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chapter 7&lt;/strong&gt; addressed the human side of change: how to introduce these ideas incrementally, read the room, and build momentum without causing friction.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Payoff: Compile-Time Confidence
&lt;/h3&gt;

&lt;p&gt;Throughout this journey, one theme has remained constant: &lt;strong&gt;the compiler is your friend&lt;/strong&gt;. Every time you replace a runtime check with a compile-time guarantee, you're not just fixing a potential bug — you're eliminating an entire class of bugs forever.&lt;/p&gt;

&lt;p&gt;When you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Replace &lt;code&gt;!&lt;/code&gt; assertions with proper initialization&lt;/li&gt;
&lt;li&gt;Replace manual subscriptions with &lt;code&gt;async&lt;/code&gt; pipes&lt;/li&gt;
&lt;li&gt;Replace mutable fields with &lt;code&gt;readonly&lt;/code&gt; and streams&lt;/li&gt;
&lt;li&gt;Replace implicit dependencies with explicit data flows&lt;/li&gt;
&lt;li&gt;Replace runtime validation with type contracts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;...you transform your codebase from a house of cards into a fortress. The compiler becomes your tireless reviewer, catching mistakes before they ever reach a browser.&lt;/p&gt;

&lt;h3&gt;
  
  
  But Isn't This Overkill?
&lt;/h3&gt;

&lt;p&gt;Some will argue that this approach adds complexity. "Why learn RxJS?" "Why bother with all these patterns when &lt;code&gt;ngOnInit&lt;/code&gt; works just fine?"&lt;/p&gt;

&lt;p&gt;The answer lies in the &lt;strong&gt;scale of complexity&lt;/strong&gt; modern applications handle. A todo app doesn't need this discipline. But an enterprise application with dozens of developers, hundreds of components, and thousands of possible states? That's exactly where these principles prove their worth.&lt;/p&gt;

&lt;p&gt;The patterns in this article don't add complexity — they &lt;strong&gt;manage it&lt;/strong&gt;. They make the implicit explicit, the hidden visible, the unpredictable deterministic. They replace "it works on my machine" with "it works, and the compiler proves it."&lt;/p&gt;

&lt;h3&gt;
  
  
  Signals and RxJS: A Partnership, Not a Replacement
&lt;/h3&gt;

&lt;p&gt;The introduction of signals in Angular doesn't invalidate RxJS — it clarifies their respective roles. As we saw in Chapter 4:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;RxJS&lt;/strong&gt; remains the universal tool for data flow, especially where asynchronicity, laziness, and complex transformations are involved. Its pipeable operators and cold observables are irreplaceable for building robust data pipelines.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Signals&lt;/strong&gt; excel at the final stage: taking loaded, transformed data and efficiently driving Angular's change detection. They're the destination, not the journey.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Use RxJS for the heavy lifting, convert to signals at component boundaries, and enjoy the best of both worlds.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Final Checklist
&lt;/h3&gt;

&lt;p&gt;As you close this article and return to your codebase, keep these questions in mind:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Can I find any &lt;code&gt;this.x =&lt;/code&gt; assignments outside of constructors?&lt;/strong&gt; If yes, those are potential bugs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Are my API contracts explicit and typed?&lt;/strong&gt; If not, you're trusting runtime behavior over compile-time guarantees.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Do my components contain manual subscriptions?&lt;/strong&gt; If yes, they contain memory leaks and implicit dependencies.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Is derived data marked &lt;code&gt;readonly&lt;/code&gt;?&lt;/strong&gt; If not, mutations are hiding in plain sight.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Can I trace a piece of data from its source to its display as a single stream?&lt;/strong&gt; If not, the flow is implicit and fragile.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  The Ultimate Metric
&lt;/h3&gt;

&lt;p&gt;The team behind this approach reported that over two years, production errors dropped to &lt;strong&gt;nearly zero&lt;/strong&gt;. Not a 20% reduction. Not a 50% reduction. Nearly zero.&lt;/p&gt;

&lt;p&gt;That's not hype. That's the natural consequence of letting the compiler do its job. Every bug caught at compile time is one that never wastes a developer's debugging session, never frustrates a QA engineer, and never reaches a paying customer.&lt;/p&gt;

&lt;h3&gt;
  
  
  Your Journey Starts Now
&lt;/h3&gt;

&lt;p&gt;You don't need to rewrite your entire application tomorrow. Start small. Add &lt;code&gt;readonly&lt;/code&gt; to one component. Refactor one subscription. Define one API contract. Show your colleagues. Let the code speak for itself.&lt;/p&gt;

&lt;p&gt;The principles in this article have been proven in production, across teams, over years. They work. They scale. They make development faster and more enjoyable.&lt;/p&gt;

&lt;p&gt;The compiler is waiting. Give it the tools it needs, and it will guard your application with unwavering vigilance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You won't have compilation errors without compilation. But with the right approach, you'll have far fewer runtime errors — and that's the whole point.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;.&lt;/p&gt;

&lt;p&gt;.&lt;/p&gt;

&lt;p&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  About me:
&lt;/h4&gt;

&lt;p&gt;10+ years of Software Development, FP enthusiast, ex C# (that’s why I prefer strict typing). Contact me on  &lt;a href="https://www.linkedin.com/in/earthdmitriy/" rel="noopener noreferrer"&gt;linkedin&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Check my tools on npm:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://www.npmjs.com/package/angular-mermaider" rel="noopener noreferrer"&gt;angular-mermaider&lt;/a&gt; — Do you remember that data flows from system design schemas map one-to-one onto RxJS streams? This works in reverse too: angular-mermaider is a static analyzer that processes your component code and automatically generates Mermaid diagrams of their actual data flows. When you build your application with explicit, stream-based architecture, you get accurate, always-synchronized diagrams for free — making system design review, documentation, and team onboarding dramatically easier.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.npmjs.com/package/@rx-evo/stateful-observable" rel="noopener noreferrer"&gt;stateful-observable&lt;/a&gt; — Stop writing &lt;code&gt;tap(() =&amp;gt; loading$.next(true))&lt;/code&gt; boilerplate and wrestling with dead observables after an error. @rx-evo/stateful-observable wraps your RxJS streams to expose value$, error$, and pending$ out of the box — while staying truly reactive and never halting on errors. It lets you chain, combine, and cache data streams with full type safety, and its design philosophy aligns perfectly with the principles in this article: atomic, feature-local state, minimal mutation, and maximum compile-time safety. For about 80% of your data-flow needs, it replaces repetitive patterns with a clean, functional abstraction.&lt;/p&gt;

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