<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Stavros Droutsas on Medium]]></title>
        <description><![CDATA[Stories by Stavros Droutsas on Medium]]></description>
        <link>https://medium.com/@stavrosdro?source=rss-7ab07974fa89------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*6kEdfoGlLUI6l-nXUOsqJA.jpeg</url>
            <title>Stories by Stavros Droutsas on Medium</title>
            <link>https://medium.com/@stavrosdro?source=rss-7ab07974fa89------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 06:11:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@stavrosdro/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Lighthouse is a chore!]]></title>
            <link>https://itnext.io/lighthouse-is-a-chore-c2c21ee4e0af?source=rss-7ab07974fa89------2</link>
            <guid isPermaLink="false">https://medium.com/p/c2c21ee4e0af</guid>
            <category><![CDATA[github]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[lighthouse]]></category>
            <category><![CDATA[seo]]></category>
            <dc:creator><![CDATA[Stavros Droutsas]]></dc:creator>
            <pubDate>Sat, 28 Dec 2024 14:22:28 GMT</pubDate>
            <atom:updated>2024-12-28T19:32:45.503Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*7E09p0pMFZpQb6f0r69NiQ.jpeg" /></figure><p>Do you remember the last time that you had to investigate an issue with Cumulative Layout Shift (CLS from now on)? A requirement to add a couple of alt attributes in the images? It is known that the above things affect the lighthouse score of the website in terms of performance and accessibility and Google considers those scores for the SEO.</p><p>So what is the recommended action? Should we run lighthouse after each change in images to make sure that we didn’t affect the metrics in a negative way? What about the performance? Should we check the score after each MR to make sure that our changes have zero impact?</p><p>Actually yes, but running lighthouse locally is not the way to go. First of all different machines may create different scores (based on the hardware). Then we have to do the extra step to create a production build and measure this instead of the development runtime. And do this in a browser without extensions to make sure that the environment is pure. The worst part is that it adds another manual action in the BAU tasks. The great part is that we can avoid all those things and automate everything with a single npm package, <a href="https://www.npmjs.com/package/@lhci/cli"><em>Lighthouse CI CLI</em></a>.</p><p>In order to do this we have to do the following</p><ul><li>create a sample project. Today we will use Vuejs</li><li>add a lighthouse rc file</li><li>configure GitHub properly</li></ul><h4>Create a project</h4><p>To create a Vue application from scratch you need nodejs and npm. I will use a utility container to avoid messing with different node versions or nvm and make it easier for everyone. You will only need Docker.</p><p>docker run -it --rm --user node -v $(pwd):/app --workdir /app node:22-alpine npm create vue@latest</p><h4>Lighthouse rc</h4><p>With the following config lighthouse will raise an error if the performance or accessibility score is below 0.8 . In order to measure it, it will start a dev server with a production build by running npm run build &amp;&amp; npm run preview and check the URL http://localhost:4173 .</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/698fa4fdabe9b7dee4519e57bd0e787e/href">https://medium.com/media/698fa4fdabe9b7dee4519e57bd0e787e/href</a></iframe><h4>GitHub tuning</h4><p>This is the final piece of the puzzle. We have to configure GitHub to protect our repo. A nice way to go is to run lighthouse on every PR. With a proper configuration no one can merge code which scores less than our predefined thresholds. This is a 2 step process.</p><p>The first step is to create a workflow. I created this file .github/workflows/.lighthouse-ci.yaml with the following content</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa997462e4d9093c486414f1fe1d08b3/href">https://medium.com/media/fa997462e4d9093c486414f1fe1d08b3/href</a></iframe><p>Commit the file and push. To test this action open a pull request and add a couple of images without the alt attribute. You will see something like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tOM_hH1zUbvVvkBjrRPFPQ.png" /><figcaption>Action works, LHCI raised error, but the merge is still possible</figcaption></figure><p>The second (and last) step is to configure GitHub from the website app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L2ZbfX0PNXsCBaFSe9Pj7Q.png" /><figcaption>Navigate to the repository &gt; Settings &gt; Branches &gt; Add classic branch protection rule</figcaption></figure><p>You have plenty of options. In my case I have to protect the master branch and I will only enable the checks before merging.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iTZsPbdYV1kbKE3ILWihHg.png" /><figcaption>Remember to type in the input and select the “Lighthouse CI” from the list</figcaption></figure><p>That’s all! Let’s see the PR now!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wDr5-1aGS1GFIyi6pRUTTQ.png" /><figcaption>The button is finally disabled. If a merge is urgent users can bypass protection</figcaption></figure><h4>Final Thoughts</h4><p>This the bare minimum imo. Lighthouse will run before merging and it will block commits that score lower than our threshold. It doesn’t mess with the BAU tasks and the maintenance cost is minimum. You can find more info about lighthouse ci <a href="https://github.com/GoogleChrome/lighthouse-ci/blob/main/docs/getting-started.md"><em>here</em></a>.</p><p>You can check the code <a href="https://github.com/stavrosdro/vue-lhci"><em>here</em></a><em>. </em>Try to open a couple of PRs and see how the actions work. Let me know if you use other checks e.g. <a href="https://github.com/LironEr/bundlemon"><em>BundleMon</em></a> offers great value as well and I can cover it in the future.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c2c21ee4e0af" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/lighthouse-is-a-chore-c2c21ee4e0af">Lighthouse is a chore!</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[You are using Pinia wrong!]]></title>
            <link>https://itnext.io/you-are-using-pinia-wrong-93d93c6c9c9d?source=rss-7ab07974fa89------2</link>
            <guid isPermaLink="false">https://medium.com/p/93d93c6c9c9d</guid>
            <category><![CDATA[store]]></category>
            <category><![CDATA[action]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[pinia]]></category>
            <dc:creator><![CDATA[Stavros Droutsas]]></dc:creator>
            <pubDate>Wed, 04 Sep 2024 21:22:04 GMT</pubDate>
            <atom:updated>2024-09-05T06:56:36.216Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y2ihuy6eVltzQxT2wKcYxg.jpeg" /></figure><p>Recently I moved away from Vuex and started working with Pinia. I saw that there are 2 possible syntaxes and I created a very basic example by following the Option Stores approach:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/28c1b41592fd51a4a46f172023b4a453/href">https://medium.com/media/28c1b41592fd51a4a46f172023b4a453/href</a></iframe><p>I also added a unit test to make sure everything is working fine:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8deb33a376e8c6ed96e40033f72a7777/href">https://medium.com/media/8deb33a376e8c6ed96e40033f72a7777/href</a></iframe><h3>Setup Stores</h3><p>As you can see in the documentation, there is this <a href="https://pinia.vuejs.org/core-concepts/#Setup-Stores"><em>amazing</em></a> alternative. Setup Stores! We can benefit from the composition API syntax and create something minimal and comfortable to work with. We cannot miss this opportunity!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f0f7e22916da8542d78417ed2012d298/href">https://medium.com/media/f0f7e22916da8542d78417ed2012d298/href</a></iframe><h3>The Problem</h3><p>Unfortunately the test fails:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/998/1*4YYtVWc62BRV7tste7Sqqg.png" /></figure><p>The strange part is that the logger actually works! You can see both logs in the first lines of the console but the number of calls in the spy function is still 0 .</p><h3>A disgusting workaround</h3><p>What about the failed test? A possible cause is that we are trying to spy on the exposed action but the executed function is different. So, in order to keep everything aligned we have this alternative:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/089c0e613b05966dfa5f888d3d516db8/href">https://medium.com/media/089c0e613b05966dfa5f888d3d516db8/href</a></iframe><p>Now all the tests pass successfully. Satisfied?</p><h3>Final Thoughts</h3><p>It looks like the setup stores’ syntax is more complex that it looks. The function(s) are mapped to actions, we can use computed as the new getters, ref and reactive for the state, we have to put .value almost everywhere, but there are other changes under the hood which create a different structure in the end. It’s much more than an alternative syntax to achieve the same thing.</p><p>It can also cause more complex problems. Many developers <a href="https://pinia.vuejs.org/core-concepts/actions.html#Subscribing-to-actions"><em>subscribe to actions</em></a> by using store.$onAction to add side effects. It’s a nice and clean way if you want to add analytics. I had to follow the same workaround to be able to keep it working.</p><p>What do you think? Any other cases that setup stores made everything more complex and ruined your day?</p><p>The example is simple, but you can find the code <a href="https://github.com/stavrosdro/pinia-testing"><em>here</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=93d93c6c9c9d" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/you-are-using-pinia-wrong-93d93c6c9c9d">You are using Pinia wrong!</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduce Mutation testing]]></title>
            <link>https://itnext.io/introduce-mutation-testing-7560315ca70e?source=rss-7ab07974fa89------2</link>
            <guid isPermaLink="false">https://medium.com/p/7560315ca70e</guid>
            <category><![CDATA[mutation-testing]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[stryker]]></category>
            <category><![CDATA[vuejs]]></category>
            <dc:creator><![CDATA[Stavros Droutsas]]></dc:creator>
            <pubDate>Sun, 04 Feb 2024 22:25:56 GMT</pubDate>
            <atom:updated>2024-02-05T01:54:40.417Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Disclaimer: While discussing mutation testing, it’s crucial to clarify that the term “mutation” refers specifically to the process of changing parts of the code during testing, rather than the broader concept of mutability or immutability in programming.</blockquote><p>Many of us write code. Not so many write tests, <em>unfortunately</em>. But how many wonder if our tests are good <strong>enough</strong>?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/586/1*Bg6bjXzLlUKKB3UpEDqTsA.jpeg" /></figure><p>Let’s start on what I want from a unit test and how I can describe a good test:</p><blockquote>A test is good if it fails when a change in the code creates an undesired outcome.</blockquote><p>But most tests are not written in this way. Most tests assert that a specific path is followed under very certain circumstances.</p><p>So they are written with focus on 2 things:</p><ul><li>Verify that a very specific action creates an expected result</li><li>Maintain it without much effort</li></ul><p>Especially in the area of unit tests, which represent the majority of our test suite, we have some tunnel vision blocks of code and rely on them to believe that for the largest part, the app operates as expected. And this is great! Instead of having huge tests with strange spaghetti logic that fail all the time, change on every commit by many developers, take a century to run because they cannot run in parallel, we extracted this logic in small unit tests. We all dislike merge conflicts, right? Imagine facing merge conflicts in tests as well! Small unit tests are highly effective and we have to empower them further to increase their value and maximize their potential.</p><p>A very simple way to start enhancing the tests is to check for the code coverage. There are many thresholds to increase the confidence level of the maintainers, some want &gt;50% in most cases, but of course there are tricky parts that need 100% coverage to verify that everything is OK.</p><p>Hmm… Does 100% coverage suffice? Let’s contrast this viewpoint with a previous statement: how do these tests respond to an unexpected code modification?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/800f7cdceb2c8229f3862abded59509a/href">https://medium.com/media/800f7cdceb2c8229f3862abded59509a/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd02d2c2bacab7ffb794aa9347f9bb36/href">https://medium.com/media/dd02d2c2bacab7ffb794aa9347f9bb36/href</a></iframe><p>A very simple function and a very simple test. 100% code coverage. Any ways to make it better? How can we improve it? An obvious suggestion is instead of adding 0+0 and verify that it equals 0, use 1+1 and verify that the result equals 2. Why? Because the above test will create a false positive result if someone replaces the + with a - in the line 2 of the function.</p><p>This is how we can harden our existing tests, by considering edge cases and include suggestions like the above. Of course there is a tool out there that can make this for us.</p><h3>Mutation testing with Stryker Mutator</h3><p>First, let’s introduce a couple of definitions.</p><p>Mutation: a change in the source code<br>Killed mutation: a change in the code that created a failed test — good scenario<br>Survived mutation: a change in the code that passed undetected and all tests are successful — bad scenario</p><p>As you probably expect, this library creates those edge cases by making mutations in the source code. After each mutation it runs all the tests. If the mutation is killed everything is good. Otherwise, our tests are not good enough. At the end of the process we can inspect an HTML file with all the tested mutations, which are killed and which survived. This is the good part because we don’t observe a metric (like in coverage) and then try to find how to cover more lines. We see exactly the mutations like below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1CwzUfuqfeca496VOswF1g.png" /><figcaption>Stryker removed the entire definition of the function</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wPYp2-XDmW2km3LIyQ2iXw.png" /><figcaption>Stryker replaced the + with a -</figcaption></figure><p>There are many mutations that can be tested. Stryker will try to replace true with false , += with -= , * with / , % with * , will replace entire expressions in do/while loops with false and many many more that you can find <a href="https://stryker-mutator.io/docs/mutation-testing-elements/supported-mutators/"><em>here</em></a>.</p><h3>How to use it</h3><p>Many test related tools are part of pipelines which run on merge requests or in pre-commit/pre-push scripts, but a mutation testing library in a large scale app will take a long time to complete. You can extract value from it if it runs weekly, so developers can observe the results and enhance the weak tests when they have capacity. Another great benefit of this tool is that it does not mess with the development circle actively. It does not bring bureaucracy in the day-2-day and it does not absorb story points from the tickets. It exists to have a metric on how good your tests are and create a couple of tickets in tech debt.</p><h3>AI increases the need for mutation testing</h3><p>Today, increasingly, developers are turning to AI tools to assist with their daily tasks. Many of us also rely on AI to write tests for certain functions, thereby accelerating the development process. Consequently, new code gets added to the application, and the developer isn’t always the sole proprietor. Mutation testing libraries offer valuable insights into the effectiveness of the tests and can highlight potential weaknesses in the suite.</p><p>And remember… what doesn’t kill you makes you write more unit tests.</p><p>You can find a small example in a Vue app <a href="https://github.com/stavrosdro/vue-stryker"><em>here</em></a>.</p><p><em>Enjoy 🚀🚀🚀</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7560315ca70e" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/introduce-mutation-testing-7560315ca70e">Introduce Mutation testing</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Event handling done right]]></title>
            <link>https://itnext.io/event-handling-done-right-59af8909ff7e?source=rss-7ab07974fa89------2</link>
            <guid isPermaLink="false">https://medium.com/p/59af8909ff7e</guid>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Stavros Droutsas]]></dc:creator>
            <pubDate>Sat, 17 Jun 2023 12:46:03 GMT</pubDate>
            <atom:updated>2023-06-17T13:12:15.404Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/572/1*eS-M8ueuJHnDt3BUdGXsYg.png" /></figure><p>This is a trap that I have fallen into numerous times. In modern frontend frameworks there are a ton of parent and child components. The parent component passes data using props and child components emit events to communicate with their parent. Or not?</p><p>Of course the parent should provide data using props but there are cases where the parent should also provide handler functions instead of reacting to the emitted event. Let’s elaborate.</p><h3>Welcome the test subjects</h3><p>To start the investigation we need a simple child component in two variations. One that emits an event to its parent and one that accepts a handler function as a prop.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3586e4883931daf6cc6c1e89a8f916dc/href">https://medium.com/media/3586e4883931daf6cc6c1e89a8f916dc/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/246d083b66971d591b07a132a3c570ce/href">https://medium.com/media/246d083b66971d591b07a132a3c570ce/href</a></iframe><p>As you can see above, the main difference is in line 13 (both gists). On the first variation the component emits an event, but on the second the component is executing the passed function.</p><h3>Start the comparison</h3><p>The comparison will cover the following aspects</p><ul><li>Child component complexity/verbosity</li><li>Parent component complexity/verbosity</li><li>Unit testing</li><li>App performance</li></ul><h4>Complexity of child components</h4><p>Both components are even and look simple. Complexity and verbosity are kept low and it’s easy to understand with a glimpse. Score for button emit VS button handler is 1–1.</p><h4>Complexity of parent components</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/07ee5ca0a3b735203a40ca8b486a449a/href">https://medium.com/media/07ee5ca0a3b735203a40ca8b486a449a/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/65218a0fede542fe33a8d73363879de9/href">https://medium.com/media/65218a0fede542fe33a8d73363879de9/href</a></iframe><p>Both parent components have the same function. The only difference is noticeable in line 3, where there is the event handler on the first and the prop on the second. Once again the complexity and verbosity are low. The tie remains, score is 2–2.</p><h4>Unit testing</h4><p>The tests of the child components:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/547d73586ce484097e8be00167561fa8/href">https://medium.com/media/547d73586ce484097e8be00167561fa8/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f13b098f7088edc97f1ed98946757b3d/href">https://medium.com/media/f13b098f7088edc97f1ed98946757b3d/href</a></iframe><p>Regarding the above unit tests the emitted event case is a bit simpler. In general there are equivalent but if we want to be precise, we have to address the extra step of the jest function definition. So the score is 3–2 and the emit variation takes the lead.</p><p>The tests of the parent components:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bcb49a149c60fe3319452165438d8c17/href">https://medium.com/media/bcb49a149c60fe3319452165438d8c17/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f5cf162ab3575a56a93954c4eedbf5b/href">https://medium.com/media/1f5cf162ab3575a56a93954c4eedbf5b/href</a></iframe><p>Regarding the above tests, we have to follow the same steps until the mount of the parent components. On the emit case, we have to force the child component to emit an event and with the help of a spy, check if the corresponding function is called. On the handler case we have to populate the stubbed child with a props field and then check if the new prop is the expected. I cannot determine which is simpler. Another tie. Score is 4–3.</p><h4>Performance</h4><p>Before checking the results it’s time to think about the differences of the components. On the first case the child component emits a custom event, its parent listens and executes a function. This type of event binding is probably implemented using the Observer pattern. It does not add an additional event listener, because it’s not a DOM event. On the second case, the child component needs an additional prop. Props have reactivity which is implemented using a Proxy object.</p><p>So, which is more expensive? Let’s add one magic line in both parent components to find out:</p><pre>v-for=&quot;i in 300_000&quot; :key=&quot;i&quot;</pre><p>Then we have to create a production build of the application and host it with NGINX or Live Server. Open an incognito window, without any extensions, open dev tools and check the performance monitor:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x2JCVxNVs-1RD6vJNUVRxw.png" /><figcaption>emit custom event</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AXFW9EO1IzjLlz-b9l7giQ.png" /><figcaption>use function as a prop</figcaption></figure><p>We can see that in both cases we have ~300,000 event listeners and ~1,200,000 DOM nodes. (The results are after the garbage collection). There is a noticeable difference in JS heap size of ~100 MB in favor of the handler variation. After the performance results, the handler variation wins by knock-out.</p><p>Even though I personally prefer emitting custom events, because I like the separation of passing data with props and react to emitting events with functions, this difference is enough for me to stick with the second approach and skip the custom events as much as possible.</p><p>You can find the code <a href="https://github.com/stavrosdro/emit-custom-event-vs-function-as-prop"><em>here</em></a></p><p><em>Enjoy 🚀🚀🚀</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=59af8909ff7e" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/event-handling-done-right-59af8909ff7e">Event handling done right</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create your home development server. VS Code & Χrdp]]></title>
            <link>https://itnext.io/create-your-home-development-server-vs-code-%CF%87rdp-b8272c497807?source=rss-7ab07974fa89------2</link>
            <guid isPermaLink="false">https://medium.com/p/b8272c497807</guid>
            <category><![CDATA[xrdp]]></category>
            <category><![CDATA[servers]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[remote-development]]></category>
            <dc:creator><![CDATA[Stavros Droutsas]]></dc:creator>
            <pubDate>Sun, 28 May 2023 13:52:29 GMT</pubDate>
            <atom:updated>2023-05-28T14:25:14.383Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PQpABO4k0dkSJ-uAPLEYeg.jpeg" /></figure><blockquote>We always like an extra development server, don’t we? If you prefer remote development, coding on a specific Linux distro, or need an alternative to your company’s Windows PC, you can check this approach.</blockquote><p>After 8 easy-to-follow steps you will be able to:</p><ul><li>execute git actions</li><li>use docker flawlessly</li><li>connect with SSH and VS Code</li><li>connect with Xrdp</li><li>test your app with your local tools (postman, browser, etc.) without compromises</li></ul><h4>Step 1: Install a Linux distro of your preference</h4><p>Nothing fancy here. I just downloaded (and verified) Xubuntu, created a boot drive and followed the installation process. I also selected “Normal installation” and “Download updates while installing Xubuntu”. Finally I ran sudo apt-get update and sudo apt-get upgrade</p><h4>Step 2: Install docker engine</h4><p>A couple of commands to download and install docker services</p><pre>sudo apt-get install ca-certificates curl gnupg<br><br>sudo install -m 0755 -d /etc/apt/keyrings<br>curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg<br>sudo chmod a+r /etc/apt/keyrings/docker.gpg<br><br>echo \<br>  &quot;deb [arch=&quot;$(dpkg --print-architecture)&quot; signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \<br>  &quot;$(. /etc/os-release &amp;&amp; echo &quot;$VERSION_CODENAME&quot;)&quot; stable&quot; | \<br>  sudo tee /etc/apt/sources.list.d/docker.list &gt; /dev/null<br><br>sudo apt-get update<br><br>sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin</pre><p>You can test the installation by running sudo docker run hello-world</p><h4>Step 3: Manage docker as a non-root user</h4><pre>sudo groupadd docker<br><br>sudo usermod -aG docker $USER<br><br>newgrp docker</pre><p>Then execute docker run hello-world to verify the system’s behavior</p><h4>Step 4: Install remote desktop protocol</h4><p>This is an optional step but let’s grab the opportunity to add this package. Even if it’s an development server, we may end up doing some extra office work.</p><p>First of all, install the Xrdp package and check if it’s running successfully.</p><pre>sudo apt install xrdp -y<br><br>sudo systemctl status xrdp</pre><p>For security reasons, we have to change the port settings to allow connections only form localhost. If you also want to change the default port this is the time. Open the settings file with sudo nano /etc/xrdp/xrdp.ini and make the changes below.</p><p>The initial settings will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/579/1*HGhSwBO39pa7uEzq20zDLQ.png" /><figcaption>initial xrdp port settings</figcaption></figure><p>Find the line with port=3389 and replace it with port=tcp://.:3389. After the change, the file will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/579/1*2bSpaMydX9at75eN3p789Q.png" /><figcaption>final xrpd port settings</figcaption></figure><p>Restart the xrdp service and verify that the port configuration is correct</p><pre>sudo systemctl restart xrdp<br>sudo systemctl status xrdp</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/579/1*XQn6VE2FThPsIp7_ces6yA.png" /><figcaption>port settings on the running service</figcaption></figure><h4>Step 5: Install SSH server</h4><p>The plan is to connect to the remote machine with SSH, use port forwarding to allow remote desktop connection, be able to test our backend with tools like postman, or open the application’s website with our host browser. That’s why we need the SSH server package.</p><pre>sudo apt-get install openssh-server<br><br>sudo systemctl enable ssh</pre><h4>Step 6: Install Git and add a new SSH key to your GitHub account</h4><p>Install git and create an SSH key. Continue with the default options. You can leave the passphrase empty, otherwise you have to fill it every time you execute git fetch, git push etc.</p><pre>sudo apt install git<br><br>ssh-keygen -t ed25519 -C &quot;your_email@example.com&quot;<br><br>eval &quot;$(ssh-agent -s)&quot;<br><br>ssh-add ~/.ssh/id_ed25519</pre><h4>Step 7: Connect to your server with remote desktop</h4><p>Find the local IP of the server. You can run ip addr</p><p>Open a tool which can establish SSH connection. I use <a href="https://cmder.app/">Cmder</a>, a portable console emulator. PowerShell is also an option. Many users use <a href="https://www.putty.org/">Putty</a>. Then execute this:</p><p>ssh sd@192.168.1.220 -L 5555:localhost:3389 -L 8080:localhost:8080 -L 3000:localhost:3000</p><p>With the above command, I connect with user sd to the remote machine with IP 192.168.1.220. I also forward the port 3389 (needed for xrdp) to my local port 5555. The ports 8080 and 3000 are also forwarded to my local ports to test my application. Replace them with your application ports.</p><p>Now open Remote Desktop Connection, type localhost:5555 and click connect</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/547/1*YlUcAeYL7Db6_KKMbh4nsQ.png" /><figcaption>Remote desktop connection</figcaption></figure><p>Select “Don’t ask me again” and Click “yes”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/1*VspbLQXRY3N1Odl__GokcQ.png" /><figcaption>popup</figcaption></figure><p>Type your credentials (same as the SSH connection)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/357/1*29T1ebwSxU9-10xurm1KYQ.png" /><figcaption>Fill username and password</figcaption></figure><p>Enjoy your remote machine with an interface!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xrrMb0yFPZ5BuHV6CFuL9g.png" /><figcaption>remote connection is ready</figcaption></figure><h4>Step 8: Connect with your VS Code and start coding</h4><p>For this step we need a VS Code installation with the Remote SSH extension</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/307/1*nAkbaL8lAcFsGXzqbIhshw.png" /></figure><p>Hit CTRL+SHIFT+P and follow the steps:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/762/1*DxeuwPPXEo7D4He8s8xfqg.png" /><figcaption>Select connect to Host…</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/762/1*MnWeMlJ0AJybfrFqZoD7rA.png" /><figcaption>Select Add New SSH Host. The next time you will be able to select the saved host and just fill the password</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/1*5qkjdBmu8XDFCtgC4Vwzyg.png" /><figcaption>Provide the SSH command</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/751/1*AMtyY6WTGqZ14Jz9Piz-6Q.png" /><figcaption>Save the configuration file</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3-lGNLMVbOeT2j210SZv6A.png" /><figcaption>Everything is ready</figcaption></figure><h4>Conclusion</h4><p>Keep in mind that you may not need all the above steps. Xrdp is optional and if you are familiar with the terminal you may not need it. Also, remember that you can set some port-forwarding rules from VS code. After the application starts, you may see a notification in the bottom right corner (or using the Ports view on the bottom panel). Click it and configure it properly. You can also configure the router’s IP table to give a selected IP to your server.</p><p>On my next article of this series I will define an easy way to setup a backup process from the server (preferably by skipping folders like node_modules etc.)</p><p>Some useful sources:</p><p><a href="https://www.tecmint.com/xubuntu-installation-guide/">xubuntu installation</a>, <a href="https://docs.docker.com/engine/install/ubuntu/">docker installation</a>, <a href="https://docs.docker.com/engine/install/linux-postinstall/">docker post-install</a>, <a href="https://www.digitalocean.com/community/tutorials/how-to-enable-remote-desktop-protocol-using-xrdp-on-ubuntu-22-04">xrdp installation</a>, <a href="https://www.cyberciti.biz/faq/ubuntu-linux-install-openssh-server/">openssh server</a>, <a href="https://code.visualstudio.com/docs/remote/ssh">vs code — ssh</a></p><p><em>Enjoy 🚀🚀🚀</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b8272c497807" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/create-your-home-development-server-vs-code-%CF%87rdp-b8272c497807">Create your home development server. VS Code &amp; Χrdp</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[From Angular to Vue: A simple transition guide]]></title>
            <link>https://itnext.io/from-angular-to-vue-a-simple-transition-guide-d0a8a952d17f?source=rss-7ab07974fa89------2</link>
            <guid isPermaLink="false">https://medium.com/p/d0a8a952d17f</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[vuejs]]></category>
            <dc:creator><![CDATA[Stavros Droutsas]]></dc:creator>
            <pubDate>Wed, 05 Oct 2022 23:21:39 GMT</pubDate>
            <atom:updated>2022-10-06T11:00:13.012Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*nliLqce-Z1DHmc7DJ6PUVw.jpeg" /></figure><p>I stumbled upon Angular 3 years ago when I started my first job as an Application Development Associate and I have to admit that I totally respect and love to work with this framework. The last seven months after a job change, I had the opportunity to meet Vue, another popular frontend framework, for the first time.</p><p>This is the first part of a guide that will help engineers experienced with Angular to make their first steps in a Vue project. Of course, basic Angular knowledge is required.</p><blockquote>TL;DR Go and check the official Vue <a href="https://vuejs.org/guide/introduction.html">documentation</a>, or continue reading at your own risk</blockquote><h3>10-mile vue</h3><h4>Install CLI, create workspace, start application</h4><pre>// Angular<br>ng new from-angular<br>cd from-angular<br>ng serve</pre><pre>// Vue<br>vue create to-vue<br>cd to-vue<br>npm run serve</pre><p>You can find the applications’ home page <a href="http://localhost:4200/"><em>here</em></a> and <a href="http://localhost:8080/"><em>there</em></a>.</p><h4>Application and Component Structure (Options API)</h4><p>In both applications we can open the src folder and check the files. Both include a main.ts file which is responsible for the application bootstrap. In Angular, the first module is the app.module which includes other modules, router and components and bootstraps the app.component.ts. In Vue the main.ts is responsible to initialize the state, import shared components, routes and create the App.vue component.</p><p>In contrast with Angular, Vue uses single file components (SFC), that hold three blocks the &lt;template&gt;<em> (HTML template)</em>, the &lt;script&gt;<em> (TypeScript code)</em> and the style<em> (CSS).</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/82d8648edff0c773bf1b0817d7d0bd31/href">https://medium.com/media/82d8648edff0c773bf1b0817d7d0bd31/href</a></iframe><p>Taking a closer look, we can see that the script returns an object with a data property, or as Vue calls it data <strong><em>option</em></strong>. This property has reactivity which means that JS tracks changes in this property and updates the DOM appropriately. We can also see the interpolation in the template block with the curly bracket syntax {{greeting}} . Finally in the final block we understand the presence of two attributes, lang and scoped , so we can use the fancier SCSS and make sure that the styles will affect this file template only.</p><p>Let’s elaborate. Alongside with data option, Vue offers computed where we can create properties from other data and update them only when the source is updated (e.g. name() { return this.first + &#39; &#39; this.last } , methods where we can put our functions and watch which can trigger a function when a reactive property changes. Furthermore, 6 lifecycle options are offered beforeCreate, created, beforeMount, mounted, beforeUnmount, unmounted . We can use the option props for passing data from a parent component (equivalent of @Input() ), option emits for declaring what events are emitted from the component (as @Output() ), components for the imported components, provide when we want to provide an object to the nested child components by avoiding prop drilling and inject when we want to access provided data from an ancestor. Three interesting offered options are the beforeRouteEnter, beforeRouteUpdate, beforeRouteLeave the route resolvers/guards of the framework and keep them close to the actual component instead of a different file (like Angular). So the script block is an object with many options, that’s why Vue calls it <strong><em>Options API.</em></strong></p><h4>Composition API</h4><p>Now Vue offers an alternative: the<strong><em> Composition API, </em></strong>which is used with &lt;script setup&gt; and we can write the entire logic there. Options API is more readable due to its structure <em>-you always know what block to check in each case-</em> but you have to scroll up &amp; down to navigate from a method to a reactive property that is used and from there to a computed variable and a watcher. Composition API provides the flexibility to keep close the variables and methods you want with the drawback of some boilerplate code to apply reactivity, computed, etc. in each case.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0aae322e40f0a6af2a52e46d6717c614/href">https://medium.com/media/0aae322e40f0a6af2a52e46d6717c614/href</a></iframe><h4>Router and Lazy Loading</h4><p>Vue router is simple and straight-forward. You can bind each path with a <strong>component</strong> or with a <strong>redirection</strong> to another path. In addition, you can provide <strong>eager</strong> and<strong> lazy</strong> loaded components and a name property. The name property allows the navigation to another route without using the hardcoded path and provides the opportunity to change paths without further concerns.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2feaa0995a7c333683fbf9c019cf7641/href">https://medium.com/media/2feaa0995a7c333683fbf9c019cf7641/href</a></iframe><p>Properties like alias <em>-to render the same component for many URLs-</em> children <em>-for child routes-</em> beforeEnter <em>-a route guard-</em> props <em>-to apply an input value or an object to the selected prop of an component!!!-</em> meta <em>-for other information/configs-</em> etc</p><p>Finally, you can display multiple views in the same path, by utilizing the <strong>Named Views</strong>, and showing many components in different sections of the page under the same path.</p><p>The template with the named router-view elements:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9581e7153c9778bd04e8da31d0b55058/href">https://medium.com/media/9581e7153c9778bd04e8da31d0b55058/href</a></iframe><p>The router config for the named views:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/22bd2cdf9c5c24d2c82e5945f3e8b839/href">https://medium.com/media/22bd2cdf9c5c24d2c82e5945f3e8b839/href</a></iframe><h4>Content Projection</h4><p>Vue supports two ways to project content inside a component: slots and scoped slots . A &lt;slot /&gt; allows a component to render a given template fragment. For multiple templates named slots are supported and it’s also possible to define a fallback (default) content for a slot. The Angular’s equivalent for slot is the &lt;ng-template&gt; .</p><p>A scoped-slot is a slot in steroids, because it allows us to access the state of the child component/template. The Angular’s equivalent is a combination of an &lt;ng-template&gt; to pass the template and &lt;ng-container&gt; to pass the given data to the provided template. A good example of scoped slot is this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a0b70a004ebb49f9118e78b1d6f7688/href">https://medium.com/media/0a0b70a004ebb49f9118e78b1d6f7688/href</a></iframe><p>As you can see there is an items list as a prop . The component iterates over the items and renders the passed template from the slot. It also passes the item of the list as a template variable.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/75bd2833c016bb11500581d7ba1b6d8c/href">https://medium.com/media/75bd2833c016bb11500581d7ba1b6d8c/href</a></iframe><p>This is how we can use the scoped slot. Just place a template with a template variable and use it as a prop in our component of choice.</p><h4>Service? Composable!</h4><p>Vue introduces the composable, a function with stateful logic. Now we are able to reuse logic (data and functions) across components. The documentation uses the following example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/50358329823696d7cfc3ff258cb1f01c/href">https://medium.com/media/50358329823696d7cfc3ff258cb1f01c/href</a></iframe><h4>State Management</h4><p>Vue leaves behind (under maintenance mode) Vuex and welcomes <a href="https://pinia.vuejs.org/"><strong><em>Pinia</em></strong></a> as a recommended Store, maintained by Vue core team. Pinia is <strong>simple</strong>, <strong>minimal</strong> and supports <strong>TypeScript</strong>. A small example here:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b539f7ec9eb6a5b71c2e72740b006e25/href">https://medium.com/media/b539f7ec9eb6a5b71c2e72740b006e25/href</a></iframe><h3>1 App in 2 Frameworks</h3><h4>The example application</h4><p>A good example of a transition is to create an app in Angular and create the exact same in Vue to understand the pros &amp; cons. This application is a master-detail example, where you can select a job from the left section and see the summary on the right. You can apply for the selected job by clicking the “Apply Now!” button. It supports a filter functionality (input area above the job list) and a “Refresh” button to re-try the request in case of error.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TQB1CWEUZSavO34P8zY1jQ.png" /><figcaption>Master — Detail example.</figcaption></figure><p>Let’s save a form’s value to localStorage after each change</p><p><em>With Angular:</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b2e6a7bb0637703a81297929546f4872/href">https://medium.com/media/b2e6a7bb0637703a81297929546f4872/href</a></iframe><p><em>With Vue :</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8746aaf13f9f8d1563f5989a5f8599c0/href">https://medium.com/media/8746aaf13f9f8d1563f5989a5f8599c0/href</a></iframe><p>A form template with an input and error message</p><p><em>With Angular:</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d81f831ddd4dadeef4650608255ebc76/href">https://medium.com/media/d81f831ddd4dadeef4650608255ebc76/href</a></iframe><p><em>With Vue:</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3f298db1fc144303133860190127d693/href">https://medium.com/media/3f298db1fc144303133860190127d693/href</a></iframe><h4>Comparison — Base diffs</h4><ul><li>Vue’s core team created Pinia, a completed Store library. Angular uses Observables <em>-a trickier approach with learning curve-</em> and depends on external libraries for complete Store solutions.</li><li>RxJS operators are a life-saver. switchMap , retry , debounce etc must be imported from external libraries or worse… created by our own ugly dirty hands.</li><li>Angular’s pipes equivalent are computed variables, but built-in pipes should be implemented from scratch (just think about date , currency etc)</li><li>Vue still uses fetch API to fire HTTP requests so developers should implement functionalities for cancelling, retry or use external libraries.</li><li>Forms, or even worse Form arrays with async validators… Nothing to mention here.</li><li>Performance: Vue uses Virtual DOM, Angular doesn’t.</li><li>Watchers: Vue is able to track objects and reacts to their updates with an easy and simple way.</li><li>Vue uses single file components (SFC), so it’s necessary to keep files lean and small.</li><li>Angular needs a large time investment. Vue is minimal and very easy to start.</li></ul><h3>Conclusion</h3><p>I know that I am biased because I started with Angular and I tend to think “the Angular way”, but I have to admit that Vue balances perfectly between the simplicity, the minimalism and the ease-of-use without many compromises. Of course, the absence of Angular’s built-in utilities is noticeable and I have to admit that it’s uncomfortable atm.</p><blockquote>The main dilemma is this: We prefer an all-inclusive framework that needs a high engagement, or a minimal framework where developers have the ability to either build their own tools or rely on the community?</blockquote><p>The application code is <a href="https://github.com/stavrosdro/from-angular-to-vue"><em>here</em></a>. Enjoy 🚀🚀🚀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d0a8a952d17f" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/from-angular-to-vue-a-simple-transition-guide-d0a8a952d17f">From Angular to Vue: A simple transition guide</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular Material — handle many mat-error(s) in the same mat-form-field]]></title>
            <link>https://itnext.io/angular-material-handle-many-mat-error-s-in-the-same-mat-form-field-6234235cf365?source=rss-7ab07974fa89------2</link>
            <guid isPermaLink="false">https://medium.com/p/6234235cf365</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[reactive-forms]]></category>
            <category><![CDATA[forms]]></category>
            <dc:creator><![CDATA[Stavros Droutsas]]></dc:creator>
            <pubDate>Sun, 19 Jun 2022 19:55:51 GMT</pubDate>
            <atom:updated>2022-06-20T06:17:32.836Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/912/1*OA6OAGX70kUKtWdtJffxhQ.png" /></figure><h4>Angular Material — handle many mat-error(s) in the same mat-form-field</h4><p>Angular Material is a collection of high quality, versatile, frictionless components. They can elevate the quality of the application and provide an elegant look &amp; feel. The highly configurable table, the stepper control, the expandable tree, and for sure the form controls can satisfy many business needs. Regarding the forms support let’s check a common use-case.</p><p>Here we have a common password field with 3 validators. Password should not be empty, be at least 6 characters and include lowercase, uppercase and numbers. For each of the above validators we have one specific error message.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZqxK7UeywxFAgjmpoMEyvQ.png" /><figcaption>A typical mat-form-field control with an input field</figcaption></figure><p>The issue with the above configuration is that it’s possible to have 2 error messages at the same time, but mat-form-field control reserves space for only one. In this case it’s possible to mess with the aesthetic of the UI.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/376/1*6VWY2Rl-H63eHXobemyReg.png" /><figcaption>Two messages are shown instead of one</figcaption></figure><p>As a result, we have to find out a way to only show the first error message and re-use this logic in all form components. One way is to create a function that returns only the first error message of the given control and use only one mat-error control. With this approach we keep all error messages in ts file and the html is lean. I am not a supporter of this way-to-go because I prefer to keep the messages in the html file. Moreover I don’t want to use again, again and again the same function in all forms to wrap the error message projection.</p><p>I admire minimal solutions, small fixes that I can use in one place and cover my entire application. For this problem the solution came from the beloved CSS. Just paste the following lines in the styles.scss and consider it done.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%25280%252C0%252C0%252C0%2529%26t%3Dseti%26wt%3Dnone%26l%3Dcss%26width%3D680%26ds%3Dfalse%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dmat-form-field%252520%25257B%25250A%252520%252520%252520%252520mat-error%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520display%25253A%252520none%252520%2521important%25253B%25250A%252520%252520%252520%252520%25257D%25250A%25250A%252520%252520%252520%252520mat-error%25253Afirst-child%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520display%25253A%252520block%252520%2521important%25253B%25250A%252520%252520%252520%252520%25257D%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%2525280%25252C0%25252C0%25252C0%252529%26t%3Dseti%26wt%3Dnone%26l%3Dcss%26width%3D680%26ds%3Dfalse%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dmat-form-field%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520mat-error%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520display%2525253A%25252520none%25252520%252521important%2525253B%2525250A%25252520%25252520%25252520%25252520%2525257D%2525250A%2525250A%25252520%25252520%25252520%25252520mat-error%2525253Afirst-child%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520display%2525253A%25252520block%25252520%252521important%2525253B%2525250A%25252520%25252520%25252520%25252520%2525257D%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/22d624f23e21d9ddc3331733d184ccec/href">https://medium.com/media/22d624f23e21d9ddc3331733d184ccec/href</a></iframe><blockquote>Now all mat-errors remain hidden except the first!</blockquote><p>Of course I know that there are many different approaches but I really believe that the above is the most minimal. I will be grateful to see alternatives in the comments!</p><p>Enjoy 🚀🚀🚀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6234235cf365" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/angular-material-handle-many-mat-error-s-in-the-same-mat-form-field-6234235cf365">Angular Material — handle many mat-error(s) in the same mat-form-field</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular — an interceptor interview question with a tricky aspect]]></title>
            <link>https://itnext.io/angular-an-interceptor-interview-question-with-a-tricky-aspect-5a0d1616c74b?source=rss-7ab07974fa89------2</link>
            <guid isPermaLink="false">https://medium.com/p/5a0d1616c74b</guid>
            <category><![CDATA[frontend-development]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Stavros Droutsas]]></dc:creator>
            <pubDate>Tue, 26 Apr 2022 10:13:34 GMT</pubDate>
            <atom:updated>2022-04-26T10:23:53.588Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/914/1*5S2CV1h1XqKHhPv2BYXZPA.png" /></figure><h3>Angular — an interceptor interview question with a tricky aspect</h3><p>Let’s suppose that we have an Angular application and we have the following requirement:</p><blockquote>The API’s major version is changed and now every HTTP call towards our API should have a specific header e.g., X-APP. We need a fast and reliable way to proceed with the necessary changes as soon as possible. Any ideas?</blockquote><p>The common answer and a good way to go, is to use an interceptor in the app or core module and add the necessary header in the outgoing requests. This implementation is fast and easy to understand. It uses a framework feature and is considered best practice.</p><p><em>The interceptor is an optional dependency of the HttpClient. We must provide it in the same injector that provides the HttpClient. Otherwise, the instance will be created without the interceptor dependency and the interceptor will be ignored.</em></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528255%252C255%252C255%252C1%2529%26t%3Dseti%26wt%3Dnone%26l%3Dapplication%252Ftypescript%26width%3D845.25%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%252520%25257B%252520Injectable%252520%25257D%252520from%252520%2527%252540angular%25252Fcore%2527%25253B%25250Aimport%252520%25257B%25250A%252520%252520%252520HttpInterceptor%25250A%25257D%252520from%252520%2527%252540angular%25252Fcommon%25252Fhttp%2527%25253B%25250A%25250A%252540Injectable%2528%2529%25250Aexport%252520class%252520HttpInterceptor%252520implements%252520HttpInterceptor%252520%25257B%25250A%25250A%252520%252520intercept%2528req%25252C%252520next%2529%252520%25257B%25250A%252520%252520%252520%252520const%252520newReq%252520%25253D%252520req.clone%2528%25257B%25250A%252520%252520%252520%252520%252520%252520headers%25253A%252520req.headers.set%2528%2527X-APP%2527%25252C%252520%2527microservice%2527%2529%25250A%252520%252520%252520%252520%25257D%2529%25250A%252520%252520%252520%252520return%252520next.handle%2528newReq%2529%25253B%25250A%252520%252520%25257D%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528255%25252C255%25252C255%25252C1%252529%26t%3Dseti%26wt%3Dnone%26l%3Dapplication%25252Ftypescript%26width%3D845.25%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%25252520%2525257B%25252520Injectable%25252520%2525257D%25252520from%25252520%252527%25252540angular%2525252Fcore%252527%2525253B%2525250Aimport%25252520%2525257B%2525250A%25252520%25252520%25252520HttpInterceptor%2525250A%2525257D%25252520from%25252520%252527%25252540angular%2525252Fcommon%2525252Fhttp%252527%2525253B%2525250A%2525250A%25252540Injectable%252528%252529%2525250Aexport%25252520class%25252520HttpInterceptor%25252520implements%25252520HttpInterceptor%25252520%2525257B%2525250A%2525250A%25252520%25252520intercept%252528req%2525252C%25252520next%252529%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520const%25252520newReq%25252520%2525253D%25252520req.clone%252528%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520headers%2525253A%25252520req.headers.set%252528%252527X-APP%252527%2525252C%25252520%252527microservice%252527%252529%2525250A%25252520%25252520%25252520%25252520%2525257D%252529%2525250A%25252520%25252520%25252520%25252520return%25252520next.handle%252528newReq%252529%2525253B%2525250A%25252520%25252520%2525257D%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/63dafa679919aed4a9faca29dac196d7/href">https://medium.com/media/63dafa679919aed4a9faca29dac196d7/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528255%252C255%252C255%252C1%2529%26t%3Dseti%26wt%3Dnone%26l%3Dapplication%252Ftypescript%26width%3D680%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dproviders%25253A%252520%25255B%25250A%252520%252520%25257B%252520provide%25253A%252520HTTP_INTERCEPTORS%25252C%252520useClass%25253A%252520HttpInterceptor%25252C%252520multi%25253A%252520true%252520%25257D%25250A%25255D%25252C&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528255%25252C255%25252C255%25252C1%252529%26t%3Dseti%26wt%3Dnone%26l%3Dapplication%25252Ftypescript%26width%3D680%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dproviders%2525253A%25252520%2525255B%2525250A%25252520%25252520%2525257B%25252520provide%2525253A%25252520HTTP_INTERCEPTORS%2525252C%25252520useClass%2525253A%25252520HttpInterceptor%2525252C%25252520multi%2525253A%25252520true%25252520%2525257D%2525250A%2525255D%2525252C&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/8e0966531e0863156ec962c66e114673/href">https://medium.com/media/8e0966531e0863156ec962c66e114673/href</a></iframe><p>Here comes the follow-up question:</p><blockquote>That’s a good approach and indeed it’s fast. Ok, let’s suppose you add the interceptor in the app module, is everything ok now? Is it possible to have an edge case where the solution suffers?</blockquote><p>Interceptor should be placed in the same module with HttpClientModule <em>(app or core module in most cases)</em>. <strong><em>Hence,</em></strong> <strong><em>we have to make sure that our entire application uses this exact instance of HttpClientModule that is created with our interceptor.</em></strong></p><p>However, if there is another provider of the HttpClient (e.g., a lazy loaded module) we will face the aforementioned issue (absence of the interceptor), so we should add the interceptor in the providers: [] of all modules that provide the HttpClient.</p><p>Now we have a fast and reliable solution.</p><p>Enjoy 🚀🚀🚀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5a0d1616c74b" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/angular-an-interceptor-interview-question-with-a-tricky-aspect-5a0d1616c74b">Angular — an interceptor interview question with a tricky aspect</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular with Eslint, Prettier, husky]]></title>
            <link>https://itnext.io/angular-with-eslint-prettier-husky-6581ecd66fbb?source=rss-7ab07974fa89------2</link>
            <guid isPermaLink="false">https://medium.com/p/6581ecd66fbb</guid>
            <category><![CDATA[prettier]]></category>
            <category><![CDATA[husky]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[hooks]]></category>
            <category><![CDATA[eslint]]></category>
            <dc:creator><![CDATA[Stavros Droutsas]]></dc:creator>
            <pubDate>Sun, 27 Feb 2022 08:02:22 GMT</pubDate>
            <atom:updated>2022-08-04T20:40:24.987Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article, we will create a new Angular project and we will configure <strong>eslint, prettier and husky</strong>. Linters and git hooks are necessary for both personal and company projects. I will use a fresh VSCode installation without a ton of extensions (only prettier is actually needed).</p><p>Most of the users have a local installation of Node.js in a Windows machine, so I will follow the exact same approach, but as a Docker enthusiast, I will provide the equivalent commands. If you use a <a href="https://medium.com/@stavrosdro/docker-development-environment-angular-full-guide-a38ee34fb651">docker development environment</a>, as I do, you will have the same results.</p><p>After the following steps we will be ready to:</p><ul><li>Auto-format the code after every file-save action.</li><li>Identify errors with code linter.</li><li>Execute linter and tests before git commit or git push.</li><li>Enjoy a consistent codebase and reduce diffs &amp; merging conflicts.</li></ul><blockquote>The purpose of this article is to make it easy for the team to clone a project and start working on it, without spending time to configure their local settings. The only required extension is Prettier.</blockquote><h4>Create a new project &amp; install dependencies</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2525280%25252C0%25252C0%25252C0%252529%26t%3Dseti%26wt%3Dnone%26l%3Dapplication%252Fx-sh%26width%3D680%26ds%3Dfalse%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dng%252520new%252520my-app%25250Acd%252520my-app%25250Anpm%252520install%25250Atouch%252520.prettierrc%25250Ang%252520add%252520%252540angular-eslint%25252Fschematics%25250Anpm%252520install%252520eslint-plugin-prettier%252540latest%252520--save-dev%25250Anpm%252520install%252520--save-dev%252520eslint-config-prettier%25250Atouch%252520.eslintignore&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252525280%2525252C0%2525252C0%2525252C0%25252529%26t%3Dseti%26wt%3Dnone%26l%3Dapplication%25252Fx-sh%26width%3D680%26ds%3Dfalse%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%25252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dng%25252520new%25252520my-app%2525250Acd%25252520my-app%2525250Anpm%25252520install%2525250Atouch%25252520.prettierrc%2525250Ang%25252520add%25252520%25252540angular-eslint%2525252Fschematics%2525250Anpm%25252520install%25252520eslint-plugin-prettier%25252540latest%25252520--save-dev%2525250Anpm%25252520install%25252520--save-dev%25252520eslint-config-prettier%2525250Atouch%25252520.eslintignore&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/894dd14f6b34119f17523dfe97ee82c4/href">https://medium.com/media/894dd14f6b34119f17523dfe97ee82c4/href</a></iframe><p>Configuration for the .editorconfig</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528182%252C117%252C117%252C0%2529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26width%3D671.75%26ds%3Dfalse%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Droot%252520%25253D%252520true%25250A%25250A%25255B%252A%25255D%25250Acharset%252520%25253D%252520utf-8%25250Aindent_style%252520%25253D%252520space%25250Aindent_size%252520%25253D%2525204%252520%252523%252520this%252520is%252520the%252520only%252520change%252520I%252520made%25252C%252520to%252520be%252520aligned%252520with%252520prettier%25250Ainsert_final_newline%252520%25253D%252520true%25250Atrim_trailing_whitespace%252520%25253D%252520true%25250A%25250A%25255B%252A.ts%25255D%25250Aquote_type%252520%25253D%252520single%25250A%25250A%25255B%252A.md%25255D%25250Amax_line_length%252520%25253D%252520off%25250Atrim_trailing_whitespace%252520%25253D%252520false&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528182%25252C117%25252C117%25252C0%252529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26width%3D671.75%26ds%3Dfalse%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Droot%25252520%2525253D%25252520true%2525250A%2525250A%2525255B%25252A%2525255D%2525250Acharset%25252520%2525253D%25252520utf-8%2525250Aindent_style%25252520%2525253D%25252520space%2525250Aindent_size%25252520%2525253D%252525204%25252520%25252523%25252520this%25252520is%25252520the%25252520only%25252520change%25252520I%25252520made%2525252C%25252520to%25252520be%25252520aligned%25252520with%25252520prettier%2525250Ainsert_final_newline%25252520%2525253D%25252520true%2525250Atrim_trailing_whitespace%25252520%2525253D%25252520true%2525250A%2525250A%2525255B%25252A.ts%2525255D%2525250Aquote_type%25252520%2525253D%25252520single%2525250A%2525250A%2525255B%25252A.md%2525255D%2525250Amax_line_length%25252520%2525253D%25252520off%2525250Atrim_trailing_whitespace%25252520%2525253D%25252520false&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/42b06f86f3d7af009aa38e876adce274/href">https://medium.com/media/42b06f86f3d7af009aa38e876adce274/href</a></iframe><p>Configuration for the .prettierrc</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528182%252C117%252C117%252C0%2529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26width%3D671.75%26ds%3Dfalse%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%25257B%25250A%252520%252520%252520%252520%252522trailingComma%252522%25253A%252520%252522es5%252522%25252C%25250A%252520%252520%252520%252520%252522printWidth%252522%25253A%25252080%25252C%25250A%252520%252520%252520%252520%252522singleQuote%252522%25253A%252520true%25252C%25250A%252520%252520%252520%252520%252522useTabs%252522%25253A%252520false%25252C%25250A%252520%252520%252520%252520%252522tabWidth%252522%25253A%2525204%25252C%25250A%252520%252520%252520%252520%252522semi%252522%25253A%252520true%25252C%25250A%252520%252520%252520%252520%252522bracketSpacing%252522%25253A%252520true%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528182%25252C117%25252C117%25252C0%252529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26width%3D671.75%26ds%3Dfalse%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%2525257B%2525250A%25252520%25252520%25252520%25252520%25252522trailingComma%25252522%2525253A%25252520%25252522es5%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522printWidth%25252522%2525253A%2525252080%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522singleQuote%25252522%2525253A%25252520true%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522useTabs%25252522%2525253A%25252520false%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522tabWidth%25252522%2525253A%252525204%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522semi%25252522%2525253A%25252520true%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522bracketSpacing%25252522%2525253A%25252520true%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/d338d98619bd2334a4880212c50dbb8f/href">https://medium.com/media/d338d98619bd2334a4880212c50dbb8f/href</a></iframe><p>Configuration for the .eslintignore</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528182%252C117%252C117%252C0%2529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26width%3D671.75%26ds%3Dfalse%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dpackage.json%25250Apackage-lock.json%25250Adist%25250Ae2e%25252F%252A%252A%25250Akarma.conf.js%25250Acommitlint.config.js&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528182%25252C117%25252C117%25252C0%252529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26width%3D671.75%26ds%3Dfalse%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dpackage.json%2525250Apackage-lock.json%2525250Adist%2525250Ae2e%2525252F%25252A%25252A%2525250Akarma.conf.js%2525250Acommitlint.config.js&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/ecf0bb88bf3c3a6722602e788ce3ec38/href">https://medium.com/media/ecf0bb88bf3c3a6722602e788ce3ec38/href</a></iframe><h4>Configure linter, package.json and workspace settings</h4><p>Now we have to configure the eslint to use prettier as a formatter and update the VSCode local settings.</p><p>Update the scripts in the package.json</p><pre>&quot;test&quot;: &quot;ng test --watch=false --browsers ChromeHeadless&quot;,<br>&quot;lint&quot;: &quot;npx eslint \&quot;src/**/*.{js,jsx,ts,tsx,html}\&quot; --quiet --fix&quot;,<br>&quot;format&quot;: &quot;npx prettier \&quot;src/**/*.{js,jsx,ts,tsx,html,css,scss}\&quot; --write&quot;</pre><p>Use this eslint configuration. The difference with the default is that we add this line &quot;plugin:prettier/recommended&quot; for typescript and html files. Also, we created a more specific rule regarding the html components where we want to apply the linter’s rules.</p><p>Configuration of the eslintrc.json</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2LaVpx9_wcPIiYyLaSOhgA.png" /><figcaption>Copy only line 20 and 39</figcaption></figure><p>Type CTRL+SHIFT+P, search for workspace settings (JSON) and use the following settings</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528182%252C117%252C117%252C0%2529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26width%3D614%26ds%3Dfalse%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%25257B%25250A%252520%252520%252520%252520%252522editor.codeActionsOnSave%252522%25253A%252520%25257B%252520%252522source.fixAll.eslint%252522%25253A%252520true%252520%25257D%25252C%25250A%252520%252520%252520%252520%252522editor.bracketPairColorization.enabled%252522%25253A%252520true%25252C%25250A%252520%252520%252520%252520%252522%25255Bhtml%25255D%252522%25253A%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252522editor.defaultFormatter%252522%25253A%252520%252522esbenp.prettier-vscode%252522%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252522editor.formatOnSave%252522%25253A%252520true%25250A%252520%252520%252520%252520%25257D%25252C%25250A%252520%252520%252520%252520%252522%25255Bscss%25255D%252522%25253A%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252522editor.defaultFormatter%252522%25253A%252520%252522esbenp.prettier-vscode%252522%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252522editor.formatOnSave%252522%25253A%252520true%25250A%252520%252520%252520%252520%25257D%25252C%25250A%252520%252520%252520%252520%252522%25255Btypescript%25255D%252522%25253A%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252522editor.defaultFormatter%252522%25253A%252520%252522esbenp.prettier-vscode%252522%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252522editor.formatOnSave%252522%25253A%252520true%25250A%252520%252520%252520%252520%25257D%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528182%25252C117%25252C117%25252C0%252529%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26width%3D614%26ds%3Dfalse%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%2525257B%2525250A%25252520%25252520%25252520%25252520%25252522editor.codeActionsOnSave%25252522%2525253A%25252520%2525257B%25252520%25252522source.fixAll.eslint%25252522%2525253A%25252520true%25252520%2525257D%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522editor.bracketPairColorization.enabled%25252522%2525253A%25252520true%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522%2525255Bhtml%2525255D%25252522%2525253A%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252522editor.defaultFormatter%25252522%2525253A%25252520%25252522esbenp.prettier-vscode%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252522editor.formatOnSave%25252522%2525253A%25252520true%2525250A%25252520%25252520%25252520%25252520%2525257D%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522%2525255Bscss%2525255D%25252522%2525253A%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252522editor.defaultFormatter%25252522%2525253A%25252520%25252522esbenp.prettier-vscode%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252522editor.formatOnSave%25252522%2525253A%25252520true%2525250A%25252520%25252520%25252520%25252520%2525257D%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522%2525255Btypescript%2525255D%25252522%2525253A%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252522editor.defaultFormatter%25252522%2525253A%25252520%25252522esbenp.prettier-vscode%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252522editor.formatOnSave%25252522%2525253A%25252520true%2525250A%25252520%25252520%25252520%25252520%2525257D%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/48d85a4775d85f93d2a43f5604fbd2bf/href">https://medium.com/media/48d85a4775d85f93d2a43f5604fbd2bf/href</a></iframe><p>Now we can run npm run format to format your entire workspace. From now on every time you save a file it will be auto formatted. <br>Also, we can run npm run lint to check our code for linting errors.</p><h4><strong>Install Husky &amp; add pre-push git hook</strong></h4><p><a href="https://typicode.github.io/husky/#/">Husky</a> is a great tool for using Git hooks. I recommend following the automatic installation and run npx husky-init &amp;&amp; npm install. This will also create a pre-commit hook. You can create a pre-push hook by running npx husky add .husky/pre-push &quot;npm test&quot;</p><p>My pre-commit file</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528182%252C117%252C117%252C0%2529%26t%3Dseti%26wt%3Dnone%26l%3Dapplication%252Fx-sh%26width%3D614%26ds%3Dfalse%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%252523%2521%25252Fbin%25252Fsh%25250A.%252520%252522%252524%2528dirname%252520%252522%2525240%252522%2529%25252F_%25252Fhusky.sh%252522%25250A%25250Anpm%252520run%252520lint%25250Anpm%252520test&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528182%25252C117%25252C117%25252C0%252529%26t%3Dseti%26wt%3Dnone%26l%3Dapplication%25252Fx-sh%26width%3D614%26ds%3Dfalse%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%25252523%252521%2525252Fbin%2525252Fsh%2525250A.%25252520%25252522%25252524%252528dirname%25252520%25252522%252525240%25252522%252529%2525252F_%2525252Fhusky.sh%25252522%2525250A%2525250Anpm%25252520run%25252520lint%2525250Anpm%25252520test&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/c25bb5f97aed07c295388ce9ed913f2f/href">https://medium.com/media/c25bb5f97aed07c295388ce9ed913f2f/href</a></iframe><p>My pre-push file</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528182%252C117%252C117%252C0%2529%26t%3Dseti%26wt%3Dnone%26l%3Dapplication%252Fx-sh%26width%3D614%26ds%3Dfalse%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%252523%2521%25252Fbin%25252Fsh%25250A.%252520%252522%252524%2528dirname%252520%252522%2525240%252522%2529%25252F_%25252Fhusky.sh%252522%25250A%25250Anpm%252520test&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528182%25252C117%25252C117%25252C0%252529%26t%3Dseti%26wt%3Dnone%26l%3Dapplication%25252Fx-sh%26width%3D614%26ds%3Dfalse%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%25252523%252521%2525252Fbin%2525252Fsh%2525250A.%25252520%25252522%25252524%252528dirname%25252520%25252522%252525240%25252522%252529%2525252F_%2525252Fhusky.sh%25252522%2525250A%2525250Anpm%25252520test&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/a67bdfc1a9707145cdcfcd936f9d5baf/href">https://medium.com/media/a67bdfc1a9707145cdcfcd936f9d5baf/href</a></iframe><h4><strong>Bonus</strong></h4><ul><li>You can use &quot;editor.codeActionsOnSave&quot;: { &quot;source.organizeImports&quot;: true } rule in workspace settings to sort imports and remove the unused ones. It’s a great rule but remember that there are critical files (e.g. test.ts) which must have a specific order. If you are aware of these files the above rule will be a time-saver.</li><li>You can create more complex rules regarding the imports, such-as keep the external imports on top and separate them with the internals with an empty line. These rules should be defined in the eslintrc.json but you need an extension to auto apply them.</li><li>If you create more complex rules which are based on certain extensions you can use extensions.json file to notify the team about the recommended extensions. Make sure to check if the extensions you need become deprecated and update your codebase appropriately.</li><li>To save a file without formatting type CTRL+SHIFT+P and search for <em>save file without formatting</em>.</li><li>You can bypass the git hooks by using the --no-verifyoption.</li></ul><blockquote>This is it! Now we have a minimal setup with huge potential!<br>Find the code <a href="https://github.com/stavrosdro/angular-demo-app">here</a>.<br>Enjoy 🚀🚀🚀</blockquote><h4><strong>Walkthrough for Docker Enthusiasts</strong></h4><p>For the Docker fellowship I started with this Dockerfile</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%25280%252C0%252C0%252C0%2529%26t%3Dseti%26wt%3Dnone%26l%3Ddockerfile%26width%3D680%26ds%3Dfalse%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3DFROM%252520node%25253A14-alpine%25250A%25250ARUN%252520npm%252520install%252520-g%252520%252540angular%25252Fcli%25254013%25250A%25250ARUN%252520apk%252520add%252520--no-cache%252520git%252520%252523%252520we%252520need%252520git%252520to%252520install%252520husky%25250A%25250ARUN%252520apk%252520add%252520chromium%252520%252523%252520we%252520need%252520a%252520browser%252520to%252520run%252520the%252520tests%25250A%25250AUSER%252520node%25250A%25250AWORKDIR%252520%25252Fapp%25250A%25250AEXPOSE%2525204200%25252049153%25250A%25250ACMD%252520tail%252520-f%252520%25252Fdev%25252Fnull&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%2525280%25252C0%25252C0%25252C0%252529%26t%3Dseti%26wt%3Dnone%26l%3Ddockerfile%26width%3D680%26ds%3Dfalse%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3DFROM%25252520node%2525253A14-alpine%2525250A%2525250ARUN%25252520npm%25252520install%25252520-g%25252520%25252540angular%2525252Fcli%2525254013%2525250A%2525250ARUN%25252520apk%25252520add%25252520--no-cache%25252520git%25252520%25252523%25252520we%25252520need%25252520git%25252520to%25252520install%25252520husky%2525250A%2525250ARUN%25252520apk%25252520add%25252520chromium%25252520%25252523%25252520we%25252520need%25252520a%25252520browser%25252520to%25252520run%25252520the%25252520tests%2525250A%2525250AUSER%25252520node%2525250A%2525250AWORKDIR%25252520%2525252Fapp%2525250A%2525250AEXPOSE%252525204200%2525252049153%2525250A%2525250ACMD%25252520tail%25252520-f%25252520%2525252Fdev%2525252Fnull&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/5caf00d8529070b57c20b9b22c028964/href">https://medium.com/media/5caf00d8529070b57c20b9b22c028964/href</a></iframe><p>Build the docker image with docker build -t ng-image . and create your container with docker run -it -v $(pwd):/app -p 4200:4200 -p 49153:49153 --name ng-container ng-image sh</p><p>You have to follow the above steps in general. I will spot the differences below:</p><p>Create application: ng new angular-demo-app --skip-git</p><p>Update the scripts in the package.json</p><pre>&quot;start&quot;: &quot;ng serve --host 0.0.0.0 --poll&quot;,<br>&quot;test&quot;: &quot;ng test --watch=false&quot;,<br>&quot;lint&quot;: &quot;npx eslint &#39;src/**/*.{js,jsx,ts,tsx,html}&#39; --quiet --fix&quot;,<br>&quot;format&quot;: &quot;npx prettier &#39;src/**/*.{js,jsx,ts,tsx,html,css,scss}&#39; --write&quot;</pre><p>In order to run the tests you have to make a couple of changes into the karma.conf.js file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jFBMoTMlb2pYp1-mDfGiug.png" /><figcaption>Copy only line 1, 2 and lines 36–42</figcaption></figure><p>Now you have the exact same configuration. The last change is to modify the hooks to use the utility container to execute the hooks.</p><p>Open the pre-commit and pre-push files.</p><p>Replace: npm run lint with docker exec -i ng-container sh -c &quot;cd angular-demo-app &amp;&amp; npm run lint&quot;</p><p>Replace: npm test with docker exec -i ng-container sh -c &quot;cd angular-demo-app &amp;&amp; npm test&quot;</p><p>Now everything is ready! You can run the tests locally with docker exec -i ng-container sh -c &quot;cd angular-demo-app &amp;&amp; npm test&quot;. Also with the above modifications in husky hooks you are able to commit and push code as before.</p><blockquote>Find the code <a href="https://github.com/stavrosdro/angular-demo-app">here</a>.<br>Enjoy 🚀🚀🚀</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6581ecd66fbb" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/angular-with-eslint-prettier-husky-6581ecd66fbb">Angular with Eslint, Prettier, husky</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RxJS Operators: Angular — KISS]]></title>
            <link>https://itnext.io/rxjs-operators-angular-kiss-36d875d2ff7d?source=rss-7ab07974fa89------2</link>
            <guid isPermaLink="false">https://medium.com/p/36d875d2ff7d</guid>
            <category><![CDATA[operators]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[rxjs]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Stavros Droutsas]]></dc:creator>
            <pubDate>Sun, 05 Dec 2021 11:41:18 GMT</pubDate>
            <atom:updated>2022-03-14T11:51:50.862Z</atom:updated>
            <content:encoded><![CDATA[<h3>RxJS Operators: Angular — KISS</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZstqIxM_ZDBMucXUsp0yvw.jpeg" /></figure><p>This article’s purpose is to provide the basic information to a beginner to use the most common RxJS operators and hopefully be the spark of embracing reactive programming in the future. The article’s target is to provide not only real-world use cases for switchMap, mergeMap, exhaustMap, concatMap, but also a small project as a starting point.</p><p>During my journey on these operators, I hated the examples that look simple and easy but not reveal the operator’s true purpose and potential. I will try my best to provide use-cases that are understandable, reproduceable in your applications and help you get the basics right.</p><h4>Project Info &amp; Structure</h4><p>Today’s project is an Admin Tool that manages employees’ access into a company’s shared folder. Let’s welcome an all-time-classic paginated table with a small checkbox on the right so we are able to set the employee’s status ‘Active’ or ‘Inactive’. We are also able to export a file with the existing employees and import a file with new employees.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mE3U_X38A3AMSYXPHN89nQ.png" /><figcaption>Our fancy UI</figcaption></figure><p>We have a smart component that hosts the table of the company’s employees. Specific users’ actions trigger events from the smart component and fire some HTTP calls towards an API (in-memory-web-api in our specific case). Of course, anyone can invoke an API when the user is selecting another page or clicks a button and populate the view with the response but now it’s time to face some challenges.</p><h4>Challenge #1: User clicks paginator rapidly</h4><p>So, we have a user that clicks the next-page button so fast that a new request starts before the completion of the previous one. For sure we are able to disable the button until the first response arrives but this approach destroys the user experience and we also force the user to wait until the data arrival and then click the button again. Instead, we should be able to silently <strong>cancel </strong>the pending request and start a new one without interrupting the user.</p><blockquote>The component emits an event when user clicks the paginator area and holds a subscription into the users$ observable.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/418/1*L9MBpqqe0B0Mdmlip8P6Wg.png" /></figure><blockquote>The service transforms the subject’s event into an HTTP call. So, the component will be notified when the data arrive due to its subscription to the users$ observable. The switchMap operator is used to unsubscribe from the current subscription (and as a result cancel the pending HTTP request if there are no other subscribers) and subscribe only to the new observable.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/468/1*6qLFQtJHaGUFxBCgL53Eww.png" /></figure><p>This makes absolute sense. When users change pages rapidly, they really don’t care for the results of the previous pages but only for the final page where they will land. That’s a perfect analogy with the switchMap operator that unsubscribes from the previous inner observable and subscribes to the latest.</p><h4><strong>Challenge #2: User clicks checkboxes </strong><em>rapidly</em></h4><p>In this scenario user wants to click several checkboxes very fast. Once again, we are able to use a loader in the center of the screen, wait until the checkbox’s HTTP request is completed, then fire another request to refresh the page’s employees and only after the updated data have arrived, hide the loader and allow user to continue. Let’s focus on the user. User wants to click a couple of checkboxes to change employees’ statuses and wait for the updated data without any interruption. So, we are in a situation that several events will be emitted, we will manage them in <strong>parallel</strong> and update the data after.</p><blockquote>The component emits an event when user clicks the checkbox and holds a subscription into the user$ observable. When an employee is updated successfully a new request is fired to update the data.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/544/1*J4u7qdmYwthFFA9__hpAHg.png" /></figure><p>The service follows the same pattern as before but the mergeMap operator is used this time. As a result, the component maintains the subscription in all active requests and it will be notified when any of them is completed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/451/1*UCAiDENkjNRbleI0xO9FvQ.png" /></figure><p>With this approach users are able to click many checkboxes very fast and the application is able to process the requests in parallel. Of course, sometimes firing multiple requests towards an API is not the optimal solution. Update a boolean value of an existing employee record is probably fine, but what will happen if we try to create many new users?</p><h4>Challenge #3: User imports a JSON file with new employees</h4><p>For this use case, let’s assume that the API can create only one employee each time, but we want to allow the user to provide a file with a list of employees for better experience. The application will read the file and fire an HTTP request for each employee in the list, but if we fire the requests in parallel, we will generate unnecessary load to the backend. The proper way to go is to maintain one active request each time and <strong>start the next one only after the previous is completed</strong>. When all new employees are created, we should refresh the data.</p><blockquote>The component will parse the JSON and create a list of employees. We use the take operator to subscribe for a number of requests equal to the number of the employees we want to create. After that we will refresh the data.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/536/1*mWAofXGwvY95XlEBpw-s9g.png" /></figure><p>The service will create a new observable for each new employee record by using the `from` operator. The concatMap operator will keep a queue with the observables and subscribe in the new one <strong>after the completion</strong> of the previous one, so now we are able to process them in sequence.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/679/1*DO-4HJUdi8AuUm4xVw16AQ.png" /></figure><h4><strong>Challenge #4: User clicks export CSV button </strong><em>rapidly</em></h4><p>Now the user tries to download a file with all the available employees. For sure this action will take some time and is resource-hungry for the backend. We should protect the API, so we have to <strong>ignore</strong> further events until the active HTTP request is completed.</p><blockquote>The component subscribes to allUsers$ observable and emits one event for each button click.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/426/1*HfjLfwJTVY1okGCHMTx4cg.png" /></figure><blockquote>The service will create a new observable when a CSV file is requested but will ignore all other events if there is one that is already active. So, all these massive clicks will have zero impact. The filter operator is used to skip events if another file type (e.g. json) is requested.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/502/1*89NK762wpAPWaLJhntJRZQ.png" /></figure><h4><strong>Bonus</strong></h4><ul><li>Use the concatMap operator when users want to upload multiple files at once, but the API accepts only one file per request and you know that file process is resource hungry. You can use a correlation id in the requests and also include the index of the file as long as the total number of the files.</li><li>Use asyncPipe as much as possible so angular has to unsubscribe from the observables. For the cases that we have to manually subscribe we should make sure that we unsubscribe to avoid memory leaks. An easy way to do it, is by utilizing the takeUntil operator combined with a subject that emits a value when the component is destroyed.</li></ul><blockquote>Find the Angular code <a href="https://github.com/stavrosdro/rxjs-operators"><em>here</em></a><em>.</em></blockquote><p><em>Enjoy </em>🚀🚀🚀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=36d875d2ff7d" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/rxjs-operators-angular-kiss-36d875d2ff7d">RxJS Operators: Angular — KISS</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>