<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Sabyasachi D Maven</title>
    <description>The latest articles on Forem by Sabyasachi D Maven (@imsabodetocode).</description>
    <link>https://forem.com/imsabodetocode</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%2F236552%2F963c41d9-7dd5-4cf0-9452-3535b75d5830.jpg</url>
      <title>Forem: Sabyasachi D Maven</title>
      <link>https://forem.com/imsabodetocode</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/imsabodetocode"/>
    <language>en</language>
    <item>
      <title>Dive into JavaScript Closures.</title>
      <dc:creator>Sabyasachi D Maven</dc:creator>
      <pubDate>Tue, 27 Dec 2022 04:07:57 +0000</pubDate>
      <link>https://forem.com/imsabodetocode/dive-into-javascript-closures-j7</link>
      <guid>https://forem.com/imsabodetocode/dive-into-javascript-closures-j7</guid>
      <description>&lt;p&gt;The closure is one of the most important concepts alongside a confusing keyword in the world of javascript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;The closure is created every time a function gets created at function creation time.&lt;br&gt;
It gives access to an outer function from the inner function.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's try to dive into the concept and understand what exactly are closures and where it comes into play.&lt;/p&gt;

&lt;p&gt;The definition of MDN, says "A closure is the combination of a &lt;strong&gt;function&lt;/strong&gt; and the &lt;strong&gt;lexical environment&lt;/strong&gt; within which that &lt;strong&gt;function was declared&lt;/strong&gt;."&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Closure is when a function can remember and access its lexical scope even when it's invoked outside its lexical scope.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Here, we need to understand the "lexical scope".&lt;/p&gt;

&lt;p&gt;Lexical scope states how a variable name is resolved within nested functions.&lt;/p&gt;

&lt;p&gt;Let's outline a basic closure.&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%2Fv46wx50iklq1k1pydh5n.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%2Fv46wx50iklq1k1pydh5n.png" alt="Image description" width="800" height="210"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;outerFunc() creates a local variable called outer and a function called innerFunc(). The innerFunc() the function is an inner function that is defined inside outerFunc() and is available only within the body of the outerFunc() function. Note that the innerFunc() function has no local variables of its own.&lt;/p&gt;

&lt;p&gt;However, since inner functions have access to the variables of outer functions, innerFunc() can access the variable outer declared in the parent function, outerFunc().&lt;/p&gt;

&lt;p&gt;This is an example of lexical scoping, which describes how a parser resolves variable names when functions are nested. The word lexical refers to the fact that lexical scoping uses the location where a variable is declared within the source code to determine where that variable is available.&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%2Fpigpegymha2anrfps49r.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%2Fpigpegymha2anrfps49r.png" alt="Image description" width="800" height="235"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In some programming languages, the local variables within a function exist for just the duration of that function's execution. Once outerFunc() finishes executing, you might expect that the outer variable would no longer be accessible.&lt;/p&gt;

&lt;p&gt;However, because the code still works as expected, this is obviously not the case in JavaScript.&lt;/p&gt;

&lt;p&gt;The reason is that JavaScript creates a "Closure". In the above case, myFunc is a reference to the instance of the innerFunc that is created when the myFunc gets executed.&lt;br&gt;
The instance of innerFunc maintains a reference to its lexical environment, within which the variable name "outer" exists. For this due reason, when the myFunc is invoked, the variable "outer" remains available for use.&lt;/p&gt;

&lt;p&gt;Let's take a slightly more complex example.&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%2F3fnj1lytx6nn5hlz8inw.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%2F3fnj1lytx6nn5hlz8inw.png" alt="Image description" width="600" height="442"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Can you guess what's going on with this code?&lt;/p&gt;

&lt;p&gt;When you are trying to invoke the add5() method, it has reference to the anonymous function which accepts an argument "y" but makeAdder() accepts an argument "x". So, how this lexical scope is getting created.&lt;/p&gt;

&lt;p&gt;Let's understand this.&lt;/p&gt;

&lt;p&gt;If you see the add5() and add10() method creates a closure.&lt;/p&gt;

