<?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: diptee</title>
    <description>The latest articles on DEV Community by diptee (@dipteekhd).</description>
    <link>https://dev.to/dipteekhd</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F739561%2Fd92af2af-3919-4831-a437-4f7ba152ea0d.png</url>
      <title>DEV Community: diptee</title>
      <link>https://dev.to/dipteekhd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dipteekhd"/>
    <language>en</language>
    <item>
      <title>Angular : RxJS concatMap operator</title>
      <dc:creator>diptee</dc:creator>
      <pubDate>Sat, 06 Nov 2021 14:41:41 +0000</pubDate>
      <link>https://dev.to/dipteekhd/angular-rxjs-concatmap-operator-4614</link>
      <guid>https://dev.to/dipteekhd/angular-rxjs-concatmap-operator-4614</guid>
      <description>&lt;ul&gt;
&lt;li&gt;In Angular, we use HTTP to get data from backend,HTTP methods always return an observable.Generally we subscribe to that observable and assign response data to class variables to use it in our component.&lt;/li&gt;
&lt;li&gt;But using RxJS operators we can compose multiple observables(data streams) or process  observable data  before subscribing to it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;RxJS concatMap operator&lt;/strong&gt;&lt;br&gt;
It is a High-order Mapping operator an operator that takes value from an outer observable and maps it into inner observable instead of plain values(&lt;strong&gt;Observable emits observable&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;concatMap operator&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Takes each value from the outer observable and maps that value  to observable(called as inner observable) &lt;/li&gt;
&lt;li&gt;Concat all inner observables into a single observable in order  and subscribe to inner observables and emit data of each inner observable into output observable in sequential manner.&lt;/li&gt;
&lt;li&gt;concatMap never subscribes to the next inner observable until the previous one completes.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example:&lt;br&gt;
Display first 3 toppers of the computer department on UI with ROLL NUMBER &amp;amp; MARKS.&lt;br&gt;
&lt;a href="https://media.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%2Fqofhfkxmgt9ffcmxa1t0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fqofhfkxmgt9ffcmxa1t0.png" alt="toppers"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://57dhr.csb.app/" rel="noopener noreferrer"&gt;Run live&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we have 2 API’s&lt;br&gt;
1.First API to get roll numbers of first 3 toppers.&lt;br&gt;
2.Second API to get marks of topper taking roll number as input.&lt;/p&gt;

&lt;p&gt;In the above case we need to take all roll numbers from the first API  and pass it to the second API.&lt;br&gt;
Normally in such scenarios developers use a nested subscription approach.&lt;br&gt;
&lt;a href="https://media.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%2Fhsmq81ny79m0e3umm3ge.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fhsmq81ny79m0e3umm3ge.png" alt="Nested"&gt;&lt;/a&gt;&lt;br&gt;
Here due to nested subscription approach output may vary,because we are hitting HTTP requests for each roll number,but here HTTP requests are not completing in sequential manner,depending upon which request completes first that request response data gets pushed in toppersList_1. &lt;/p&gt;

&lt;p&gt;So solution to this problem is use concatMap RxJS operator instead of nested subscription.&lt;br&gt;
For given use-case,we need to use two concatMap operator.&lt;br&gt;
&lt;a href="https://media.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%2Ftwlmz1i1soc73lx2pgd2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Ftwlmz1i1soc73lx2pgd2.png" alt="concat"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First contactMap operator -&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;getToppers_rollNum()&lt;/em&gt; this method returns an Observable of type number[] , conactMap  maps &lt;code&gt;Observable&amp;lt;number[]&amp;gt;&lt;/code&gt; to &lt;code&gt;Observable &amp;lt;number&amp;gt;&lt;/code&gt;(inner observable) then subscribes to inner observable and emit data into output observable(top_3_rollNum$).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Second  concatMap operator -&lt;/strong&gt;&lt;br&gt;
Let's consider &lt;strong&gt;top_3_rollNum$&lt;/strong&gt; as outer observable.concatMap takes each roll number from the outer observable and &lt;strong&gt;hits http request sequentially&lt;/strong&gt;(here we have 3 inner observables return by 3 http requests) &amp;amp; emits http response data into output observable(topper_marks$).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;concatMap never hits the next http request until the previous one completes.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Shorthand syntax for above code&lt;br&gt;
&lt;a href="https://media.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%2F7x84zt0xszzv3a45pxfs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F7x84zt0xszzv3a45pxfs.png" alt="Shorthand "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So,concatMap is used where sequence of execution matters.&lt;/p&gt;

&lt;p&gt;Thanks for reading! If you found this helpful please share!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>angular</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Angular : RxJS BehaviorSubject</title>
      <dc:creator>diptee</dc:creator>
      <pubDate>Fri, 29 Oct 2021 07:22:47 +0000</pubDate>
      <link>https://dev.to/dipteekhd/angular-behaviorsubject-p1</link>
      <guid>https://dev.to/dipteekhd/angular-behaviorsubject-p1</guid>
      <description>&lt;p&gt;As we know multiple components share the common data and always need updated shared data. In such scenarios most of the time BehaviorSubject is used which acts as a single store to hold updated shared data.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;BehaviorSubject is both observer and type of observable.&lt;/li&gt;
&lt;li&gt;BehaviorSubject always need an initial/default value.&lt;/li&gt;
&lt;li&gt;Every observer on subscribe gets current value.&lt;/li&gt;
&lt;li&gt;Current value is either latest value emitted by source observable using next() method or initial/default value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s implement BehaviorSubject to understand a concept better!&lt;/p&gt;

&lt;p&gt;For e.g In order tracking app, display &lt;strong&gt;total items in cart&lt;/strong&gt; and &lt;strong&gt;total items in wish list&lt;/strong&gt; on UI in header and dashboard section based on user action.&lt;/p&gt;

&lt;p&gt;Now we have three components -&lt;br&gt;
HeaderComponent, DashBoardComponent, TableComponent&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Ferwmls05vpdt8k46286h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Ferwmls05vpdt8k46286h.png" alt="UI"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://gvf3i.csb.app/" rel="noopener noreferrer"&gt;Run Live&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When user add items in the cart/wishList, total count needs to be updated in the header and dashboard component.&lt;/p&gt;

&lt;p&gt;1.First create a BehaviorSubject in order service which holds the initial state of order count ,so that it can be used by any component.&lt;br&gt;
&lt;a href="https://media.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%2Flm878gn53zi2wnvnu5bu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Flm878gn53zi2wnvnu5bu.png" alt="service"&gt;&lt;/a&gt;&lt;br&gt;
2.Now all observers(3 components) need to subscribe to source observable to get current value and show it on UI.&lt;br&gt;
&lt;a href="https://media.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%2Fsy0cm0xvr8z80y9d67ke.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fsy0cm0xvr8z80y9d67ke.png" alt="header"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.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%2F2ne51tzrgygyf33kw4pj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F2ne51tzrgygyf33kw4pj.png" alt="dashboard"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.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%2F5xwxjbma1o3jwl5ssx7a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F5xwxjbma1o3jwl5ssx7a.png" alt="table"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;3.When a user performs any action, call the next() method of BehaviorSubject. When the next() method gets called it will &lt;strong&gt;update current count&lt;/strong&gt; with &lt;strong&gt;new count&lt;/strong&gt; and &lt;strong&gt;notifies&lt;/strong&gt; updated count to all observers(3 Components) who subscribed to source observable.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fvjdclty5j36jwndhp7an.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fvjdclty5j36jwndhp7an.png" alt="Action"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This way BehaviorSubject makes components communication more effective.&lt;/p&gt;

&lt;p&gt;P.S. Don't forget to unsubscribe all subscriptions in ngOnDestroy() to avoid memory leaks &amp;amp; unexpected output.&lt;/p&gt;

&lt;p&gt;Thanks for reading! If you found this helpful please share!&lt;/p&gt;

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