<?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 Kaleb Dalla on Medium]]></title>
        <description><![CDATA[Stories by Kaleb Dalla on Medium]]></description>
        <link>https://medium.com/@kaleb-dalla?source=rss-bfd3b01818df------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*1aArCvcnSZ7I3ObT35EnEA.jpeg</url>
            <title>Stories by Kaleb Dalla on Medium</title>
            <link>https://medium.com/@kaleb-dalla?source=rss-bfd3b01818df------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 12:11:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kaleb-dalla/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[How to test a service that uses Signals + Observables with a declarative approach?]]></title>
            <link>https://kaleb-dalla.medium.com/how-to-test-a-service-that-uses-signals-observables-with-a-declarative-approach-f407c6f993ae?source=rss-bfd3b01818df------2</link>
            <guid isPermaLink="false">https://medium.com/p/f407c6f993ae</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[unittest]]></category>
            <category><![CDATA[jest]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Kaleb Dalla]]></dc:creator>
            <pubDate>Thu, 06 Feb 2025 11:25:35 GMT</pubDate>
            <atom:updated>2025-02-06T11:25:35.069Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Wg875iKXAJ0ac69auFvcOg.png" /><figcaption>jest logo</figcaption></figure><p>Heey Heey Folks! How are you doing? Recently I have been studying Signals and how to use them with Observables. There is a great video from <a href="https://medium.com/u/410e2ec02d13">Deborah Kurata</a> addressing this topic, where you can watch it <a href="https://www.youtube.com/watch?v=5SD995zKvbk&amp;t=1s">here</a>.</p><p>I have a project called RxF1 which I developed quite some time ago to study some reactive patterns with RxJS. These patterns use a declarative approach instead of the imperative approach, which is the most common one. I explained these patterns in details in my other article, which you can check <a href="https://kaleb-dalla.medium.com/rxjs-two-data-retrieve-patterns-for-you-to-use-a30c732ccd33">here</a>.</p><p>Some time ago, I refactored my services on the RxF1 project to use Signals and I struggled a lit bit to test these guys. It was difficult to find any answers on StackOverFlow, so I decided to write this article as documentation and I hope it may help someone in the future. Let’s start?</p><p>Before we move on, just a side note: <em>For the sake of simplicity, I will just add the skeleton of my files to make them smaller and easier to read. I will provide the link to the project’s repository at the end of the article </em>:D</p><p>The first example that I want to talk about is from a class called DriversService. This service is responsible for getting the data used on the drivers page where I show the list of drivers driving for each constructor per season.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NZYTWYTgDhxqzTPUEubH5A.png" /><figcaption>a screenshot of the drivers page showing the drivers grid for the 2021 season</figcaption></figure><p>Let’s have a look on the code of the DriversService.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e90c789d78d7a443db4fee04d8f07c38/href">https://medium.com/media/e90c789d78d7a443db4fee04d8f07c38/href</a></iframe><p>On line 1, I declared a property called seasonSelectedwhich is a Signal with an initial value of ‘2021&#39;. On line 3, I have a method responsible for setting a new value to the seasonSelectedSignal and everytime that its value change, I want to trigger an HTTP request to update my list of drivers based on this new season information. This is accomplished on line 7, where I declared a private observable called driversList$that will hold the value return from the HTTP call.</p><p>I’m also using the function <em>toObservable() </em>provided by the <strong>@angular/core/rxjs-interop</strong> <strong>package </strong>to convert my Signal to an observable. Then I’m using the pipe function to apply a switchMap operator which automatically subscribes to the outer seasonSelectedobservable to get the value of the season and call the <em>getDrivers() </em>method responsible for the HTTP call.</p><p>Finally, on line 12 I expose a Signal property called driversListusing the function <em>toSignal() </em>that will convert the driversList$ observable to a Signal. The function <em>toSignal() </em>is also available through the <strong>@angular/core/rxjs-interop package</strong>.</p><p>Now, how can we test that?</p><p>The test itself is quite simple to write. You can find the code bellow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ab8a4817d93e84a53ac2f7fea91cd984/href">https://medium.com/media/ab8a4817d93e84a53ac2f7fea91cd984/href</a></iframe><p>First I’m going to mock a response object from the API. Then on line 8, I’m declaring the object that I expect to receive from my service.</p><p>On line 12, I’m creating a Signal referencing the Signal from my service. On line 13, I’m mocking the function <em>get() </em>from the HttpClient to return the mocked object of line 8.</p><p>On line 15 I’m calling the service method <em>onSeasonSelected()</em> that will start the chain of changes until the HTTP request.</p><p>On line 16, I’m using the <em>flushEffects()</em> function to manually trigger the signal to be read.</p><p>Finally, on line 18 I compare the content of the Signal with my mocked driversResponse, and that’s it!</p><blockquote>The most important part of this test is the function <strong>flushEffects() </strong>that will make the signal be read.</blockquote><p>Now, I want to talk about a second example that is a little bit more complicated. For this example, I’m going to use a class called RacesService . In this service, I’m combining two HTTP requests to show a modal with information about a specific race including the final result and the championship standings until that date, as you can see bellow.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VDZ4zsCBKZ8VDeOlJKuiiA.png" /><figcaption>rxf1 modal example displaying information about the race final grid and championship standings</figcaption></figure><p>Let’s see the code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7343d38c29152d61bac48e2a64396611/href">https://medium.com/media/7343d38c29152d61bac48e2a64396611/href</a></iframe><p>Again, I have a similar structure to the driversService class. The only difference now is that I have one more information that I will need in order to make the request, which is the round. For that, I have created a subject called roundSelectedSubject and expose it as an observable on line 4.</p><p>Now, to trigger the HTTP requests we need that the roundSelected() and selectedSeasonChanged() functions to be called. The roundSelected() function will start the HTTP request chain, using the withLatestFrom operator that emits the value of the season signal. Then, on line 50 I’m exposing another observable calledfinalResults$ which combine the response from both HTTP requests using the RxJS operator called <strong>zip </strong>that emits values as an array after all observables emit.</p><p>So, how to test it?</p><p>As I mentioned before, this test is a bit more challenging because I have two parallel requests being made, both using the withLatestFrom operator. Firsts, let’s have a look on the code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1997169779097259545182a4aef2f6d2/href">https://medium.com/media/1997169779097259545182a4aef2f6d2/href</a></iframe><p>Again, I started the test mocking my data. I mocked the responses from both HTTP requests. On line 14, I’m subscribing to the finalResults$ observable in order to check its data emitted. I’m also using a callback from jest called done which will wait until it is called before finishing the test.</p><p>On line 19, I created a variable mockSignal to reference the season signal from the service. On line 20, I call the <em>selectedSeasonChanged()</em> function with a new season value and right after I called the <em>flushEffects()</em> functions to make this signal change to be read.</p><blockquote>This sequence is important, because the withLatestFrom operator needs that its source had emitted some data before the roundSelected$ emits.</blockquote><p>Then, I called the <em>roundSelected()</em> function that will trigger the emission from the roundSelected$ observable which starts the HTTP requests.</p><p>Finally, I mocked the return from both HTTP requests with the mocks that I created in the beginning of the test and I’m also using a timeout of 10s since this code is asynchronous.</p><p><a href="https://github.com/kaleb22/rxf1-angular">Here is the link to my repository on github.</a> You can check the complete files there.</p><p>That’s it guys! I hope you’ll find this article useful.</p><p>Nice coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f407c6f993ae" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular Signals — An overview]]></title>
            <link>https://kaleb-dalla.medium.com/angular-signals-an-overview-4da66c9f591e?source=rss-bfd3b01818df------2</link>
            <guid isPermaLink="false">https://medium.com/p/4da66c9f591e</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[angular2]]></category>
            <category><![CDATA[reactive-programming]]></category>
            <dc:creator><![CDATA[Kaleb Dalla]]></dc:creator>
            <pubDate>Thu, 23 Jan 2025 12:43:37 GMT</pubDate>
            <atom:updated>2025-02-08T10:46:58.836Z</atom:updated>
            <content:encoded><![CDATA[<h3>Angular Signals — An overview</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/577/1*Ke2rL_A6KYegy0WmiXqDCw.png" /><figcaption>retrieved from: <a href="https://tech.sparkfabrik.com/en/blog/angular-signals-whats-all-the-fuss-about/">https://tech.sparkfabrik.com/en/blog/angular-signals-whats-all-the-fuss-about/</a></figcaption></figure><p>Hey Hey folks!! I hope you’re doing great. This is my first article of 2025 and I want to start wishing you all a happy new year!! Nothing better than starting this period studying and sharing knowledge right?</p><p>In this article I want to talk about the new Signals API that angular released as a developer preview in v16 and that become stable in v17. Let’s discussed what is this new API, why we should use it and how to use it. Shall we begin?</p><p>Let’s start answering this question: What is the point of this new Signals API?</p><blockquote>The whole objective of Signals is to provide a new way for our <strong>code</strong> to tell our <strong>templates</strong> that our data has changed which will improve Angular’s <strong>change detection</strong> and make or code more <strong>reactive</strong>.</blockquote><p>Here is a simple example that will illustrate the why:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1de1fc0fbb9333200bd439aa5c8b6db8/href">https://medium.com/media/1de1fc0fbb9333200bd439aa5c8b6db8/href</a></iframe><p>What is the value printed by console log on line 4 and then line 6? If your answer is 3, it is correct. The value of a + b is assigned when the <strong>expression is first evaluated</strong>. The variable z <strong>doesn’t react to changes</strong> in a or b. And that is the exact problem that signals want to solve. The whole idea of Signals is to provide an easy way to our code react to changes!</p><p>How can we react to changes prior to signals? One way is to create a getter method. For example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/48f9a315a1c23f690dc8be67ec3b2eee/href">https://medium.com/media/48f9a315a1c23f690dc8be67ec3b2eee/href</a></iframe><p>By defining the method to get the total, when the quantity changes, Angular’s change detection kicks in, the price is updated and the template can see the updated value.</p><p>Another way to implement the same functionality is using events. We could define a method called onQuantitySelected that will be bind to an event on the template and get executed when the user changes the quantity.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e4847b15e09d8cea295740d951cd31d5/href">https://medium.com/media/e4847b15e09d8cea295740d951cd31d5/href</a></iframe><p>With this approach we will get the same result, which is to update our data when some information changes.</p><p>These two approaches works for changes in the same component. But what if this change would need to reflect on a totally separated component? How that component would know when to update its information? That’s when Signals come in.</p><p>Here is an example using the Signals API approach.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1a70c489578c59f78df0947cbd56849f/href">https://medium.com/media/1a70c489578c59f78df0947cbd56849f/href</a></iframe><p>We create the quantity property as a Signal of number. Then we can define a computed signal called total that reacts and recalculate every time the quantity signal changes.</p><p>If we go back to the first example, we could rewrite it using signals, and the final result would be totally different, like below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd5746c2db442f9720f05f8393342d0f/href">https://medium.com/media/cd5746c2db442f9720f05f8393342d0f/href</a></iframe><p>We create a signal called a with the value of 1, a signal called b with the value of 2 and a computed signal c with the sum of a and b. If we change the value of a, the computed signal c will automatically reacts and recalculates and we get the value of 12.</p><p>If we were using the value of c on our template, Angular’s change detection will do its job and the value of c would be updated. Now, our components can be more reactive :D</p><p>In summary, Signals:</p><ul><li>Provide more reactivity to our code.</li><li>Allows a finer control over change detection.</li></ul><p>In the first part we talked a little bit about the why, now we are going to discuss: What is a signal?</p><blockquote>Signal = value + change notification.</blockquote><p><strong>A signal could be explained as a value + a change notification</strong>. To illustrate, we could think of a signal as a box, which will hold values inside it. Every time that the value changes, the box would glow providing a notification so others can see it.</p><p>Here is a few characteristics of a signal:</p><ul><li>It is reactive.</li><li>It always has a value.</li><li>It is side effect free — meaning that reading a signal can cause no other changes nor execute any other code.</li></ul><p>Last but not least, how can we use Signals? Let’s see some examples of how to create, read and set them.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e708f2020d5c044aca01c2708bb9ac2a/href">https://medium.com/media/e708f2020d5c044aca01c2708bb9ac2a/href</a></iframe><p>On line 1 to 4 we <strong>create</strong> some signals using the signal constructor function. We can declare a type or not, and the signal must always have a value.</p><p>On line 7 to 12 we see some examples of reading a signal. We can <strong>read</strong> them by calling its getter function using the <strong>signal name + () (empty parentheses)</strong>.</p><p>On line 15 there is an example of <strong>setting</strong> a new value to a signal using the <strong>set function</strong> providing a new value. There is also some other functions that are worth mentioning. We can use the<strong> update() function to update a signal value based on the current value</strong> and the <strong>mutate() function to mutate content in place, not the value itself</strong>.</p><p>When the value of a signal is updated, it automatically notifies any <strong><em>consumers*</em></strong> <em>(any code that is interested in notifications when the signal changes)</em> that the signal changed, so those consumers can update when it’s their turn to execute.</p><p>Finally, the last function that I want to talk about is the <strong>computed()</strong>. This function creates a signal that depends on other signals. It receives a computation function that is executed every time that any dependent signal changes and its value is read. <strong>This computed signal is read only</strong>, so it cannot be modified by set, update or mutate functions.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fb95e72896d15d5878f8ecbad1ee30d6/href">https://medium.com/media/fb95e72896d15d5878f8ecbad1ee30d6/href</a></iframe><p>The computed value of this signal is memoized, which means that it stores the computed result to be reused when its value is read by other consumers.</p><p>In conclusion, the new signals API is a powerful feature added to the Angular framework. It is also paving the way to make angular zoneless.</p><p>I wrote this article based on a video by <a href="https://medium.com/u/410e2ec02d13">Deborah Kurata</a> where she explains all the above in more details. You can check her video <a href="https://www.youtube.com/watch?v=oqYQG7QMdzw&amp;t=865s">here</a>.</p><p>I hope you find this writing useful.</p><p>Nice coding!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4da66c9f591e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to configure Jest in your Angular projects.]]></title>
            <link>https://kaleb-dalla.medium.com/how-to-configure-jest-in-your-angular-projects-d29155b8ed01?source=rss-bfd3b01818df------2</link>
            <guid isPermaLink="false">https://medium.com/p/d29155b8ed01</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[unit-test-framework]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[jest]]></category>
            <dc:creator><![CDATA[Kaleb Dalla]]></dc:creator>
            <pubDate>Mon, 04 Nov 2024 20:24:58 GMT</pubDate>
            <atom:updated>2024-11-04T20:24:58.578Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Wg875iKXAJ0ac69auFvcOg.png" /><figcaption>a Jest logo image</figcaption></figure><p>Hey Hey folks, how are you doing? In this article I want to discuss how you can configure Jest in your angular projects and why you should use it.</p><p>Let&#39;s start with why you should use Jest. Jest is a powerful JavaScript test framework with focus on simplicity. It works with a lot of frameworks, such as React, Vue and Angular. Below I list some reasons of why Jest is a good choice to use with Angular:</p><ul><li><strong>Quickness and Efficiency:</strong> Jest is known for its execution speed. It optimizes the time of running tests by adopting techniques such as parallel tests execution that can be very helpful in Angular projects where the compilation time can be a challenge.</li><li><strong>Perfect integration with Angular:</strong> Jest works perfectly with the Angular ecosystem since it supports TypeScript and offers advanced features to test pipes, components, services, etc.</li><li><strong>Mocking:</strong> Jest offers advanced mocking features that simplify the process of simulate external dependencies in tests. This is extremely handy with Angular where components frequently rely on external services.</li><li><strong>Simple and intuitive syntax: </strong>The Jest syntax is straightforward and easy to understand which decreases the learning curve for new comers to the Angular framework.</li><li><strong>Good integration with CI/CD pipelines:</strong> Jest works great with CI/CD pipelines because it runs on a node environment in contrast with Karma that runs on a browser.</li></ul><p>These are just a few reasons of why adopting Jest is a good choice. Now, let’s see how we can configure it on an Angular project.</p><p>To configure Jest, we’re going to work on four files, which are:<strong> angular.json</strong>, <strong>jest.config.ts</strong>, <strong>package.json</strong> and <strong>tsconfig.spec.json</strong>. Let’s start with the <strong><em>angular.json</em></strong> file. We need to change the test builder from Karma to Jest, and I will also remove the polyfills. Below, you can see the differences:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/718/1*HsQNVIRzXfK5ds-tLdjzMw.png" /><figcaption>an image showing the changes on the angular.json configuration file.</figcaption></figure><p>Then you need to create a <strong><em>jest.config.js</em> </strong>file. You can create this file in the root project tree. On it, we’re going to add the Jest preset and global configuration setup like below:</p><pre>module.exports = {<br>  preset: &#39;jest-preset-angular&#39;,<br>  globalSetup: &#39;jest-preset-angular/global-setup&#39;,<br>};</pre><p>After that, we’re changing the<strong> tsconfig.spec.json</strong> file to replace Jasmine with Jest. You can see the differences below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/718/1*dMCeYZ9CJ73-V2EXsmOyLQ.png" /><figcaption>an image showing the changes on the tsconfig.spec.json file.</figcaption></figure><p>Finally, we need to install the Jest and its dependencies and also remove karma and jasmine packages that we are not going to use anymore.</p><p>We can do that by using these commands:</p><pre>// to install jest, @types/jest and @angular-builders/jest<br>npm install --save-dev jest @types/jest @angular-builders/jest<br><br>// to uninstall karma and jasmine packages<br>npm uninstall @types/jasmine jasmine-core karma karma-chrome-launcher <br>karma-coverage karma-jasmine karma-jasmine-html-reporter</pre><p>Just a small disclaimer,<strong> the version of the @angular-builders/jest package must match the version of your angular core</strong>, so if you’re not using the latest angular version, you need to run the npm command specifying your angular version, for example:</p><pre>// to install @angular-builders/jest on an Angular v15 project<br>npm install --save-dev jest @types/jest @angular-builders/jest@15.0.0</pre><p>To finish, we can remove the <strong>karma.conf.js</strong> and <strong>test.ts</strong> files and we are good to go! Now you have setup Jest to work in your angular project and can run your tests with it.</p><p>In conclusion, it’s pretty simple to configure Jest in an Angular project and I hope this little hands on article help you in your dev journey.</p><p>Best wishes and nice coding!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d29155b8ed01" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[OOP in JavaScript. How it works?]]></title>
            <link>https://kaleb-dalla.medium.com/oop-in-javascript-how-it-works-93db2f63cd1d?source=rss-bfd3b01818df------2</link>
            <guid isPermaLink="false">https://medium.com/p/93db2f63cd1d</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[object-oriented-program]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[oop-concepts]]></category>
            <dc:creator><![CDATA[Kaleb Dalla]]></dc:creator>
            <pubDate>Fri, 28 Jun 2024 20:50:13 GMT</pubDate>
            <atom:updated>2024-06-28T20:50:13.941Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*rk52T7T9icNAIx-y" /><figcaption>Photo by <a href="https://unsplash.com/@jstrippa?utm_source=medium&amp;utm_medium=referral">James Harrison</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Heeey Heey folks! I hope you all are doing great. In this article I want to talk about how the concept of OOP (Object Oriented Programing) works in JavaScript since it isn’t a OOP language such as Java or C#.</p><p><strong>JavaScript is a multi-paradigm language</strong> which uses the <em>imperative/procedural paradigm</em> and also the <em>functional programming paradigm</em>. They play a key part on how the language works.</p><p>The language did not have classes at all before they were added to the language specification in 2015, known as ES6, but allowed for object-oriented programming using prototype-based inheritance. JavaScript is still a prototype-based language, even though a<strong> </strong><strong>class </strong>keyword is available nowadays.</p><p>For us to understand how this all work we need to go back in time when there were no classes.</p><h3>Functional Programming Paradigm</h3><p>This paradigm produces programs by composing and applying functions. A function is a first class citizen, meaning that they can be bound to a name, passed as arguments and returned from other functions. This is one of the main characteristics of the JavaScript language.</p><h4>Constructor Function</h4><p>In JavaScript, almost everything is an object. So, when we wanted to create a template (blueprint) we would use a <strong><em>constructor function</em></strong> which is basically a function that looks like this:</p><pre>function Car(name, logo) {<br>  this.name = name;<br>  this.logo = logo;<br>}<br><br>const myCar = new Car(&#39;ferrari&#39;, &#39;horse&#39;);<br>myCar.name;<br>//=&gt; ferrari</pre><p>It’s a convention that the function name should start with a capital letter. Instances (objects) are derived from this template (blueprint) using the <strong>new </strong>keyword.</p><h4><strong>The Prototype</strong></h4><p>All objects in JavaScript have a prototype. The prototype is another object that the original one <em>inherits</em> from. In others words, the original object has access to all of its prototype’s methods and properties. So, every prototype object <em>inherits</em> from Object.prototype. Let’s see an example here:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/862/1*5EiDRfQhSf6UTD8gUFSZxw.png" /><figcaption>JavaScript code on Chrome’s console</figcaption></figure><h4>The Prototype Chain</h4><p>This is a key concept on how inheritance works in JavaScript. When you try to access any <strong>property</strong> (field or method) of an object, JavaScript will first check if that property exists on the object itself. If not, it continues to look for the property in the object referenced by the prototype . Let’s have a look on an example:</p><pre>function Car(name, logo) {<br>  this.name = name;<br>  this.logo = logo;<br>}<br><br>Car.prototype.addGas(quantity) {<br>  // ...<br>}<br><br>const myCar = new Car(&#39;ferrari&#39;, &#39;horse&#39;);<br>myCar.addGas(10);<br>myCar.toString();</pre><p>If we inspect the myCar object, we would not find the function addGas() and toString()defined on it. So, when the myCar.addGas() function is called, what JavaScript does is to look on the Car.prototype to check if the property exists there. Since addGas() is defined on the Car.prototype the search ends and the function is run successfully. Now, let’s see what happens when the myCar.toString() function is called. First, JavaScript will look on the Car.prototype , but it will not find the function there. So, it will try to find this property on the next layer (next prototype in the chain), which is the Car.prototype.prototype that references the Object.prototype (the prototype property of the <strong>Object</strong> <strong>constructor function</strong>) that every object on JavaScript inherits from by default, and it contains general methods that are available for all JavaScript objects, such as toString(). In summary, that is how the prototype chain works.</p><h4>Instance fields</h4><p>Inside the constructor function we use the <strong>this keyword which represents the new object that will be created via </strong><strong>new</strong>. this will be automatically returned from the constructor function when it is called with new. That means we can add fields to the new instance by adding them to this in the constructor function.</p><pre>function Car(name, logo) {<br>  this.name = name;<br>  this.logo = logo;<br>  this.emptyFuel = false;<br>}<br><br>const myCar = new Car(&#39;ferrari&#39;, &#39;horse&#39;);<br>myCar.emptyFuel<br>//=&gt; false<br>myCar.logo<br>//=&gt; horse</pre><h4>Instance methods</h4><p>Methods are added via the prototype property of the constructor function. Why? To save memory! Defining every property and method on the constructor function takes up a lot of memory, especially if you have many common properties and methods and a lot of created objects.</p><p>Defining them on a centralized, shared object which all the instances have access to, thus saves memory :D</p><p>Inside of a method, you can access the fields of the instance via this and it works because of the following general rule:</p><blockquote>When a function is called as a method of an object, its this is set to the object the method is called on.</blockquote><pre>function Car(name, logo) {<br>  this.name = name;<br>  this.logo = logo;<br>  this.emptyFuel = false;<br>}<br><br>Car.prototype.addGas(quantity) {<br>  if(this.emptyFuel) {<br>    //...<br>  }<br>  // ...<br>}<br><br>const myCar = new Car(&#39;ferrari&#39;, &#39;horse&#39;);<br>myCar.addGas(10);<br></pre><p>All right, now that we understood how the prototype chain works and how we simulate the creation of classes before ES6, let’s see how to use the new classkeyword introduced in 2015 to the language specification.</p><h4>Class Syntax</h4><p>Nowadays, JavaScript supports defining classes with the class keyword. The new syntax is more similar to how classes are written in languages like Java and C#. This provides a lesser learning curve for new developers switching over from those languages.</p><p><strong>In summary, this new syntax is basically a <em>syntactic sugar</em> over the existing prototype-based constructors that makes classes easier to read and write.</strong> However, what happens under the hood when you use the class keyword is that you are actually using prototype-based constructors!</p><h4>Class declaration</h4><p>Let’s see what changes from the object constructor approach to the new class syntax. Now, we can defined a class using the class keyword followed by its name and the class body in curly brackets. The body will contain the definition of the properties and methods of the object.</p><pre>class Car {<br><br>  constructor(name, logo) {<br>    this.name = name;<br>    this.logo = logo;<br>    this.emptyFuel = false;<br>  }<br><br>  addGas(quantity) {<br>  //...  <br>  }<br>}<br><br>const myCar = new Car(&#39;ferrari&#39;, &#39;horse&#39;);<br>myCar.addGas(10);</pre><h4>Private Fields</h4><p>By default, all instance fields are <strong>public</strong> in JavaScript. They can be directly accessed and assigned to. Since ES2022 you can define <strong>private</strong> fields for a class using <strong>#</strong> sign prefixed with identifiers. This fields should never be accessed directly from outside the class.</p><p>To access private fields you will need to define <strong>getters</strong> and <strong>setters</strong>. With the keywords get and set you can define functions that will be executed when a property with the same name as the function is accessed or assigned to. Let’s see an example:</p><pre>class Car {<br>  #km;<br><br>  constructor(name, logo) {<br>    this.name = name;<br>    this.logo = logo;<br>    this.emptyFuel = false;<br>    this.#km = 0;<br>  }<br><br>  get km() {<br>    return this.#km;  <br>  }<br><br>  set km(kilometers) {<br>    this.#km = kilometers;<br>  }<br><br>  addGas(quantity) {<br>  //...  <br>  }<br>}<br><br>const myCar = new Car(&#39;ferrari&#39;, &#39;horse&#39;);<br>myCar.km = 500;</pre><h3>Conclusion</h3><p>Oh boy! That was a lot, I know. But, I hope that I was able to give an overview of how OOP works in JavaScript. I always like to quote the phrase from Seneca, a Roman philosopher, that said:</p><blockquote>While we teach, we learn.</blockquote><p>And this article was first to me, to really understand the details of the JavaScript language.</p><p>Hope you guys enjoyed. :D</p><p>Nice coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=93db2f63cd1d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular Standalone Component’s — Simplify your code.]]></title>
            <link>https://kaleb-dalla.medium.com/angular-standalone-components-simplify-your-code-550feaa69b47?source=rss-bfd3b01818df------2</link>
            <guid isPermaLink="false">https://medium.com/p/550feaa69b47</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[lazy-loading]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[standalone-component]]></category>
            <dc:creator><![CDATA[Kaleb Dalla]]></dc:creator>
            <pubDate>Tue, 23 Jan 2024 21:59:10 GMT</pubDate>
            <atom:updated>2024-01-23T21:59:10.117Z</atom:updated>
            <content:encoded><![CDATA[<h3>Angular Standalone Component — Simplify your code.</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yTDYNFykRhoGlK0nN8Fuzg.png" /><figcaption>retrieved from <a href="https://blogs.halodoc.io/angular-standalone-components-unleashed-exploring-the-magic-of-a-world-without-ngmodule/">https://blogs.halodoc.io/angular-standalone-components-unleashed-exploring-the-magic-of-a-world-without-ngmodule/</a></figcaption></figure><h3>Intro</h3><p>Hey Hey folks, how are you? In this article I want to talk about a new angular feature called standalone components.</p><p>Standalone components were a new feature added to the Angular framework in v14 as a developer preview. In v15+ versions it became stable, so now we can use it in production to simplify our code.</p><p>So, what standalone component means?</p><blockquote>It means that a component can, well… stand alone :D — ba dum tss.</blockquote><p>Jokes aside, with the new standalone API, now it is possible to create components, directives and pipes without creating or updating an associated NgModule. That is amazing! By using standalone components you don’t even need to know what a NgModule is, which breaks a barrier for new starters with this framework.</p><p>In summary:</p><blockquote>a standalone component is self contained and it directly manage their template dependencies, so they’re easier to understand, test and maintain.</blockquote><p>So, what are some of the benefits of using this approach?</p><ul><li>You write less code and have fewer files to manage, since you don’t need an NgModule anymore. Moreover, it enhances efficiency and reduces the bundle size.</li><li>Lower learning curve to new developers, since it becomes easier to create components, directives and pipes.</li><li>You can take full advantage of the lazy-loading feature with standalone components.</li></ul><h3>Hands-on</h3><p>Let’s get down to some practical code, shall we?</p><p>First question that I made to myself: Is it possible to update/migrate an existing angular component to be standalone?</p><p>The answer is: Yes! And it’s very straightforward to accomplish it, as long as you are using a v14+ angular version. You basically have two things to do:</p><ol><li>Update your @component metadata to include the standalone: true property and the imports: [] array. In this array, you are going to import all the dependencies that your component template is using e.g. *ngIf, *ngFor, *ngClass, etc.</li></ol><p>2. Finally, you can remove your component from any NgModule that it might be declared on. And that’s it! :D</p><p>Let’s see an example. Imagine that you have a component that you want to migrate to be a standalone one. Bellow you can see a structure of aDriverComponent.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7179115a480b4b2bcf987fb9c6b64baf/href">https://medium.com/media/7179115a480b4b2bcf987fb9c6b64baf/href</a></iframe><p>The first thing that I’m going to do is to add the standalone: true and imports:[]properties to the component’s metadata. It should look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c7eb1cef73ae31fb26d69b325f09a15/href">https://medium.com/media/3c7eb1cef73ae31fb26d69b325f09a15/href</a></iframe><p>Then, we need to go to this component’s template and identify all the features that it’s using so we can import those in the imports array. Let’s do this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9cf27b725ac46bc575d9c4123b7da093/href">https://medium.com/media/9cf27b725ac46bc575d9c4123b7da093/href</a></iframe><ul><li>In lines 5, 17, 18, 20 and 28 I’m using the directives <em>*ngIf, *ngFor, ngStyle</em> and the <em>Async pipe</em>. So, I can import all these guys in the imports array, or a simpler way would be to import the CommonModule that already has all of them.</li><li>In line 4, I’m using the <em>NgModel </em>directive. Thus, I’m also going to need to import the FormsModule that exports this guy for us.</li><li>From lines 2 to 10, I’m using a mat-form-field and a mat-select component from the angular material library and these pals come from the MatFormFieldModule and MatSelectModule . We need to add them too.</li><li>And finally, I’m also using a app-body-title component from my on application on line 14. Therefore, this one too goes to the imports array.</li></ul><p>After identifying all the dependencies and putting them on the imports array, our component may look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/611dfbfe2e1574d63464a7500f68820a/href">https://medium.com/media/611dfbfe2e1574d63464a7500f68820a/href</a></iframe><p>To finish this update, we just need to remove the DriverComponent from the declarations array on the NgModule that it has been declared on. In our case, in the app.module.ts .</p><p>And that is it!! By doing these steps you can easily migrate your component to be a standalone one.</p><h3><strong>Lazy Loading</strong></h3><p>To finish this article I want to talk about lazy loading and how it became super simple to work with when using standalone components.</p><p>By using the lazy loading approach, your application can load and bootstrap faster, since the main bundle will be smaller.</p><p>To use it, is very straightforward. You just need to modify your app-routing.module.ts to use the loadComponent function as shown bellow.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f17b29edf9fc74475ee2c1267a8a02a/href">https://medium.com/media/9f17b29edf9fc74475ee2c1267a8a02a/href</a></iframe><p>And there is also this GIF that I made to show the lazy loading in action.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W6AV5-QPLU14k33UB-M71A.gif" /><figcaption>a gif showing the lazy loading approach in an angular application.</figcaption></figure><p>In conclusion, standalone components are a very nice new feature introduced to the framework which brings lot’s of benefits. I recently migrate some components from a application that I built to study this concept and write this article.</p><p>You can find my commit doing this migration <a href="https://github.com/kaleb22/rxf1-angular/commit/27ac434cade3260832b7926a3bb1663244d93bd5#diff-1b44dae3a21aa33a5a98d1fdf32f2a8462f68965bbe4ab354175f85f975f69f8">here</a>. Please, feel free to reach me out with any questions :)</p><p>I hope you like this article.</p><p>Nice coding!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=550feaa69b47" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular — 3 Ways to automatically unsubscribe]]></title>
            <link>https://kaleb-dalla.medium.com/angular-3-ways-to-automatically-unsubscribe-153e9d0501ae?source=rss-bfd3b01818df------2</link>
            <guid isPermaLink="false">https://medium.com/p/153e9d0501ae</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[rxjs-operators]]></category>
            <category><![CDATA[patterns]]></category>
            <category><![CDATA[reactive-programming]]></category>
            <category><![CDATA[rxjs]]></category>
            <dc:creator><![CDATA[Kaleb Dalla]]></dc:creator>
            <pubDate>Fri, 24 Nov 2023 20:41:45 GMT</pubDate>
            <atom:updated>2023-11-24T20:41:45.735Z</atom:updated>
            <content:encoded><![CDATA[<h3>Angular — 3 Ways to automatically unsubscribe</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TmxXSD3Y42kNtC5oEpLjEg.png" /><figcaption>retrieved from <a href="https://brandslogos.com/a/angular-logo-1/">https://brandslogos.com/a/angular-logo-1/</a></figcaption></figure><p>Hey Hey Folks, how are you doing?</p><p>In this article I want to talk about three nice ways you can automatically unsubscribe from your observables, which are more elegant than using the unsubscribe method.</p><p>Before we dive in: Why I should unsubscribe from my observables?</p><blockquote>In a short answer: to avoid memory leaks.</blockquote><h3>#1 Async Pipe</h3><p>The first approach you can use to automatically unsubscribe from observables is to use the Async Pipe in your templates. I extremely recommend that you try to use it whenever is possible. Why?</p><p>By using this pipe, the Angular framework will be responsible for automatically subscribe and unsubscribe from the observable, so you don’t have to do anything. Pretty good, isn’t it?</p><p>I already wrote about the Async Pipe in this article where I discuss two design patterns that you can use with HTTP requests. You can check it out <a href="https://medium.com/@kaleb-dalla/rxjs-two-data-retrieve-patterns-for-you-to-use-a30c732ccd33">here</a>. :)</p><p>Bellow we have a use example of the Async Pipe in a template file. The <em>driver$</em> variable is an observable which is automatically subscribed and unsubscribed by using this approach.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c3db02bf0c4c77b0cea18e974ecb5c58/href">https://medium.com/media/c3db02bf0c4c77b0cea18e974ecb5c58/href</a></iframe><p>For the next 2 approaches we are going to use RxJS operators. If you don’t know what is RxJS or why should you use it, check out my other article <a href="https://kaleb-dalla.medium.com/rxjs-why-should-you-learn-it-721fcda7f431">here</a> where I discuss some keys points about this library.</p><h3>#2 takeUntil Operator</h3><p>This approach is a very common one and I already used it myself a couple of times. I’m going to use the <em>takeUntil </em>operator to manage the subscribe and unsubscribe from my observable.</p><p>What this operator does?</p><blockquote>Emit values until provided observable emits.</blockquote><p>In other words, it receives a notifier, which is an observable, as an input and keeps emitting values into the stream until the notifier emits the first value.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fcd681b8c25d455a860baebd58e064d2/href">https://medium.com/media/fcd681b8c25d455a860baebd58e064d2/href</a></iframe><p>So to use this pattern, you will need to create an observable, which I called <em>unsub$</em>, that I’m going to use to emit a value when my component is destroyed. By doing so, it will automatically unsubscribe from the observable on the method <em>ngOnInit</em>, avoiding memory leaks.</p><h3>#3 take Operator</h3><p>Finally, I’m going to talk about the take operator. This guy is perfect to use with HTTP requests. Why? Because usually with this type of requests, I’m only expecting to receive one value. So, if I’m not going to use a retryOnError function, for example, there is no need to keep listening to a HTTP request after it emitted a value or an error.</p><p>What take operator does?</p><blockquote>Emit provided number of values before completing.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec375d8c9f3337d44ea39312d99bcc5f/href">https://medium.com/media/ec375d8c9f3337d44ea39312d99bcc5f/href</a></iframe><p>This operator expects a number as an input. This number will be the number of times that I want values to keep flowing into my stream of data. In case of HTTP requests, I’m only interested in the first value, so 1 is the number I’m going to use. And after one value is emitted, the observable completes and it automatically unsubscribes from the outer observable.</p><p>So that’s it. Above there are three nice ways to use reactive programming in your favor. Enjoy!! hahah</p><p>I based this article in a video from <a href="https://medium.com/u/377429f128c6">Loiane Groner</a> where she explains these approaches in detail. You can watch it <a href="https://www.youtube.com/watch?v=0w2i7h5c9jk&amp;t=1198s">here</a>. :)</p><p>nice coding!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=153e9d0501ae" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RxJS —Two Data Retrieve Patterns for you to use.]]></title>
            <link>https://kaleb-dalla.medium.com/rxjs-two-data-retrieve-patterns-for-you-to-use-a30c732ccd33?source=rss-bfd3b01818df------2</link>
            <guid isPermaLink="false">https://medium.com/p/a30c732ccd33</guid>
            <category><![CDATA[patterns]]></category>
            <category><![CDATA[rxjs-operators]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[angular2]]></category>
            <category><![CDATA[rxjs]]></category>
            <dc:creator><![CDATA[Kaleb Dalla]]></dc:creator>
            <pubDate>Mon, 23 Jan 2023 17:30:35 GMT</pubDate>
            <atom:updated>2023-01-23T17:30:35.364Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6FVTz9uNuBnElsfWrp2tKw.png" /><figcaption>RxJS logo.</figcaption></figure><p>Hey Hey foooolks! I hope you all are doing great.</p><p>In this article I want to talk about some patterns that we can use with RxJS which will make your code better and less verbose :)</p><p>Before we begin, I just want to recommend the reading of my other article if you want to have an overview about the RxJS library:</p><p><a href="https://medium.com/@kaleb-dalla/rxjs-why-should-you-learn-it-721fcda7f431">RxJS — Why shoud you learn it?</a></p><p>To exemplify the patterns bellow, I’m going to use a context of an Angular application consuming data from the <a href="https://documenter.getpostman.com/view/11586746/SztEa7bL#intro">F1 API.</a></p><p>So, the first pattern that I want to talk about is:</p><p><strong>Retrieving data pattern</strong> — This is one of the most common operations that we have to deal with in our daily routines. To exemplify this concept, let’s see the classic pattern for retrieving data. We usually have a service like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e65c6495df28351a313c26fe92a73ba0/href">https://medium.com/media/e65c6495df28351a313c26fe92a73ba0/href</a></iframe><p>The code above should look very familiar to you. We are simply doing an HTTP request to get some data and using the pipe operator to pipe the <em>tap()</em> and <em>catchError()</em> operators from RxJS in order to display the data or catch any errors that may occur during the request. Now, let’s have a look in the code of our component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/430a608b81908e922c8810d6070d56ae/href">https://medium.com/media/430a608b81908e922c8810d6070d56ae/href</a></iframe><p>Again, a code that should look very familiar to you. In the component file, we are simply taking the <em>drivers array</em> received from the request and putting it in the component level property so we can bind this in the template file.</p><p>Now, what I want to highlight is that the code above is<strong> Procedural</strong>. What I mean by procedural is that we are using a <em>getDrivers()</em> method in order to retrieve our data. What if we took a declarative approach instead? Let’s compare to see how it would look like.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f97f366a343d7ee2f78eceb5e0643ad/href">https://medium.com/media/1f97f366a343d7ee2f78eceb5e0643ad/href</a></iframe><p>With the declarative approach, instead of calling a function to get the data, we are simply declaring a variable with the sufix $ (the dollar sign in the end of a variable name is a convention to identify observables) and assigning the return of the HTTP request to this variable. Now we have an observable that holds the result of the operation. Let’s see how the component file would look like with this change:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6aa982a161edd6f96be5105cda4ad573/href">https://medium.com/media/6aa982a161edd6f96be5105cda4ad573/href</a></iframe><p>Ooooh, boy. Instead of 10 lines of code, now we would have just one in our component file. <strong>With the declarative approach we don’t need to subscribe and unsubscribe from our observable anymore.</strong> Therefore, we don’t need the life cycle hooks <em>onInit </em>and <em>onDestroy </em>either. But will this work? We need to subscribe to an observable in order to it emit a value, right?</p><blockquote>Yes, that is correct and we can do that by using the <strong>async pipe</strong> in the template file.</blockquote><p>The async pipe is an angular pipe, do not confuse it with the pipes from RxJS. According to the angular documentation, what the async pipe does is:</p><blockquote>It automatically subscribes and unsubscribes to an Observable or Promise and returns the latest value it has emitted.</blockquote><p>In our template file, we would have something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c3db02bf0c4c77b0cea18e974ecb5c58/href">https://medium.com/media/c3db02bf0c4c77b0cea18e974ecb5c58/href</a></iframe><p>So, this syntax async as drivers means that the<em> async pipe</em> will automatically subscribe and unsubscribe to the <em>drivers$</em> <em>observable </em>and put the value emitted in the <em>drivers variable </em>that we can now access in our template :)</p><blockquote>This is the first pattern that you can use to simplify your code!</blockquote><p>The second pattern that I want to talk about is a variation of the first, which is:</p><p><strong>Retrieving Data on Action pattern</strong>— Again, another pretty common operation that we need to deal with in our routines. So, for this example, let’s imagine that we have a select field filled with seasons from 2018–2022. We want to select a season and based on this user action, show information about the races of this season. Let’s start with our service again.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7819cf4e0d9774539110a3d96e8c93c0/href">https://medium.com/media/7819cf4e0d9774539110a3d96e8c93c0/href</a></iframe><p>The first thing that we do is to create a Subject of type string. A Subject is a special type of observable that we can use. We are going to use this Subject to emit the data coming from the UI. Second, we create a public variable that will be our observable. Note the $ sign at the end of the variable name again to indicate that it will be an observable. Then, we’re going to defined a method called <em>selectedSeasonChanged()</em> that will be responsible for emitting the data coming from the UI.</p><p>Now, based on the season selected by the user, we need to fire a HTTP request to get the data from races of that particular season. We know that our <em>raceSeasonSelectedAction$ </em>will emit a new season, so now we can pipe this observable to access its data flow.</p><p>*<strong>Important note here</strong>: Notice that now we have an inner observable from the this.http.get and also the outer observable which is the raceSeasonSelectedAction$<em>.</em></p><blockquote>So in order to subscribe to an inner observable and flatten the result, we need to use a higher-order mapping operator (flattening operator). These operators automatically subscribe and unsubscribe to inner observables. e.g. <strong>switchMap</strong>, <strong>concatMap</strong> and <strong>mergeMap.</strong></blockquote><p>We’re going to use the switchMap operator<strong> because it stops the current operation and performs a new one automatically every time a new data is emitted</strong>, which makes him perfect to use with HTTP requests. The code in our service is pretty much done. Let’s have a look in our component code now.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7d24ad31e719004b9c2603a3a1f8a9cf/href">https://medium.com/media/7d24ad31e719004b9c2603a3a1f8a9cf/href</a></iframe><p>We’re defining a seasons array and initializing it in our constructor with values from 2018–2022. Then, again we are binding the observable from our service to a component variable in order to use it in our template (line 9). Finally, we defined a method called <em>onSeasonSelected()</em> that will be responsible for getting the value selected by the user and sending it to the method in our service which will emit a new data on the stream.</p><p>Last, but not least we have our template file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d2fa2026e17a19a96ae86f74b376bf7/href">https://medium.com/media/9d2fa2026e17a19a96ae86f74b376bf7/href</a></iframe><p>On lines 1 to 8 we are simply defining a select box which will hold the season values and when the user click on a season, it will call the function <em>onSeasonSelected() </em>that will pass its value to the service to be emitted into the stream.</p><p>In conclusion, these were the two patterns that I wanted to share with you all. I have created a little application based on the F1 APIs to study these concepts and you can find the code <a href="https://github.com/kaleb22/rxf1-angular">here</a> and a live preview of the application <a href="http://rxf1-angular.vercel.app/">here</a>.</p><p>Also, this article is based on the video from <a href="https://medium.com/u/410e2ec02d13">Deborah Kurata</a> which you can watch it <a href="https://www.youtube.com/watch?v=vtCDRiG__D4&amp;t=1938s">here</a>. It really changed my mind regarding how to think about reactive components after I watched her other video <a href="https://www.youtube.com/watch?v=CZYAph73mnI">here</a>. I strongly recommend you follow her on YouTube :)</p><p>I hope you like this article.</p><p>Nice coding!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a30c732ccd33" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why you should build your own Pokédex.]]></title>
            <link>https://kaleb-dalla.medium.com/why-you-should-build-your-own-pok%C3%A9dex-7f58c25c9792?source=rss-bfd3b01818df------2</link>
            <guid isPermaLink="false">https://medium.com/p/7f58c25c9792</guid>
            <category><![CDATA[development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[programming-skills]]></category>
            <category><![CDATA[dev]]></category>
            <category><![CDATA[tips]]></category>
            <dc:creator><![CDATA[Kaleb Dalla]]></dc:creator>
            <pubDate>Wed, 09 Nov 2022 16:46:20 GMT</pubDate>
            <atom:updated>2022-11-09T16:46:20.058Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7ZfmJQdgK5xoMxJ4wW8idQ.png" /><figcaption>My pokédex logo image.</figcaption></figure><p>Hey folks, I hope you are doing great!</p><p>Why am I writing a story about Pokémon? Hahahah. The short answer is:</p><blockquote>Everybody loves Pokémon. And if you don’t like it, you’re not a good person. Joking! :)</blockquote><p>The long answer is: I always found difficult to motivate myself to study new topics in programming. What I discovered is that it’s much easier to study something if you are having fun along the way. And that was exactly what I had while building my own Pokédex: <strong>I was having fun while building it!</strong></p><p>So my main objective with this short article is to tell you:</p><blockquote>If you are finding it difficult to keep yourself motivated to study, my suggestion to you is — Try to find something that you really like and think about a small application that you can build with that theme.</blockquote><p>For me, Pokémon was the perfect theme choice and maybe it is a good theme to you too!</p><p>Before we talk about some of the resources you can use to build your own Pokédex, I will leave my own implementation so maybe you can inspire yourself :)</p><p>You can check my <a href="http://angular-pokedex-teal.vercel.app/">Angular Pokédex here.</a> And the source code <a href="https://github.com/kaleb22/angular-pokedex">here</a>.</p><p>So, what are the resources that you can use in order to build your own Pokédex?</p><ul><li><a href="https://pokeapi.co/">PokeAPI</a> — This a web site that has a great documentation about all the endpoints available to you so you can “<strong><em>catch</em></strong>” your Pokémon information. <em>And the most important part: it is FREE! :)</em></li><li><a href="https://www.figma.com/">Figma</a> — is an excellent source of resources to get design ideas for your applications. There is a lot of free templates available to use. You can check some of the Pokédex designs <a href="https://www.figma.com/community/search?model_type=hub_files&amp;q=pokedex">here</a>.</li></ul><p>Basically these were the two main resources that I used to build my Pokédex, alongside with the Angular framework.<br>I chose to use Angular because it is the framework that I’m currently studying, but you can choose whatever framework/language you want.</p><p>To conclude this story, before you start your PokéCoding adventure, I highly recommend the reading on this article — <a href="https://www.freecodecamp.org/news/how-to-think-like-a-programmer-lessons-in-problem-solving-d1d8bf1de7d2/"><em>How to think like a programmer</em> </a>— by Richard Reis.</p><p>In summary, the article above gives you a nice framework to solve problems which is defined in three simple steps:</p><ol><li>Understand the problem.</li><li>Plan your solution.</li><li>Divide — break a problem into a smaller one.</li></ol><p>I strongly recommend that you follow these steps before starting to code your application. A good tool to use in order to plan your solution is a flow chart. You can see an example of a Pokédex flow chart <a href="https://www.figma.com/community/file/1139911273397210434">here</a>.</p><p>When I started to apply these concepts into my daily routine I realised how helpful they are to organize your thoughts before starting some new task.<br>I hope they can help you as well :)</p><p>Nice coding!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7f58c25c9792" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How does Javascript work? An overview of the fundamentals.]]></title>
            <link>https://kaleb-dalla.medium.com/how-does-javascript-work-an-overview-of-the-fundamentals-86d98cb8e1bd?source=rss-bfd3b01818df------2</link>
            <guid isPermaLink="false">https://medium.com/p/86d98cb8e1bd</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[call-stack]]></category>
            <category><![CDATA[javascript-environment]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript-development]]></category>
            <dc:creator><![CDATA[Kaleb Dalla]]></dc:creator>
            <pubDate>Mon, 12 Sep 2022 16:23:51 GMT</pubDate>
            <atom:updated>2024-02-06T12:37:38.431Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nDtBtfwjx0pFAT9lIYk8-g.jpeg" /><figcaption>Javascript logo. retrieved from <a href="https://postimg.cc/D8b5Bw05">https://postimg.cc/D8b5Bw05</a></figcaption></figure><p>Hey Hey guys, in this article I want to talk about a topic that I didn’t fully understand until some time ago. I’ve been working as a full stack developer for almost 5 years now and to be very honest with you, I didn’t fully understand how the Javascript language worked under the hood.</p><p>I studied Computer Science in college and as many of you know, javascript isn’t a language taught in school, like Java or C++. The first time that I came across with Javascript I was already employed as a Junior developer at Itau. Thus, I developed a lot of projects using the language without knowing much of its core concepts. Not that there is a huge problem with not knowing every little detail about the language, but if we want to become better developers we need to have a deeper understanding of the tools that we work with.</p><p>A Roman philosopher said:</p><blockquote>“While we teach, we learn”.</blockquote><blockquote>Seneca</blockquote><p>That is why I decided to write this article.</p><p>I will start with this statement which I think it’s quite accurate in defining the Javascript language:</p><blockquote>Javascript is a single threaded language that can be non-blocking.</blockquote><p>Alright, but what does it mean? Let’s have a look on the image below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RLbK8nM3pfLWPu4qIUaWww.png" /><figcaption>An example of the Javascript memory heap and call stack.</figcaption></figure><p>The Javascript code that we write needs an environment in which it can be run. Each browser has its own implementation of the JS Environment. In chrome you will find the V8 Engine, edge uses Chakra JS Engine, firefox uses SpiderMonkey and safari WebKit.</p><p>The JS Environment is basically compose by a Memory Heap, a Stack and a Call Stack. And, what are those? hahah</p><blockquote>The <strong>memory heap</strong> is where the dynamic memory allocation will take place. e.g. where your objects, functions, arrays will be stored. <br>The <strong>stack</strong> is used to store static data whose size is known by the engine during compile time. e.g. primitives, variable initialisation.<br>The <strong>call stack</strong> keeps track of which part of the code is being executed.</blockquote><p>Let’s see an example of how the Call Stack works.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1df34637cc2d9a20f7d65889a82ee574/href">https://medium.com/media/1df34637cc2d9a20f7d65889a82ee574/href</a></iframe><p>In the code example above, we have two simple functions declared and on line 8 we call the function one to execute.</p><p>The Call Stack will look like this after line 8 executes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/671/1*Fbun7y88ioYgPQ3_PCy6-Q.png" /><figcaption>an image representing the call stack.</figcaption></figure><p>Then, we call function two that will be added to the call stack as well and finally the console.log()will be called and added to the call stack which will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/676/1*SmXK5N11UEzCfpT6uMpUwg.png" /><figcaption>an image representing the call stack.</figcaption></figure><p>Then it starts to pop the functions that are finished. First it will pop the console.log() function, then it pops function two and finally function one.<br>This is a very simple example to illustrate how the call stack works with Javascript.</p><p>Do you remember the statement that I mentioned in the beginning of this article?</p><blockquote>Javascript is a <strong>single threaded </strong>language that can be non-blocking.</blockquote><p>So, what <strong>“single threaded”</strong> means is that <strong>there is only one Call Stack. </strong>In other words, you can do only one thing at a time.</p><p>Awesome! now to understand the last part of the statement <strong>“a language that can be non-blocking”</strong> we will need to talk about the Javascript Run-Time Environment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FA9NGxNB6-v1oI2qGEtlRQ.png" /><figcaption>an image representing the Javascript run time environment.</figcaption></figure><p>The Javascript Run-Time Environment is included in all browsers and on top of the JS Engine it adds the Web APIs, Callback Queue and Event Loop.</p><p>As you can see in the image above, setTimeOut is not part of the Javascript itself but it comes from the Web APIs that all browsers makes available for us to use.</p><p>To illustrate how all of these concepts work together, let’s see an example.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1aaefe4dc57ff6f5e390bf1e4632171e/href">https://medium.com/media/1aaefe4dc57ff6f5e390bf1e4632171e/href</a></iframe><p>What order to you think the numbers will be displayed in the console when the code runs? Let’s verify what happens line by line.</p><p>First, console.log(1) will be added to the call stack and right after it gets executed it will be popped out from the stack.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tQDkmEl_ebquyAA6VgZiIA.png" /><figcaption>an image representing the call stack, web api, callback queue and event loop.</figcaption></figure><p>Then, we have a setTimeOut function that will be added to the call stack. Now the Call Stack sees it and says:</p><blockquote>Hmmm, I have a function here that is not part of the Javascript, but it is from the Web API. Let me notify the Web API that setTimeOut has been called.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gJo3BgmRFycd6C4KGZmheg.png" /><figcaption>an image representing the call stack, web api, callback queue and event loop.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q97MqwI675O1JJtJ1prKpQ.png" /><figcaption>an image representing the call stack, web api, callback queue and event loop.</figcaption></figure><p>After that, setTimeOut is popped from the Call Stack and the Web API will start a timer of 2 seconds. <em>It will know that in 2 seconds some task will need to be executed</em>. Now, since the Call Stack is empty, the Javascript Engine goes to line 7 and adds the console.log(3) into the Call Stack.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1016/1*GSAAcT1n0ce9zI-pV-3fWg.png" /><figcaption>an image representing the call stack, web api, callback queue and event loop.</figcaption></figure><p>console.log(3) gets executed and popped out from the Call Stack. And 2 seconds has passed. So, Web API says:</p><blockquote>Alright, setTimeOut needs to be executed right now. Let me send a callback to the Callback Queue.</blockquote><p>So, setTimeOut gets popped out from the Web API and gets added in the Callback Queue.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FZiZiWd7ZvW_qWn5ArqtXQ.png" /><figcaption>an image representing the call stack, web api, callback queue and event loop.</figcaption></figure><p>Here comes the Event Loop into scene. What this guy does is to keep asking the question:</p><blockquote>Hey, is the Call Stack empty?</blockquote><p>If the answer is yes, the Event Loop goes and checks the Callback Queue and asks:</p><blockquote>hmm, is anything in there? Because the Call Stack is empty and we can throw something in there to be executed.</blockquote><p>Finally, callback goes into the Call Stack and by running it we get console.log(2) . Then, console.log(2) and callback are popped out from the Call Stack.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1010/1*I_SrlbaGRibvZ4bmc2gKBQ.png" /><figcaption>an image representing the call stack, web api, callback queue and event loop.</figcaption></figure><pre>what gets printed in the console:<br>1<br>3<br>2</pre><blockquote>and there you go! Everything is empty again and the whole program was executed.</blockquote><p>In conclusion,</p><blockquote>Javascript is a <strong>single threaded </strong>language <strong>that can be non-blocking.</strong></blockquote><p>In other words, Javascript has only one Call Stack that allows it to do one thing at a time. In order to not block the single thread, Javascript can be asynchronous with callback functions.</p><p>Oh boy, that was a lot of information hahahah<br>But, I hope that makes sense to you.</p><p>Nice coding!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=86d98cb8e1bd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to test a component that uses the ActivatedRoute class in Angular?]]></title>
            <link>https://kaleb-dalla.medium.com/how-to-test-a-component-that-uses-the-activatedroute-class-in-angular-6561a474e8f9?source=rss-bfd3b01818df------2</link>
            <guid isPermaLink="false">https://medium.com/p/6561a474e8f9</guid>
            <category><![CDATA[activatedroute]]></category>
            <category><![CDATA[jest]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[tutorial]]></category>
            <dc:creator><![CDATA[Kaleb Dalla]]></dc:creator>
            <pubDate>Tue, 30 Aug 2022 22:51:35 GMT</pubDate>
            <atom:updated>2022-08-30T22:51:35.787Z</atom:updated>
            <content:encoded><![CDATA[<p>Hey everyone,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Wg875iKXAJ0ac69auFvcOg.png" /><figcaption>The Jest logo. Retrieved from <a href="https://acervolima.com/testando-com-jest/">https://acervolima.com/testando-com-jest/</a></figcaption></figure><p>Not a while ago I had to write some tests to a component which used the ActivatedRoute class to check some query params in order to do some logic and it took me quite some time until I figured out a solution in order to mock the ActivedRoute class. Because of that, I decided to write this little tutorial as documentation and I hope it helps someone in the future who has the same doubt.</p><p>Alright, for this example will be using Jest as out test framework.</p><p>So, let’s imagine that we have an Angular application that will make an API request to get some data and based on the response it will use the angular router to navigate to another component passing some information through queryParams. An example of a component with this logic is shown bellow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/145eb880d483c57b892cbda924530df3/href">https://medium.com/media/145eb880d483c57b892cbda924530df3/href</a></iframe><p>Now, let’s see an example of the implementation of the someComponent which will access the information from the queryParams through the ActivatedRoute class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a1a768edda7eadc45a07bf73ddc1672c/href">https://medium.com/media/a1a768edda7eadc45a07bf73ddc1672c/href</a></iframe><p>Nice, now that we have these components set up, how can we test the someComponent? In order to do so, we will need to mock the ActivatedRoute class so we can provide some fake information in the test that will simulate the same behavior on our component. So, let’s do that.</p><p>If we look into the documentation of the ActivatedRoute class we will note that the queryParams is an Observable that has type of Params.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/1*Oauu7JQXz8D_rKN0Wgcpiw.png" /><figcaption>A screenshot of the ActivatedRoute class blueprint from the angular docs.</figcaption></figure><p>Now that we know what is the queryParams we can try to mock it in our test. One way of doing it is shown below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/49a6ac9ceab08408ce0bfabab8a18539/href">https://medium.com/media/49a6ac9ceab08408ce0bfabab8a18539/href</a></iframe><p>What we are doing in the example above is creating a class which has a property called queryParams that is an Observable. With this observable, we are defining an object called urlParams which has a property called type. By doing so, we are creating a class that mocks the same behaviour implemented in the ActivatedRoute class.</p><p>Now that we have our mock class ready, we can provide it to Jest so when it see the ActivatedRoute instance on the someComponent it will use the mock class instead of the actual ActivatedRoute class which is not instantiated.</p><p>Let’s see how we can finally write the basic test of the someComponent.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/92417551766d1e23105beac239adb150/href">https://medium.com/media/92417551766d1e23105beac239adb150/href</a></iframe><p>And that is it guys. Now you can test your component that makes use of the ActivatedRoute class. I hope this tutorial helps you!</p><p>Nice coding!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6561a474e8f9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>