&lt;p&gt;If you try to console the add5 method, it will display as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function(y) {
  window.runnerWindow.proxyConsole.log(y)
  return x + y;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The anonymous function still holds the value of "x" in the outer scope created from closure.&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%2Fc3cv7d7obhpytrgype3f.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%2Fc3cv7d7obhpytrgype3f.png" alt="Image description" width="800" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope by this point, you might have started making sense of how tricky the concept of "Closures" is but at the same time it is also interesting.&lt;/p&gt;

&lt;p&gt;Let's talk about the most common mistakes, developers make while understanding the closures.&lt;/p&gt;

&lt;p&gt;We will take a most asked interview questions on the concept of "Closures", i.e. the canonical for loop example.&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%2F3cnbkzxnqdy3ir7l5wbj.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%2F3cnbkzxnqdy3ir7l5wbj.png" alt="Image description" width="800" height="187"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At the first glance, if you are learning "Closures" for the first time, most of the answers you will get are like.&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%2Fv7u4ryqmi8g0etaegg1z.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%2Fv7u4ryqmi8g0etaegg1z.png" alt="Image description" width="408" height="142"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But, what actually you will get is like.&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%2F3ukvsathbz8bkie8u1am.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%2F3ukvsathbz8bkie8u1am.png" alt="Image description" width="514" height="146"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, the point here to ponder is Why? Let's talk about a few possible solutions.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using the "let" keyword.&lt;/li&gt;
&lt;/ol&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%2Fak969fzey1w9i26dmwum.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%2Fak969fzey1w9i26dmwum.png" alt="Image description" width="800" height="183"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using the let keyword, every closure binds the block-scoped variable, meaning that no additional closures are required.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using the "IIFE".&lt;/li&gt;
&lt;/ol&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%2Fya2u35ioc78thxl1o6fm.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%2Fya2u35ioc78thxl1o6fm.png" alt="Image description" width="800" height="221"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you see, we are using an IIFE function that takes an argument "index" which holds the current value of "i" at every iteration.&lt;br&gt;
Closely analyzing the IIFE block, the setTimeout() function holds the value of "index" even after it has been executed which was created using a "closure".&lt;/p&gt;

&lt;p&gt;That is the magic of using a "Closure".&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Returning a "function" just like a "closure".&lt;/li&gt;
&lt;/ol&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%2Fdmh4mojr3x79t4f1re43.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%2Fdmh4mojr3x79t4f1re43.png" alt="Image description" width="800" height="218"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you understand the above example, the solution here is self-explanatory, the setTimeout() function holds the current value at every iteration. As we are returning a function with IIFE too, it prints the desired output.&lt;/p&gt;

&lt;p&gt;Let's take another example of Closure.&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%2Fh4szmhfinetjy8kg1lcq.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%2Fh4szmhfinetjy8kg1lcq.png" alt="Image description" width="800" height="476"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I guess, now you would be able to make what should be the output?&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%2Fa97gp6t573w7vbjriff0.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%2Fa97gp6t573w7vbjriff0.png" alt="Image description" width="704" height="258"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you answer is above, then let's understand what's going on?&lt;br&gt;
If your answer is 1,2,3,4. I guess, you are on the right track.&lt;/p&gt;

&lt;p&gt;So, let's try to understand what's going in the code block.&lt;/p&gt;

&lt;p&gt;Always remember this point.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Closures - It is created when functions are created, not when they are invoked.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's see when we are invoking the getItems&lt;a href=""&gt;0&lt;/a&gt; on line # 13, let's start executing the code block.&lt;/p&gt;

&lt;p&gt;An empty array is initialized on line #2.&lt;/p&gt;

&lt;p&gt;Then the for loop on the line #3, variable "i" is hoisted. In other words, the "i" variable is declared outside of the for loop, and the variable is then mutated during each iteration.&lt;/p&gt;

&lt;p&gt;For each iteration, we're pushing an anonymous function to the array. You can push a value, an object or a function to an array. If we pushed i to the array, we would push the value of i at the time of iteration to the array.&lt;/p&gt;

&lt;p&gt;As we are pushing an anonymous function to the array, the anonymous function will return the value of "i" at the time of invocation. At the time of the invocation, the loop would have already run.&lt;/p&gt;

&lt;p&gt;By the time the loop completes, "i" will be equal to 5 because of the way that the increment expression in a for loop works.&lt;/p&gt;

&lt;p&gt;The only left out question is how can each anonymous function in the getItems array can access the value of i at each iteration?&lt;/p&gt;

&lt;p&gt;The definite answer is to bind the value of "i" at each iteration and that too at the creation time and not the invocation time ???&lt;/p&gt;

&lt;p&gt;Think about what's happening when we execute the anonymous function.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We're invoking an anonymous function from the global scope.&lt;/li&gt;
&lt;li&gt;addItems on line 1 is an outer function that returns a bunch of inner functions, because the items array that is returned contains a bunch of anonymous functions that are essentially an array of inner functions in the addItems function&lt;/li&gt;
&lt;li&gt;Each of these inner functions in the items array is accessing the i variable which is in the addItems function scope&lt;/li&gt;
&lt;li&gt;At the time that each inner function is invoked, the addItems function no longer exists, yet each of the inner functions maintains access to the i variable that is in the addItems scope&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, all the above conditions for a closure have been met.&lt;/p&gt;

&lt;p&gt;Now, only part left is to uniquely bind the "i" variable at each iteration.&lt;/p&gt;

&lt;p&gt;We can solve using "let" keyword and IIFE.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Using "let" keyword.&lt;/strong&gt;&lt;/em&gt;&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%2F542brqfpieead3s1ujc6.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%2F542brqfpieead3s1ujc6.png" alt="Image description" width="800" height="505"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Using "IIFE"&lt;/strong&gt;&lt;/em&gt;&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%2Faocm09g6yv2cvf9srd1r.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%2Faocm09g6yv2cvf9srd1r.png" alt="Image description" width="800" height="572"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By wrapping each items.push in an IIFE, we are ensuring that the j variable is privately scoped to this function, and as a result, the j variable will have a unique binding for each iteration.&lt;/p&gt;

&lt;p&gt;I hope the above examples will help you a lot in understanding the "Closures".&lt;/p&gt;

&lt;p&gt;Two important characteristics of determining a Closure.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;em&gt;An outer wrapping function is invoked, to create the enclosing scope.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;The return value of the wrapping function must include reference to at least one inner function that then has closure over the private inner scope of the wrapper.&lt;/em&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's see some practical use of the "Closures".&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;em&gt;Encapsulation - Emulating private methods&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Common web examples like "Adding buttons to a page to adjust the text size".&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Module pattern and Revealing Module pattern&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Closures can be used to make functions more memory efficient and performant - for example, ensure that a large array/object is only initialized once.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Closures are the fundamental concept of the functional programming. HOF, currying is not possible without closures.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Init functions - closures can be used to ensure that a function is only called once.&lt;/em&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I hope this article might have cleared most of the misconceptions around "closure". If you really like the article, please follow me.&lt;br&gt;
Happy coding. Keep learning. Keep exploring.&lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
    <item>
      <title>Angular: core.ts in detail</title>
      <dc:creator>Sabyasachi D Maven</dc:creator>
      <pubDate>Sat, 11 Jul 2020 21:51:06 +0000</pubDate>
      <link>https://forem.com/imsabodetocode/angular-core-ts-in-detail-jp8</link>
      <guid>https://forem.com/imsabodetocode/angular-core-ts-in-detail-jp8</guid>
      <description>&lt;p&gt;I was working on a feature module with deep nested components. And I need to improve the rendering of the component.&lt;/p&gt;

&lt;p&gt;So, I thought let's start debugging. I had idea about how the change detection works in Angular but was not sure from where this keywords comes like &lt;strong&gt;changeDetection, Input, Output, providers, host, selector, entrycomponents, encapsulation, etc..&lt;/strong&gt;, I then found the definition for all these keywords which we use in our angular components is present in &lt;strong&gt;core.ts&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A very common question asked &lt;strong&gt;What exactly is the difference between a Component and a Directive?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In a nutshell: ** A component is a self contained directive with the template in it.**&lt;/p&gt;

&lt;p&gt;We will try to demystify to make it more sense.&lt;/p&gt;

&lt;p&gt;Let's see the directive declaration in core.ts&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6omPMlQF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vgxevaxfv2g0l1vx8kxv.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6omPMlQF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vgxevaxfv2g0l1vx8kxv.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Basically, a directive is an interface with props like inputs, providers, selector, etc.**. So, this is what we try to use when we create a Component decorator.&lt;/p&gt;

&lt;p&gt;Now, if we look at the component declaration, which looks like as shown in the image below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BVw_jQlr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8f5pkyv5rgr01p0u7tgt.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BVw_jQlr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8f5pkyv5rgr01p0u7tgt.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we see the component declaration, we see the it extends the Directive interface (but properties are optional), it solely depends on the user which properties the user wants to use.&lt;/p&gt;

&lt;p&gt;So, our component declaration has template (inline html) or templateUrl (used as an external html file). Same goes with the styles (inline styles) and styleUrls (external css file).&lt;/p&gt;

&lt;p&gt;Next, comes the encapsulation property which is nothing but a enum called ViewEncapsulation. Attached is the image for the same.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TRZGfeRT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xedvlvrjowoazf51lm6f.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TRZGfeRT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xedvlvrjowoazf51lm6f.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then comes the changeDetection which type is also an enum called ChangeDetectionStrategy. Attached is the image for the same.&lt;/p&gt;

&lt;p&gt;The change detection itself a separate topic which is outside the context of this article.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One important thing to note that if you see the definition of the Component, the view and component is different, they are not exactly the same.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YcoyOU_1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/iltsvj7nly6nojdy67de.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YcoyOU_1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/iltsvj7nly6nojdy67de.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The entrycomponents property we try to deal with dynamic component which will mostly called from the feature module. (you can call directly from the component from Angular 9 Ivy), but entrycomponents property is now obsolete if you have upgraded your application to Angular version 9.0 or higher.&lt;/p&gt;

&lt;p&gt;I hope, next time if somebody asks you the question &lt;/p&gt;

&lt;p&gt;Let's dig in further..&lt;/p&gt;

&lt;p&gt;Have you ever wondered what goes behind the decorator like Component, Input and Output? &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Decorator in a angular is a keyword appended with @ like @Component, @Input, @Output, @Pipe, @Directive, etc....&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's see the definition of few decorators like Component, Input and Output (most used).&lt;/p&gt;

&lt;p&gt;If we see the Angular component change detection is uses Default which means if there is any change in the input, or browser events, it runs the entire change detection cycle. Attached is the image for the same.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Component&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--405zwH0T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xw4h65idf1jf9ak75wh6.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--405zwH0T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xw4h65idf1jf9ak75wh6.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope, it might makes sense of the things of the decorators usage in the day to day web development in Angular.&lt;/p&gt;

&lt;p&gt;Let's move on further to see what core.ts serves for us.&lt;/p&gt;

&lt;p&gt;Now, let's see the Input and Output decorator property.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TMhKmssO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/73djf17lvq95xpnopx6l.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TMhKmssO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/73djf17lvq95xpnopx6l.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kN9wW7ru--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ynycrlaq7f82pjb9u819.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kN9wW7ru--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ynycrlaq7f82pjb9u819.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Input and Output decorator is used for the parent child communication between the components. A good practice when you have few components within your module. Else, it is advisable to use the RxJS Subjects like (BehaviorSubjects, ReplaySubject, AsyncSubject, Subject).&lt;/p&gt;

&lt;p&gt;We generally have used the HostBinding and HostListener as a directive to listen to some events and react to it. Have you ever wondered what it contains under the hood.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HostBinding &amp;amp; HostListener&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cWHi3K5s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/hpo7n9k71ntlgjizqf11.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cWHi3K5s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/hpo7n9k71ntlgjizqf11.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We use the @HostBinding decorator to bind some property around the element. Let's say a border style, background-color, etc..&lt;/p&gt;

&lt;p&gt;Once, the binding is done, then we think on the event that binding should be done, where the @HostListener decorator comes into the picture. Attached is the image on how we use the host binding and host listener.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--z6OhbeGl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lphxu7p4r9fy1kuidhtb.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--z6OhbeGl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lphxu7p4r9fy1kuidhtb.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Moving further, let's see the @Pipe decorator.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pipe&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tbejj_KI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nx8ccxhxbxlrjtah8638.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tbejj_KI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nx8ccxhxbxlrjtah8638.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The pipe decorator is used for applying a transformation to the input value received. It has two property called the name and pure.&lt;/p&gt;

&lt;p&gt;The pure is of type boolean. It can be thought like a pure and impure function. Same it applies to the Angular.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pure Function:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Input parameters value determine the output. It's like on every same input, we get same output. The result can be shared across without the result be unchanged.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Impure:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;The same guarantee cannot be provided in the case of impure function. The internal state can be changed from outside and that is why we cannot share it across.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's move on..&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NgModule&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We all have used the root module or the feature module, but have you seen, what all the module offers within.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7kqNCjxc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mfciq6i64qlt0aqac4hs.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7kqNCjxc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mfciq6i64qlt0aqac4hs.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we see the NgModule declaration, we have providers prop for injecting the services, directives.&lt;/p&gt;

&lt;p&gt;The declarations property, we use for the components addition.&lt;/p&gt;

&lt;p&gt;The entrycomponents property can be used at the component level or the module level when trying to deal with the dynamic components in angular.&lt;/p&gt;

&lt;p&gt;Now, the injectable services. Let's see what it contains under the hood.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Services&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--N64B3a----/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2h9xmtcmwcp5ka3mfwxg.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--N64B3a----/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2h9xmtcmwcp5ka3mfwxg.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The povidedIn prop, where we want to inject this service. It has three values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;'root' : The application-level injector in most apps. The service has only instance throughout the application.&lt;br&gt;
'platform' : A special singleton platform injector shared by all applications on the page.&lt;br&gt;
'any' : Provides a unique instance in each lazy loaded module while all eagerly loaded modules share one instance.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's see the other props like useClass, useExisting, etc.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The providers prop has two property called provide and the useClass. If we have same name, the providers acts like a shorthand.&lt;br&gt;
Then, the question is when we can we use the useClass or useExisting.&lt;/p&gt;

&lt;p&gt;We know that different classes can provide the same service. Let's see the example.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--f_oWYOx6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/n2oqvhp6kcof6uu9ao6m.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--f_oWYOx6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/n2oqvhp6kcof6uu9ao6m.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The image description is self explanatory.&lt;/p&gt;

&lt;p&gt;Let's suppose an old component depends upon the OldLogger class. OldLogger has the same interface as NewLogger, but for some reason you can't update the old component to use it.&lt;/p&gt;

&lt;p&gt;When the old component logs a message with OldLogger, you want the singleton instance of NewLogger to handle it instead. In this case, the dependency injector should inject that singleton instance when a component asks for either the new or the old logger. OldLogger should be an alias for NewLogger.&lt;/p&gt;

&lt;p&gt;If you try to alias OldLogger to NewLogger with &lt;strong&gt;useClass&lt;/strong&gt;, you end up with two different NewLogger instances in your app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NId5HsJt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ohvt05unjqact5rgxpzs.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NId5HsJt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ohvt05unjqact5rgxpzs.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To avoid, two instances, we can the useExisting prop.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6IcqQ7lh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fz9pu8o34x6b6o47gbt4.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6IcqQ7lh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fz9pu8o34x6b6o47gbt4.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We all have used the viewchild or contentchild for the DOM manipulation as using as reference on the html element and use it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GJ10lHNr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kxmz09899x9v3vsohcqm.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GJ10lHNr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kxmz09899x9v3vsohcqm.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If you see the ViewChild uses the Query interface, it take a single element reference but if you want to use multiple reference, we use ViewChildren.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I hope, you might know few more details about the core.ts in angular the purpose it serves.&lt;/p&gt;

&lt;p&gt;There are many areas to learn. I have just covered the few which is mostly used in our day to day development with Angular.&lt;/p&gt;

&lt;p&gt;Happy coding. Keep learning. Keep exploring. 😊&lt;/p&gt;

</description>
      <category>angular</category>
      <category>typescript</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Angular + RxJS: From Pull to Push Based Strategy</title>
      <dc:creator>Sabyasachi D Maven</dc:creator>
      <pubDate>Mon, 06 Jul 2020 20:05:14 +0000</pubDate>
      <link>https://forem.com/imsabodetocode/angular-rxjs-from-pull-to-push-based-strategy-5005</link>
      <guid>https://forem.com/imsabodetocode/angular-rxjs-from-pull-to-push-based-strategy-5005</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QaJBi2OE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/r39gz2dei4b3rgmo2b3o.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QaJBi2OE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/r39gz2dei4b3rgmo2b3o.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Today, we will try to explore the shift we can make from pull based strategy towards the push based strategy.&lt;/p&gt;

&lt;p&gt;In the nutshell, we will see how we can minimize the use of explicit subscription as much as possible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The overhead of using the subscribe method is to unsubscribe explicitly in comparison to the async keyword which takes care of the unsubscription automatically.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We will kick-start with a simple component fetching the list of users and binding it to the dropdown.&lt;/p&gt;

&lt;p&gt;Let's do it then....&lt;/p&gt;

&lt;p&gt;For the sake of this article, we will be using the jsonplaceholder fake api. If we want to play around with the code, please visit this link &lt;a href="https://jsonplaceholder.typicode.com/"&gt;https://jsonplaceholder.typicode.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pull based strategy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, create a user model.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ogBwG1Q1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/l5djd81oe6ie2lpz9ypp.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ogBwG1Q1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/l5djd81oe6ie2lpz9ypp.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The model is the same as present in the api (&lt;a href="https://jsonplaceholder.typicode.com/users"&gt;https://jsonplaceholder.typicode.com/users&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;The second step, we will create a user service.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Qb1cp-1F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rsqyaohy3yg0alelt3ov.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Qb1cp-1F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rsqyaohy3yg0alelt3ov.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's try to render the data on the UI. So, our user component ts file will look something like this..&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JHrslQIo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4apga8pquiavx17p2sca.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JHrslQIo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4apga8pquiavx17p2sca.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's stop and figure out the code in the user component ts file.&lt;/p&gt;

&lt;p&gt;We are invoking a user service and passing the id on user selection drop-down which will fetch the user details.&lt;/p&gt;

&lt;p&gt;Secondly, on every drop-down selection, we are adding it to the subscription that we can later unsubscribe to avoid from the &lt;strong&gt;memory leaks&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is where an extra overhead is required whenever you are trying to subscribe explicitly.&lt;/p&gt;

&lt;p&gt;Let's move on to see the user component html file where we trying to loop and display the data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--l6dOXwjm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rhikizht6son9jf6e77e.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--l6dOXwjm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rhikizht6son9jf6e77e.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With the above approach, every time we are trying to change the user from the drop-down, we are relying on the change detection and in turn which pulls the data based on the user id provided to the api.&lt;/p&gt;

&lt;p&gt;If, we analyze, this approach has some limitations. I won't say the drawback which would in turn sounds like a negative connotation.&lt;/p&gt;

&lt;p&gt;So, think what could be those limitations.. 🤔🤔🤔🤔&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. It will always rely on the default strategy of Angular change detection cycle.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;2. Secondly, we cannot convert it to the Push based strategy if the particular user data is required by some other component.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;3. If the user response needs some transformation, we are doing to on every user selection. Thinking from the performance perspective, we can create a memoized function and get the data.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The above approach which we had elucidated is basically called the pull based strategy.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KBfXxxXG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ljw787hpg6qexwfsvfjy.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KBfXxxXG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ljw787hpg6qexwfsvfjy.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Push based strategy&lt;/strong&gt;&lt;br&gt;
Let's make a shift slowly towards the Push based strategy and will see the changes as the article progress.&lt;br&gt;
&lt;strong&gt;Basically, in a simple term, you can understand that if any data gets changed, we need to notified, if I am subscribed to it.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's start making changes with the user service.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SRMr2thx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nfqgql786jfu2r7lfk1m.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SRMr2thx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nfqgql786jfu2r7lfk1m.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, we are replacing the user property to a user subject (type: BehaviorSubject) and keeping it as private.&lt;/p&gt;

&lt;p&gt;So, instead of exposing the userSubject, we are trying to expose a user observable as readonly so that value cannot be changed after initialization.&lt;/p&gt;

&lt;p&gt;If we observe the loadUser method, whenever the new id is updated, the api is called and the user details is passed to subject. Whichever component wants to use the user details, can subscribe to details and use the data. &lt;/p&gt;

&lt;p&gt;If we see, we are now moving from the limitation phase to expansion phase.&lt;/p&gt;

&lt;p&gt;As, we had seen this article, our intention is to minimize the explicit subscription.&lt;/p&gt;

&lt;p&gt;So, if we see the loadUser method in the user service. There is still some room of improvisation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fiWKIkbb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wnqq4n6wzv1j7pyapj0h.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fiWKIkbb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wnqq4n6wzv1j7pyapj0h.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With the above implementation, I simply push the selected user id into the private user subject which acts as the source stream into the user$ observable. Every time a user’s id changes, a new http request is made.&lt;/p&gt;

&lt;p&gt;The output with this approach remains the same. So, the updated user component html will look as shown in below image.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--E-tfAVRU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rqad5iwihxe5wqt1mh1f.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--E-tfAVRU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rqad5iwihxe5wqt1mh1f.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Overall, if we see, we have removed the explicit subscription from the user service.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: shareReplay function is used for multi-casting.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So, the intent was to move from the traditional pull based strategy which is more sort of imperative style to the push based strategy which is more sort of reactive style.&lt;/p&gt;

&lt;p&gt;I hope it makes sense about using the #angular observables using rxjs in more &lt;strong&gt;reactive way&lt;/strong&gt;.&lt;br&gt;
Feel free to play around with this approach with some other scenarios.&lt;/p&gt;

&lt;p&gt;Also, please add comments or feedback about this article.&lt;/p&gt;

&lt;p&gt;Happy coding. Keep learning. Keep exploring. 😊&lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
      <category>typescript</category>
      <category>rxjs</category>
    </item>
    <item>
      <title>Angular: Debug Expression Changed Checked Error</title>
      <dc:creator>Sabyasachi D Maven</dc:creator>
      <pubDate>Sat, 04 Jul 2020 19:54:23 +0000</pubDate>
      <link>https://forem.com/imsabodetocode/error-expressionchangedafterithasbeencheckederror-expression-has-changed-after-it-was-checked-53gn</link>
      <guid>https://forem.com/imsabodetocode/error-expressionchangedafterithasbeencheckederror-expression-has-changed-after-it-was-checked-53gn</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KonUh4Fw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2vpb5ngugvb9mrd9sv7l.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KonUh4Fw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2vpb5ngugvb9mrd9sv7l.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope, most of the Angular developers out there sometime has faced the error "ExpressionChangedAfterItHasBeenCheckedError". &lt;br&gt;
But, have you wondered why?&lt;/p&gt;

&lt;p&gt;Let's try to demystify it out.&lt;/p&gt;

&lt;p&gt;The most obvious reason when you are trying to update any input property from the parent (top) component to the child (inner) component, you receive this error once the view (inner component) is initialized.&lt;/p&gt;

&lt;p&gt;First, let's see the error details on the console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1dp6clEc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/oq3t730bj9l9lqpbq22a.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1dp6clEc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/oq3t730bj9l9lqpbq22a.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's closely look at the error, it says when the component was loaded, the previous value was "null: Best wishes from Dad!!" but after the check the value got updated to "null: Got the Best wishes from Dad!!"&lt;/p&gt;

&lt;p&gt;So, what's happening in the background.&lt;/p&gt;

&lt;p&gt;Let's start with two simple component as shown in the diagram below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4fGVc6re--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ibpjx6dytv2onzp65gir.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4fGVc6re--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ibpjx6dytv2onzp65gir.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the app component html file, we are trying to pass the input property message with the value "Best wishes from Dad!!".&lt;/p&gt;

&lt;p&gt;Now, let's see what's going on in the child component ts file and html file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d7pdUMXC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/n67cl2ihgguirw6yv9ds.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d7pdUMXC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/n67cl2ihgguirw6yv9ds.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the ts file, we are trying to update the input message property as "Got the Best wishes from Dad!!".&lt;/p&gt;

&lt;p&gt;Let's go and debug the error in the console. If we see the error in the console..&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1dp6clEc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/oq3t730bj9l9lqpbq22a.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1dp6clEc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/oq3t730bj9l9lqpbq22a.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we try to get to the file which is view.ts, we will see the method as shown in the below diagram that contains the &lt;strong&gt;view definition&lt;/strong&gt; after being initialized.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HUH7kXmy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8ig8uwtjs5abp9nxsac1.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HUH7kXmy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8ig8uwtjs5abp9nxsac1.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If, you see the value of &lt;strong&gt;message&lt;/strong&gt; property in the app component which is &lt;strong&gt;Best wishes from Dad!!&lt;/strong&gt;.&lt;br&gt;
After the child component is initialized, we will see the the value of &lt;strong&gt;message&lt;/strong&gt; property in the child component which is &lt;strong&gt;Got the Best wished from Dad!!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RcQj8ha0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/99du8cydh8jnn8vx88le.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RcQj8ha0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/99du8cydh8jnn8vx88le.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, if you notice the &lt;strong&gt;oldValues&lt;/strong&gt; property is also not getting updated in both of the component view details.&lt;/p&gt;

&lt;p&gt;So, this where the error is popped up when the check is done among the previous value and current value in the error ts file under the viewDebug method as shown in the below diagram.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zfevEitq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tudbpogy66dr1x54wg3z.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zfevEitq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tudbpogy66dr1x54wg3z.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, now comes what are the ways to deal with this problem. There may be few but most important we will try to see the 3 ways.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. setTimeout function.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;2. Triggering change detection manually.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;3. asyncscheduler.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's make the changes with each of the aforementioned ways and check it's output.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. code change with the setTimeout implementation in the child component.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SqmFIPDj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rduomwumys32xgdq5ne0.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SqmFIPDj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rduomwumys32xgdq5ne0.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. code change with the manual change detection implementation in the child component.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JWWsAnA0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/qegdzdijun2g5l0a8doa.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JWWsAnA0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/qegdzdijun2g5l0a8doa.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. code change with the asyncscheduler implementation in the child component.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bzQtxFY0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ynts9aqyi95uy82d7f7f.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bzQtxFY0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ynts9aqyi95uy82d7f7f.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: asyncscheduler uses the setTimeout under the hood.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So, if we debug with any of the above 3 options, you will get this view details as shown in the below diagram.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--C9tIFvqf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/52cpbw9mjos2kc56ou1w.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--C9tIFvqf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/52cpbw9mjos2kc56ou1w.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Feel free to play around with the code and dig more about such #angular errors.&lt;/p&gt;

&lt;p&gt;I hope it makes sense about the #angular change detection.&lt;br&gt;
Happy coding. Keep learning. Keep exploring. 😊&lt;/p&gt;

</description>
      <category>angular</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
    <item>
      <title>RxJS Operators: distinctUntilChanged &amp; distinctUntilKeyChanged</title>
      <dc:creator>Sabyasachi D Maven</dc:creator>
      <pubDate>Sat, 04 Jul 2020 17:47:35 +0000</pubDate>
      <link>https://forem.com/imsabodetocode/rxjs-operators-distinctuntilchanged-distinctuntilkeychanged-1pgn</link>
      <guid>https://forem.com/imsabodetocode/rxjs-operators-distinctuntilchanged-distinctuntilkeychanged-1pgn</guid>
      <description>&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%2Fi%2Fkpjuohduqbs81nftexky.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%2Fi%2Fkpjuohduqbs81nftexky.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As per the official doc, RxJS is a library for composing asynchronous and event-based programs by using observable sequences.&lt;/p&gt;

&lt;p&gt;While performing the input search by keyword, we generally use the distinctUntilChanged operator.&lt;/p&gt;

&lt;p&gt;Today, we will try to explore both of the rxjs operators.&lt;/p&gt;

&lt;p&gt;Going by the official definition:&lt;br&gt;
&lt;strong&gt;distinctUntilChanged: It returns an Observable that emits all items emitted by the source Observable that are distinct by comparison from the previous item.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The sequence diagram for the &lt;strong&gt;distinctUntilChanged&lt;/strong&gt; is attached below:&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%2Fi%2F3e460b9mla0aswignb51.JPG" 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%2Fi%2F3e460b9mla0aswignb51.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we have a look at the compare function, it compares the distinct item from previous item from the source.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Also, we skip the compare function, it does the equality check by default. (applies to distinctUntilChanged &amp;amp;&amp;amp; distinctUntilKeyChanged)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If we are trying to compare for the primitive values, we will consider the &lt;strong&gt;distinctUntilChanged&lt;/strong&gt; operator. See the example below:&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%2Fi%2F6les1t6oize9s6h963ae.JPG" 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%2Fi%2F6les1t6oize9s6h963ae.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let's try to do some variations with the custom objects.&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%2Fi%2F2p5o2d52asgem0il55ro.JPG" 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%2Fi%2F2p5o2d52asgem0il55ro.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we see the example, we are trying to compare with "username" property, if it's distinct, we will take the value, otherwise it will be skipped.&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%2Fi%2Fjwg8ciy9uz6biw7sgig9.JPG" 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%2Fi%2Fjwg8ciy9uz6biw7sgig9.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's move on to the different flavor, which is &lt;strong&gt;distinctUntilKeyChanged&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;distinctUntilKeyChanged: Returns an Observable that emits all items emitted by the source Observable that are distinct by comparison from the previous item, using a property accessed by using the key provided to check if the two items are distinct.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The sequence diagram for the &lt;strong&gt;distinctUntilKeyChanged&lt;/strong&gt; is attached below:&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%2Fi%2Fbliaw7ti4m2yy4hxd3mz.JPG" 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%2Fi%2Fbliaw7ti4m2yy4hxd3mz.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we have a look at the compare function, it compares the distinct item from previous item from the source.&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%2Fi%2F9p8vqexslkiduuawqzuu.JPG" 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%2Fi%2F9p8vqexslkiduuawqzuu.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we see the example, we are trying to compare with "username" property. But, here we trying to just pass the key directly and get the same output. Here, we write less code. If it's distinct, we will take the value, otherwise it will be skipped.&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%2Fi%2F70ilxv574l9zcro72nlp.JPG" 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%2Fi%2F70ilxv574l9zcro72nlp.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If, we want to try out more examples, we can try to play-around with the code.&lt;/p&gt;

&lt;p&gt;We can use these two operators based on the usage. But, common is the &lt;strong&gt;input search keyword&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I hope this article have given some sense about the RxJS operators.&lt;br&gt;
Happy coding. Keep learning. Keep exploring. 😊&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>rxjs</category>
      <category>angular</category>
    </item>
    <item>
      <title>JavaScript: Revealing Module Pattern</title>
      <dc:creator>Sabyasachi D Maven</dc:creator>
      <pubDate>Tue, 19 May 2020 17:21:27 +0000</pubDate>
      <link>https://forem.com/imsabodetocode/javascript-revealing-module-pattern-3ji4</link>
      <guid>https://forem.com/imsabodetocode/javascript-revealing-module-pattern-3ji4</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ewuRT5uS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gm6g6f3zvnarrfcnsy2q.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ewuRT5uS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gm6g6f3zvnarrfcnsy2q.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s understand a bit of the theory behind the module pattern and the purpose it serves on implementing this pattern.&lt;/p&gt;

&lt;p&gt;The Module pattern was originally defined as a way to provide both private and public encapsulation for classes.&lt;/p&gt;

&lt;p&gt;It is further used to out-vie the concept of the classes by including the private and public properties/methods and variables inside a single object, thus encapsulating the underlying details (either implementation details, properties, variables, etc.) from the global scope.&lt;/p&gt;

&lt;p&gt;If we try to summarize the gist of the Module pattern, it serves the below purposes:&lt;br&gt;
&lt;strong&gt;&lt;em&gt;Code encapsulation&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;Data privacy&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Let’s move on to understand the pattern with the help of code.&lt;/p&gt;

&lt;p&gt;The first step is to create a module.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vJWUEI6u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u8p03ool0t23li7minv2.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vJWUEI6u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u8p03ool0t23li7minv2.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The next step is to create a public method to our module so that the user can invoke this public method. For exposing the method, we will return an object with the methods defined.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--J2cf2oDK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vfbd3zvlg0m6ndzscti1.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--J2cf2oDK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vfbd3zvlg0m6ndzscti1.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we try to execute the above method, we will see that we get the output from the public method being exposed to our calcOrderModule. Otherwise, if we try to execute the private property or methods from the module function, we get undefined. Just to make a small identification change, the private property or the method starts with the underscore sign(_).&lt;/p&gt;

&lt;p&gt;Just to recapitulate, we have performed 3 steps:&lt;br&gt;
&lt;strong&gt;Create a module using an IIFE.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Create a private property and a private method.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Create a public method to be exposed outside our module.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So until now we have covered the basics of the Module pattern. Let’s move on with the Revealing module pattern.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The way of implementing the module pattern and the revealing module pattern is almost similar with the only difference is that the latter returns the object while exposing the public method from our module.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sidenote: A module pattern is similar to factory functions as we create an object without the nuances of using the “new” keyword.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Without further delay, we will try to implement the code using the revealing module pattern and later will emphasize what’s going on with the code snippet.&lt;/p&gt;

&lt;p&gt;Let’s try to add a few more private properties and the private methods to our existing module.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AxmfpI-w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/pf98jgc9dv0cqgxy63fy.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AxmfpI-w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/pf98jgc9dv0cqgxy63fy.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, we have declared a few private properties and 2 private methods to calculate the discount on the shopping cart item list.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KkKsYBL2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ik0u2laqhcmsy4ifeuyj.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KkKsYBL2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ik0u2laqhcmsy4ifeuyj.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This method will give us the count of the items and the price if any items from the list have any discount applied to it.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--81tsPdwE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/izk376pu6itbttdnavrh.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--81tsPdwE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/izk376pu6itbttdnavrh.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is our final method which will return an object for us with the total count and the total order value.&lt;/p&gt;

&lt;p&gt;Finally, the public method which we return from our module will invoke the above method.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m7vYmL1H--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rn4jx1g1intpc8dn55zg.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m7vYmL1H--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rn4jx1g1intpc8dn55zg.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code snippets in chunks above shows the implementation of the revealing module pattern.&lt;/p&gt;

&lt;p&gt;Now, let’s try to execute the code by providing the input and check its output.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--64zok96n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bbuw07yjk11gvgkg6292.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--64zok96n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bbuw07yjk11gvgkg6292.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you try to execute the above code, we will observe that while getting the private property, we get undefined. Most of the code implementation is self-explanatory.&lt;/p&gt;

&lt;p&gt;As promised, here is below attached entire code snippet.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_Ck1d9w4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gh2nuv80rvgh9l9ns73t.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_Ck1d9w4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gh2nuv80rvgh9l9ns73t.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope, you might have got some ideas about the module and revealing module pattern.&lt;/p&gt;

&lt;p&gt;This way we can achieve the data encapsulation and privacy which is the idea behind using the module pattern or revealing module pattern.&lt;/p&gt;

&lt;p&gt;Happy coding. Keep learning. Keep exploring.😊 &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>design</category>
      <category>es6</category>
    </item>
    <item>
      <title>DOM Manipulations in Angular</title>
      <dc:creator>Sabyasachi D Maven</dc:creator>
      <pubDate>Tue, 19 May 2020 05:55:39 +0000</pubDate>
      <link>https://forem.com/imsabodetocode/dom-manipulations-in-angular-1dh1</link>
      <guid>https://forem.com/imsabodetocode/dom-manipulations-in-angular-1dh1</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--j-QzsaTs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/z959x1m6l5ri8jul2mdr.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--j-QzsaTs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/z959x1m6l5ri8jul2mdr.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While working as the front-end developer, the most important point to focus is to manipulate the DOM as less as possible which in turns may improve the web performance.&lt;/p&gt;

&lt;p&gt;As we all know that Angular was developed to run on all platforms like browser, webworkers, universal, pwa.&lt;/p&gt;

&lt;p&gt;In this article, we will emphasize on the following points.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Correct approach working with DOM.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;2. Tools to work with DOM.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;3. View and rendering layer architecture.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Lets’ start...&lt;/p&gt;

&lt;p&gt;Any Angular application has rendering and presentational logic. &lt;br&gt;
I know most of us know about it. Still, we will try to decipher the difference between them.&lt;br&gt;
Suppose, we have two list of the data that needs to be rendered as the left and the right panel. On the left panel, we have menu items by category and based on the category, we display the items in the right panel.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AcBuiloc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v7ud9y9b5r0t8g98ul15.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AcBuiloc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v7ud9y9b5r0t8g98ul15.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The presentational logic goes to the component. The component is sort of the container with the view/ presentations specific data.&lt;/p&gt;

&lt;p&gt;So, now the question is where to put the rendering logic.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pPe86x17--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0389xfoymdjvr0lpesz3.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pPe86x17--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0389xfoymdjvr0lpesz3.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, there is layer between the component and the DOM element is the directives where we encapsulate the templates and perform the manipulation within the directives. Then, we pass the data binding to the directives and render the business related data to the UI.&lt;/p&gt;

&lt;p&gt;So, just to recapitulate the points whatever we have learned so far.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Split the presentational and rendering logic.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;2. Put the presentational logic to the components.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;3. Put the rendering logic to the directives.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;4. Use the data-binding mechanism for the communication.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So, using the above approach has 2 benefits overall.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Presentational logic can be reused on the other platforms.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;2. Rendering logic reuse across the application.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Angular, by default, don’t provide the built-in directive to the above approach. So, we need to implement the custom directive for it which we will see it in action going forward.&lt;/p&gt;

&lt;p&gt;Let’s try to use a custom directive inside a component.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--g8P5lJ55--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xa5p0ar9lralokbq54pl.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--g8P5lJ55--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xa5p0ar9lralokbq54pl.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Nothing, great at this point until we see how the custom directives is implemented.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hbjiFUh_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/qk77nu9qdgprz7166net.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hbjiFUh_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/qk77nu9qdgprz7166net.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, if we see the above code, this is how the custom directive is implemented. But, if we look the code, we are directly accessing the api to manipulate the DOM which Angular doesn’t recommends.&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;The reason is the plaform dependence as we have discussed above.&lt;/p&gt;

&lt;p&gt;The Non-DOM environments like Universal, Webworker. If we try to run the directive inside WebWorker, it will result in an error.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PBIzAhdZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1wi6k9089c8lw4f8wteu.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PBIzAhdZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1wi6k9089c8lw4f8wteu.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But, we have tools which makes the DOM access safe. Here comes the point, where will uncover the ways to manipulatet the DOM in Angular.&lt;/p&gt;

&lt;p&gt;Before manipulating DOM, first we need to understand what operations are will going to perform on the DOM.&lt;/p&gt;

&lt;p&gt;Generally we can categorize into two parts as shown in the below diagram.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5-GKqADv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/yj19am3950u0222ijzbz.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5-GKqADv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/yj19am3950u0222ijzbz.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In a nutshell, it as follows:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Renderer: Use when we need to change/read DOM element properties.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;ViewContainerRef: Use when we need to modify DOM hierarchy.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We will try to see both these ways in much detail.&lt;/p&gt;

&lt;p&gt;Renderer (Renderer 2): It marks the direct DOM access safe (platform independent).&lt;/p&gt;

&lt;p&gt;Few DOM manipulation methods as follows:&lt;br&gt;
&lt;strong&gt;&lt;em&gt;setAttribute&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;removeAttribute&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;addClass&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;removeClass&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;setStyle&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;removeStyle&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If we look the implementation of the custom dirctive using Renderer2 in recommended Angular way.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Eb01VF1K--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mugpv87glukf1ddi1r9s.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Eb01VF1K--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mugpv87glukf1ddi1r9s.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, if we think how Renderer service is making this code implementation, lets’ visualize the Renderer architecture how it makes possible.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ep5BQ1l---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xyjr8i0uqltjafbxl5ga.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ep5BQ1l---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xyjr8i0uqltjafbxl5ga.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The main piece in connecting the dot is the DOM adapter which acts as a bridge between the Native API( browser) / platform and the framework (Renderer). It’s implementation is always platform specific.&lt;/p&gt;

&lt;p&gt;But, there is one more point to note that each component in Angular depends on how to render the view. We have used the encapsulation like &lt;strong&gt;Native, Emulated (default), ShadowDOM, None&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So, Renderer service depends on the component encapsulation before the DOM is manipulated.&lt;/p&gt;

&lt;p&gt;In turn, the Angular create the dedicated Renderer as per the component.&lt;/p&gt;

&lt;p&gt;So, this is the reason we can’t inject the Renderer in the standalone service.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nHylXFEx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/39jdtx9u7bdrowbme2op.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nHylXFEx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/39jdtx9u7bdrowbme2op.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hope the above diagram makes sense the connection between the View Encapsulation and Renderer.&lt;br&gt;
For the layer of bit comfortability, attaching a diagram to get idea different Renderer abstraction on different platforms.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--64cKglgd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2pf272u0zpik5u3ewcqw.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--64cKglgd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2pf272u0zpik5u3ewcqw.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Sidenote: Don’t use Renderer for the DOM hierarchy modification.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Moving ahead with the other piece of the DOM modification is the ViewContainerRef.&lt;/p&gt;

&lt;p&gt;So, to understand the ViewContainerRef, we first need to understand the &lt;br&gt;
relationship between the View and the DOM.&lt;/p&gt;

&lt;p&gt;Lets’ visualize the diagram.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gTxx4ONt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/eg4xyg153uwr6yp8lsvi.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gTxx4ONt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/eg4xyg153uwr6yp8lsvi.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, when we create a component in Angular, the compiler in turns takes the set of the template instructions from the component and creates the view. In turn, the view creates the node elements (depends on the template).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;View is core-concept of Angular. It’s an abstraction which associates the DOM elements defined in the component templates.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, to our surprise, we define the component, but how the view gets created. The angular compiler does that. Let’s see this diagram.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QEg4Ggld--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4p66huu7ln3jh5urvv2i.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QEg4Ggld--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4p66huu7ln3jh5urvv2i.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If anyone have worked on the AOT compilation, we might have seen the viewfactory. But, what exactly is the ViewFactory?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ViewFactory can be interpreted as a set of instructions (what type of view and DOM nodes) getting from the component template and which in turns creates the view.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Basically, the instruction set has 1–1 relationship between the components and its view which gets resolved by the View Factory. We can take the example of the dynamic component creation in Angular.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One important to note that the Angular Change Detection works with the View. Any changes in the view reflec the changes in the DOM elements defined in the component. So, its a one way process.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Zi63KWf9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vpqnj527cj44e91hufd8.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Zi63KWf9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vpqnj527cj44e91hufd8.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let’s remove the span element from the DOM. We will see that the structure of the DOM gets changed but the structure of the view is intact.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The answer is even if we have removed the element from the DOM, its’ reference is still present in the View which creates the possibility for the memory leak.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine, we have a component with the lot of DOM elements and removing one element from the component tree still have its reference in the view nodes. So, when the angular change detection mechanism runs, it will still run on the removed element from the DOM which has it’s reference in the view. So, in order to access the DOM safely, we use the ViewContainerRef.&lt;/p&gt;

&lt;p&gt;ViewContainerRef: Makes DOM hierarchy changes safe.&lt;br&gt;
Creating views:&lt;br&gt;
&lt;strong&gt;1. createComponent (dynamic component creation) — Created from the view factory. Component that is not found other component templates.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;2. createEmbeddedView (TemplateRef) — In Angular, we reference template using the Template ref (a reference to the compiled template).&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;View manipulation methods&lt;br&gt;
&lt;strong&gt;&lt;em&gt;insert&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;attach&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;detach&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;remove&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;move&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To conclude this article, just a few takeaway points.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;put presentational logic into components.&lt;/li&gt;
&lt;li&gt;use custom directives to manipulate the DOM to make it more platform independent and DOM safe.&lt;/li&gt;
&lt;li&gt;Use Renderer when direct access to native DOM api is necessary.&lt;/li&gt;
&lt;li&gt;Use templating technique and ViewContainers to modify the DOM hierarchy.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Hope, you have got some sense on how the work with the DOM manipulations in Angular.&lt;/p&gt;

&lt;p&gt;Happy coding. Keep learning. Keep exploring. 😊&lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
      <category>node</category>
      <category>es6</category>
    </item>
    <item>
      <title>RxJS Operators: throttleTime &amp; debounceTime</title>
      <dc:creator>Sabyasachi D Maven</dc:creator>
      <pubDate>Sat, 25 Apr 2020 14:47:04 +0000</pubDate>
      <link>https://forem.com/imsabodetocode/rxjs-operators-throttletime-debouncetime-3pdf</link>
      <guid>https://forem.com/imsabodetocode/rxjs-operators-throttletime-debouncetime-3pdf</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Cc2Rj13E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kpjuohduqbs81nftexky.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Cc2Rj13E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kpjuohduqbs81nftexky.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As per the official doc, RxJS is a library for composing asynchronous and event-based programs by using observable sequences.&lt;/p&gt;

&lt;p&gt;One of the most used operators while performing search input is the debounceTime and throttleTime but there are variations between these two on their usage.&lt;br&gt;
Let's deep dive about these operators.&lt;/p&gt;

&lt;p&gt;As per official definition:&lt;br&gt;
&lt;strong&gt;debounceTime : Emits a value from the source Observable only after a particular time span has passed without another source emission.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A classical example of the debounceTime is the &lt;strong&gt;type-ahead/ autocomplete&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The marble diagram for the debounceTime is shown below:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5aWtYR35--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v2p2dbt0bdz9w149w3cr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5aWtYR35--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v2p2dbt0bdz9w149w3cr.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we observe the marble diagram, the debounceTime wait is 20ms, if the user performs any value search within 20ms, the value which gets captured for the search only after the debouncetime is elapsed. So, if we observe, the value "b" is not captured for the search value.&lt;/p&gt;

&lt;p&gt;Let's see an practical example for the debounceTime.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rW3BUbVq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/aspotfgx3le3h8cdveyw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rW3BUbVq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/aspotfgx3le3h8cdveyw.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have created an html input tag with using ngmodel as the above code is much self-explanatory.&lt;/p&gt;

&lt;p&gt;Let's focus on its implementation which goes as.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--40G_pmA1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jf7y0oz4h90dynvet75u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--40G_pmA1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jf7y0oz4h90dynvet75u.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the above info, we have used an wikipedia search api, and on the constructor method, we have called the fetchdata method with the argument of the input value search passed to it.&lt;/p&gt;

&lt;p&gt;Don't worry about the other operators in the image like switchMap, distinctUntilChanged. Our main focus will be on the debounceTime. &lt;/p&gt;

&lt;p&gt;So, if we try to run the above example, we will find out that the latest value which will be passed as a search parameter after the 4000ms has been elapsed.&lt;br&gt;
Just for the reference, the search value will yield as shown below:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bfEwD0vz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2zs7ykf37sl049xcg3bd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bfEwD0vz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2zs7ykf37sl049xcg3bd.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Please feel free to play-around with the code to make yourself more comfortable with the debounceTime operators.&lt;br&gt;
Further moving on, let's see the second operator throttleTime with the same example. We will just update the debounceTime to throttleTime to observe the behavior.&lt;/p&gt;

&lt;p&gt;As per the official docs.&lt;br&gt;
&lt;strong&gt;throttleTime: Emits a value from the source Observable, then ignores subsequent source values for duration milliseconds, then repeats this process.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If we try to demystify the above definition, we will see the value which gets emitted will be captured and then it delays the amount of the time which has been provided. Once, the time has elapsed, it will again start capturing the subsequent values and the process continues.&lt;br&gt;
Firstly, let's observe the marble diagram for the throttleTime as per the RxJS docs.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MTtI1kD0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ylthpx7eangdp9amf5p7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MTtI1kD0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ylthpx7eangdp9amf5p7.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Try to observe the marble diagram, the value which gets emitted first was "a", then there is some throttleTime(let's say 20), the value "x" got ignored and once the time had elapsed, the next value which was captured is "y" and similarly this process continues.&lt;/p&gt;

