<?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 Sourabh Parmar on Medium]]></title>
        <description><![CDATA[Stories by Sourabh Parmar on Medium]]></description>
        <link>https://medium.com/@sourabh.parmar?source=rss-fbe5ec35157------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*GH5RO1FZAl0FglNT.</url>
            <title>Stories by Sourabh Parmar on Medium</title>
            <link>https://medium.com/@sourabh.parmar?source=rss-fbe5ec35157------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 19:59:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sourabh.parmar/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[Improved Handling of APIs in React-Redux apps: Introduction to NetPack]]></title>
            <link>https://medium.com/@sourabh.parmar/improved-handling-of-apis-in-react-redux-apps-netpack-479a8aa018f4?source=rss-fbe5ec35157------2</link>
            <guid isPermaLink="false">https://medium.com/p/479a8aa018f4</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[ajax]]></category>
            <category><![CDATA[redux]]></category>
            <dc:creator><![CDATA[Sourabh Parmar]]></dc:creator>
            <pubDate>Wed, 08 Aug 2018 17:58:47 GMT</pubDate>
            <atom:updated>2018-08-20T18:44:47.789Z</atom:updated>
            <content:encoded><![CDATA[<p>In a typical web application, AJAX calls plays an important role. It is an approach to Web application development that uses client-side scripting to exchange data with the Web server.</p><p>In modern application development, <em>fetch </em>is preferred over <em>ajax</em>. You can find the differences between <em>fetch</em> and <em>ajax</em> <a href="https://stackoverflow.com/questions/35549547/what-is-the-difference-between-the-fetch-api-and-xmlhttprequest">here</a>. <strong>Fetch returns a promise</strong> and gives the flexibility to handle the responses in a modern and better way.</p><p>Generally, whenever we make an API call, there is a certain algorithm which we follow. We handle the API failure or undesired response along with the successful response. If we write the logic to handle these kinds of responses at every API level, then that’s a problem, we are certainly repeating ourselves.</p><p>This is the <strong>part one </strong>in a series of two blog posts, where we will see how we can write a <strong>common utility logic to make the API calls</strong> (using fetch function) and handle the responses. I will be using React-Redux, but the logic is generic and can be used with vanilla JS or any of the JS framework.</p><h3>NetPack</h3><p>NetPack is a utility for making fetch requests with basic error handling of network issues or parsing issues or any backend issues and polling for the data in case of network failure.</p><p>Let’s look at the different parts of the NetPack class:</p><p><strong>The Constructor</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/54fe9f62c0e962c090909f6f1e22c506/href">https://medium.com/media/54fe9f62c0e962c090909f6f1e22c506/href</a></iframe><p>It accepts 3 arguments: redux dispatch method, error handlers and default options.</p><p><strong>Core logic</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2f539521c15b1a33482c449ae61b9d8c/href">https://medium.com/media/2f539521c15b1a33482c449ae61b9d8c/href</a></iframe><p>Basically, we are using fetch to make the asynchronous call.</p><p>If the <strong>promise is resolved</strong>, we check for the response. If the status is <em>OK, </em>we get a valid response. Furthermore, this response can be checked for any <em>parse error</em>. In case of any parse error, we can dispatch an action to inform the redux store about the parse error.</p><p>If the status is not <em>OK, </em>we are certain that there is a backend error, and we can inform the redux store about the backend error.</p><p>If the <strong>promise is rejected</strong>, there is some error in the network. We can inform the redux store regarding the network error and parallelly we can re-fetch the failed API again in an exponential time frame.</p><p>Once the redux store is updated with the appropriate errors, appropriate error component is rendered.</p><p><strong>The user-facing methods</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be5d0703ba088ab8b724ad2366ce80a1/href">https://medium.com/media/be5d0703ba088ab8b724ad2366ce80a1/href</a></iframe><p><em>ajax</em> method returns an object containing functions to formats the coreUtil method and calls the core function based on the request types (GET&lt; POST&lt; PUT or DELETE).</p><h3>How to use NetPack</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d1284c7a9d27edb0b3635f312ec3aa64/href">https://medium.com/media/d1284c7a9d27edb0b3635f312ec3aa64/href</a></iframe><p>We create an instance of NetPack class by passing redux dispatch method, action creators for errors and some default options and then export the ajax method of the NetPack class so that appropriate functions can be called (get, post, put or delete).</p><h3>Limitations of NetPack</h3><p>Nothing is perfect in life. The logic for NetPack was written in 2016. While using NetPack in my first application, I realized that there are many things, which can be done in a better way. Below is the list of few limitations I was able to identify:</p><ul><li>Unable to handle <strong>race condition</strong>: To understand race condition for APIs, let’s take an example: We have a web page where a list of books is displayed, a user can click on an individual book to get more information about the book. On click of a book from book list, we make an API call and fetch the data related to that book. The data is stored in the individualBookDetails reducer. We have a component which gets the data from the reducer and renders the view of the individual book. Let’s assume user clicks on Book1, but the API is failed due to some network error. According to the polling mechanism of the NetPack, it will keep trying to fetch Book1 data in an exponential time frame. Meanwhile, the user clicks on Book2, and the call is successful, so Book2 data will be stored in the individualBookDetails reducer and will be shown to the user. Let’s assume we get Book1 data now, this data will be stored in the reducer and the view will be re-rendered. The user who clicked on Book2 will be able to see Book1 data now. This problem arose because we don’t have a logic to handle the race condition in the NetPack.</li><li>Need <strong>access to the redux store</strong>: In case of any error, NetPack dispatch an action to inform the store about the error. For this, it needs access to the store. There is nothing majorly wrong here, but I felt that NetPack is doing more than what it should do.</li><li>Non-optional<strong> retry mechanism</strong>: In case of any network error, NetPack will always try to re-fetch the API in an exponential time frame. But based on use case, there can be few APIs where we don’t need to retry to fetch the data in case of any failure.</li><li>No logic to store <strong>internet connectivity status</strong>: NetPack is responsible to make API calls to the server. This can be used as an opportunity to store the internet connectivity status.</li><li>Only focused on <strong>making and handling the API call</strong>: This can go as an improvement rather than a limitation. Generally, when we make an API call, we follow a common trend (like form the URL, format a request, activate loader, make the request, parse the response, deactivate loader etc). NetPack can be extended to handle these kinds of common functionalities.</li></ul><h3>Conclusion</h3><p><strong>Abstraction</strong> is an important concept in computer science. NetPack is nothing but an abstraction over the way we deal with APIs. We are hiding the logic to fetch the data and handle the errors and providing the user with simple methods.</p><p>In this post, we focused on the NetPack. We saw the logic, use case and how we can use it. We dug dipper into the utility and figured out few challenges as well. In <strong>part two </strong>of this series, we will see how we can solve these challenges.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=479a8aa018f4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Sequencing animations in ReactJS]]></title>
            <link>https://medium.com/prod-io/sequencing-animations-in-reactjs-9ea0c4263fea?source=rss-fbe5ec35157------2</link>
            <guid isPermaLink="false">https://medium.com/p/9ea0c4263fea</guid>
            <category><![CDATA[animation]]></category>
            <category><![CDATA[queue]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Sourabh Parmar]]></dc:creator>
            <pubDate>Thu, 04 Jan 2018 11:40:25 GMT</pubDate>
            <atom:updated>2018-08-03T06:28:23.426Z</atom:updated>
            <content:encoded><![CDATA[<p>Animations are very important to create a great user experience. As developers, we deal with complex animations to make the user experience even better. In this post, I won’t be talking about the animation, rather I will be focusing on how we can sequentialize complex entities, including animations, UI elements etc.</p><p>Recently, while working on a web app, built on ReactJS, I came across this problem of sequentializing multiple animations. There was a scenario, where I needed to animate multiple components, in a particular sequence. For example, one component had to finish before another starts animating. The problem was growing with the requirements.</p><p>I googled the problem and found some solutions, but as my application started to grow, it became very difficult to solve all the problems with the help of a single solution. Finally, I decided to write a solution on my own and tried to abstract it, so that most of the problems can be generalised and solved easily.</p><h4>The Solution</h4><p>Let’s say we have three components in our app “A, B and C”, and A needs to be animated first followed by B followed by C.</p><pre>                           <strong>A =&gt; B =&gt; C</strong></pre><p><strong>If the current animating component can notify the next component to be animated, the problem is solved.</strong></p><p>To notify other components in ReactJS, either we can give responsibility to the component itself, or to the common parent, or to the topmost level, i.e redux.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VanqKHyC_BvmWLR8iwFIVw.jpeg" /></figure><p><em>Direct communication between components is considered as bad practice in ReactJS. There are few limitations in putting the logic at the parent level, for example, if the components need to be animated do not share the same parent, or maybe they are too far from each other.</em></p><p><strong><em>So it’s better to put the logic in the Redux. </em></strong>All our components listen to store and can be notified when to animate.</p><h4>In Action</h4><p>Let’s start by creating a simple reducer which will have a queue of components need to be animated.</p><pre>state = {<br> animationQueue: [A, B, C]<br>}</pre><p>Should <strong>animationQueue </strong>be<strong> </strong>a normal queue or priority queue?</p><p>There is a possibility that components need to be animated based on <em>priority</em>. A high priority animation cannot wait in the queue and needs to be animated as soon as possible. Based on the use case, animationQueue can be a normal queue or a priority queue. In this post, we will assume animationQueue to be priority queue so that we can cover as many use cases as possible.</p><p>All the components will have a priority associated with them, and as soon as there is an entry in the queue, it will shuffle and will be arranged based on the priority.</p><pre>state = {<br>  animationQueue: [<br>    {<br>      A,<br>      priority: 1<br>    },</pre><pre>    {<br>      B,<br>      priority: 2<br>    },<br>    {<br>      C,<br>      priority: 3<br>    }<br>  ]<br>}</pre><p>What’s next? We need another variable to store the current animation component.</p><pre>state = {<br>  animationQueue: [<br>    {<br>      B,<br>      priority: 2<br>    },<br>    {<br>      C,<br>      priority: 3<br>    }<br>  ],<br>  currentAnimation: &#39;A&#39;<br>}</pre><p>What about <strong>currentAnimation, </strong>can we think of a scenario where we need to animate multiple components together?</p><p>Yes, right? And we know the solution as well, our currentAnimation can’t be a string anymore, it needs to be an array.</p><pre>state = {<br>  animationQueue: [<br>    {<br>      B,<br>      priority: 2<br>    },<br>    {<br>      C,<br>      priority: 3<br>    }<br>  ],<br>  currentAnimation: [A]<br>}</pre><p>Now the reducer is ready and is responsible for managing the animation queue and the current animation.</p><p>All the components are subscribed to the store. The components in currentAnimation array start animating. Once a component is finished animating, it notifies the reducer, which pops out that components from the currentAnimation array. Once the currentAnimation array is empty, the next set of components are dequeued from the animationQueue and pushed to the currentAnimation array in order to animate.</p><p>While dequeuing from animationQueue, we need to make sure that we dequeue <em>all the same priority components</em> and push them into the currentAnimation array.</p><p>In this way, we can ensure that all the components are animated in the particular order only.</p><h4>Only Animations??</h4><p>Is the solution restricted to only sequencing the animations? The answer is <strong>no.</strong></p><p>The solution is generic, and we can push anything to our queue. For example, if we want to <em>render a component</em> once “A” finishes animating, and on some user interaction we want to remove the component from the DOM and starts animating “B”. It’s simple; assign the correct priority to each entity and listen to the reducer. Whenever your turn comes, do your job, and once the job is done, inform the reducer, it will take care of the remaining things.</p><p>So all the major things, be it animation, rendering a component, opening and closing of popups or anything actionable, which should be part of any sequence, can be solved using this approach.</p><h4>Multiple Sequences</h4><p>Till now we discussed a single sequence consisting of one or more elements. But we can have a scenario where <strong>a sequence of animations needs to be followed by another sequence of animations</strong>.</p><p>We can create a single sequence by merging the two sequences, but that’s not the ideal solution because it’s not scalable. And also the individual sequences may have there own significance, and should not be merge.</p><p>The solution is to have one more queue, the <strong>sequenceQueue</strong>, either a priority or normal, depending on the use case. The sequenceQueue is responsible for managing the multiple sequences and acts as a source of input for the animationQueue.</p><pre>state = {<br>  sequenceQueue: [<br>    {<br>      sequence: [<br>        {<br>          X,<br>          priority: 1<br>        },<br>        {<br>          Y,<br>          priority: 2<br>        },<br>        {<br>          Z,<br>          priority: 3<br>        }<br>      ],<br>      priority: 2<br>    }<br>  ],<br>  animationQueue: [<br>    {<br>      B,<br>      priority: 1<br>    },<br>    {<br>      C,<br>      priority: 2<br>    }<br>  ],<br>  currentAnimation: [A]<br>}</pre><p>The sequenceQueue dequeues a sequence only once the animationQueue is empty. It makes sure that a particular sequence is always executed in the same manner, and is uninterrupted by maybe even a high priority animation in another sequence.</p><p>So now we can handle multiple sequences without solution, and it’s scalable as well.</p><h4><strong>Conclusion</strong></h4><p>The generic nature of solution gives us the flexibility to modify it as per our requirement<strong>. </strong>I used this solution in my ReactJS app and discussed in that context only. However, if we look at the solution closely, we are using basic data structures and javascript events. So this solution is not restricted to ReactJS, but with slight modifications, it can be applied to any javascript framework.</p><p>The problem of sequencing complex entities is solved quite easily by using basic javascript concepts. The structured nature of the solution makes it very easy to implement and maintain.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9ea0c4263fea" width="1" height="1" alt=""><hr><p><a href="https://medium.com/prod-io/sequencing-animations-in-reactjs-9ea0c4263fea">Sequencing animations in ReactJS</a> was originally published in <a href="https://medium.com/prod-io">Prod.IO</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>