&lt;p&gt;The most viable use of the throttleTime where it can make more sense is the button click rate or the double click, if the user clicks the button multiple number of times.&lt;br&gt;
Overall, the throttleTime makes more sense when we try to limit the number of events that happens in sequential manner.&lt;/p&gt;

&lt;p&gt;Let's see the implementation of it.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--keMbOl8x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kzc82y0kpqo3aw3zeqo9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--keMbOl8x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kzc82y0kpqo3aw3zeqo9.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As already explained about the throttleTime, we will observe after executing the piece of the code above, we will see that 1 event has gone through, throttleTime will allow events to go through again after 4000ms. But only for events created after the 4000ms. This means that when a user types ro within 4000ms the suggestions will only represent the return value r. When the user types m it will search for the value rom.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4w5C3FeZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/qc1sli59h6zc526t6hdn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4w5C3FeZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/qc1sli59h6zc526t6hdn.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we observe the above diagram, the user has entered the search value "ro" but the value which gets passed is only "r" which means the first events which took place was with the value "r" and the next event will perform after 4000ms has elapsed. So, there is a delay of 4000ms, the next event where the value will be searched as "rom"; See the below image as follows:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IVQrtnTq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/23sydztv65hga7kfx809.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IVQrtnTq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/23sydztv65hga7kfx809.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My suggestion is to play around with the code and get yourself more to explore about this operators in more detail.&lt;br&gt;
Most used areas for these operators as follows:&lt;br&gt;
&lt;strong&gt;debounceTime:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. AutoComplete&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;2. Typeahead&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;throttleTime:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Limit click rate&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;2. Double Click&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I hope this article have given some sense about the RxJS operators.&lt;br&gt;
Happy coding. Keep learning. Keep exploring. 😊&lt;/p&gt;

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