<?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 Vlad Ilie on Medium]]></title>
        <description><![CDATA[Stories by Vlad Ilie on Medium]]></description>
        <link>https://medium.com/@ilievlad73?source=rss-8de1db7d4073------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*ynGKqHCsm6bUnqIB</url>
            <title>Stories by Vlad Ilie on Medium</title>
            <link>https://medium.com/@ilievlad73?source=rss-8de1db7d4073------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 19:05:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ilievlad73/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[Make the most out of your Redux middleware]]></title>
            <link>https://ilievlad73.medium.com/make-the-most-out-of-your-redux-middleware-dc24a461c3a1?source=rss-8de1db7d4073------2</link>
            <guid isPermaLink="false">https://medium.com/p/dc24a461c3a1</guid>
            <category><![CDATA[cqrs]]></category>
            <category><![CDATA[redux-middleware]]></category>
            <category><![CDATA[event-driven-programming]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[reactive-programming]]></category>
            <dc:creator><![CDATA[Vlad Ilie]]></dc:creator>
            <pubDate>Wed, 31 Mar 2021 19:35:42 GMT</pubDate>
            <atom:updated>2021-03-31T19:35:42.506Z</atom:updated>
            <content:encoded><![CDATA[<p>Hello! Welcome to a deep dive into Redux middleware. We will cover together in this article the following topics: what is a <strong>Redux middleware </strong>and how it’s implemented, how to create a middleware, and how to reason about it. Further, we are going to explore the <strong>Redux messaging system</strong> by looking at how different <strong>messaging patterns</strong> can be implemented through Redux middlewares.</p><p>First things first, before jumping into more action, make sure you’ve watched <a href="https://medium.com/u/91e7a417e575">Nir Kaufman</a>’s <a href="https://www.youtube.com/watch?v=5gl3cCB_26M&amp;ab_channel=JSHeroes">talk</a> on advanced patterns in Redux.</p><p>Now! Let’s recap a few key aspects of Redux:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uCL0q84KqzBsqW6Oqe9EmQ.png" /><figcaption>Redux Architecture</figcaption></figure><ol><li><strong>CQS Pattern: Command</strong>/<strong>Query &lt;=&gt; Action</strong>/<strong>Selector</strong>.</li><li>Redux is a lightweight implementation of <strong>Flux</strong>.</li><li>The <strong>state </strong>is updated by triggering <strong>reducers </strong>through <strong>actions</strong>.</li><li>The <strong>state </strong>is read through <strong>selectors</strong>.</li><li>Only the <strong>middleware </strong>layer can accept actions and dispatch new actions.</li><li><strong>Redux</strong> is a <strong>messaging system</strong>.<strong> </strong>The other Redux powers (centralized state management, time travel, predictable, etc…) are based on its messaging capabilities.</li><li>Actions should be plain serializable objects.</li><li>It’s all about <strong>action/event </strong>processing.</li></ol><h4>The Redux middleware</h4><blockquote>Redux middleware provides a third-party extension point between dispatching an action, and the moment it reaches the reducer.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d4ba0919c0a66ce6d093f3100623c50/href">https://medium.com/media/8d4ba0919c0a66ce6d093f3100623c50/href</a></iframe><p>Seems cool and everything…but why it looks like that? And <strong>who is next anyway?</strong></p><h4>The Redux <em>applyMidleware</em> store enhancer</h4><p>Let’s take a look at the way the applyMiddleware store enhancer is implemented. You can find the full source code <a href="https://github.com/reduxjs/redux/blob/master/src/applyMiddleware.ts">here</a>. Keep in mind that a store enhancer adds extra functionality to the Redux store.</p><p>The applyMiddleware enhancer will replace the default dispatch with a chain of middlewares. Each middleware will receive the next middleware and the last middleware will receive the store dispatch function in the next param.</p><p>If a middleware won’t call next(action) , then the current action will be discarded from the chain and it will never reach the rest of the middlewares. This means that the current action will never reach your reducers since the default dispatch call won’t happen.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dec57486af1976700090aab8f2d7ff9b/href">https://medium.com/media/dec57486af1976700090aab8f2d7ff9b/href</a></iframe><h4>The <a href="https://github.com/reduxjs/redux-thunk">Redux Thunk</a></h4><p>Let’s check out how the thunk middleware is implemented and how it can be used to achieve async actions. By using thunks, the code is not reusable. If you want to delay some action you would just duplicate it? How about the throttle and debounce support? How about an API call? The action creators for success and error must be created each time you add a new API call and the fetch logic will be identical. If later on, you want to add the possibility to cancel a request, all redux API thunks must be updated.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a8aeeded63d5a3361bed211441b277d4/href">https://medium.com/media/a8aeeded63d5a3361bed211441b277d4/href</a></iframe><p>Let’s create our custom middleware that can manage action delays. We will update the action definition by adding some <strong>metadata</strong>. This way, the actions will be more declarative and the action instructions (delay, throttle, denounce) implementation can be extracted into <strong>separate middlewares</strong>.</p><h4>Delay Middleware</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae2863b88d6a9a7aa34e9ff344cc1472/href">https://medium.com/media/ae2863b88d6a9a7aa34e9ff344cc1472/href</a></iframe><h4>Throttle Middleware</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7d921ed3ff948df40f400e1b3521c974/href">https://medium.com/media/7d921ed3ff948df40f400e1b3521c974/href</a></iframe><p>Now that we had our introduction into Redux middleware, let’s discover its true power by looking at different <a href="https://www.enterpriseintegrationpatterns.com/patterns/messaging/"><strong>messaging patterns</strong></a>.</p><h3>Messaging patterns</h3><h4>Filter</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s1R2i9W-j--e3IjAPrfvaA.png" /><figcaption>Filter middleware overview</figcaption></figure><p>Noticeably, the throttle and the debounce middleware are implementing some filtering logic on the stream of actions. You can try to emulate more RxJS operators like <em>bufferCount</em> or<em> skipUntil.</em></p><h4>Map</h4><p>You can make decisions based on context (testing, development, production) or based on action content (payload, meta). For instance, maybe you want that, in production, all actions to be throttled by default. Clearly, this middleware should be placed before the throttle middleware.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IeV7Gxg3BEuiGeTGPY05sQ.png" /><figcaption>Map middleware overview</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a77796a2600d03ea8a8f855c7a2d9ad5/href">https://medium.com/media/a77796a2600d03ea8a8f855c7a2d9ad5/href</a></iframe><h4>Split</h4><p>The splitter pattern will take an action and will translate it into multiple actions. A simple use case can be displaying a global loading animation when data is being fetched in our application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8S8Js9OvK6QTp8-FQKQHJA.png" /><figcaption>Split middleware overview</figcaption></figure><p>For now, don’t worry about the API_REQUEST event. We will see later how the API middleware will process that event.</p><h4>Aggregate</h4><p>The aggregator will gather multiple events and will react based on them. Let’s say that we want to wait for three API requests to finish and then remove the global display loading animation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GO6OGupqZK1_DKSgGsT_6A.png" /><figcaption>Aggregate middleware overview</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b16931430c1a8c54b294a23f9848a6e1/href">https://medium.com/media/b16931430c1a8c54b294a23f9848a6e1/href</a></iframe><h4>Enrich</h4><p>As the name suggests, the<em> enrich middleware </em>is used to supply additional props to an event. A middleware that adds a timestamp to the request is a simple example of an enrich middleware.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fY9bdbzllJbioxazZf89kw.png" /><figcaption>Enrich middleware</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ff4b448231bf2719fb5478d893b20434/href">https://medium.com/media/ff4b448231bf2719fb5478d893b20434/href</a></iframe><h4><strong>Normalize</strong></h4><p>Assuming you are familiar with the <a href="https://github.com/paularmstrong/normalizr">normalizr</a> library, let’s try to emulate it!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/65a536f8dae992685815cf137f63b095/href">https://medium.com/media/65a536f8dae992685815cf137f63b095/href</a></iframe><h3>API Middleware</h3><p>It’s time to expose an API request handler for the whole system through a middleware. The idea is as follows: an action is dispatched that contains all the necessary information for the API middleware to process the request. After the request has been processed, the API middleware will dispatch an event, notifying the system about the result of the request (success/error). By doing this, we are isolating the API logic in a single place, making it easy to maintain and add more features(eg. cancel requests). Here is a minimal working example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d86b64f6861528d4b81ffd5a68a26975/href">https://medium.com/media/d86b64f6861528d4b81ffd5a68a26975/href</a></iframe><p>In order to use this, a middleware has to be created for each <strong>feature</strong>. The feature middleware will be responsible for dispatching the API_REQUEST_${FEATURE} event. Furthermore, the feature middleware will listen for API_SUCCESS_${FEATURE} and API_FAILURE_${FEATURE} events and react accordingly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xj88mFVtAJa6HR9tq9-gTA.png" /><figcaption>API middleware overview</figcaption></figure><ul><li>API_SUCCESS_USERS — dispatch SET_USERS and APP_PENDING(populate application state and update global loader state)</li><li>API_FAILURE_USERS — dispatch SET_NOTIFICATION and APP_PENDING(trigger notification regarding the received error, update global loader state)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2_DtXLe_Opg5srG8_6vrfw.png" /><figcaption>API middleware success/failure flow</figcaption></figure><h3>Thinking in actions</h3><p>The only way we can modify the application state is by dispatching an action. Remember, we can dispatch an action from the UI or from a middleware. The actions can be categorized by their intention into three groups:</p><ul><li>Command — <strong>starts </strong>a procedure that will trigger multiple actions. A command should <strong>never </strong>be processed by a reducer because it doesn’t carry the final data that needs to be written to the state ({type: GET_USERS, ...})</li><li>Event — <strong>notifies</strong> the system about the starting, progress, or ending of a flow. An event should <strong>never</strong> be processed by a reducer because reducers don’t carry the final data that needs to be written to the state ({type: API_REQUEST_PENDING, ...})</li><li>Document — <strong>updates </strong>the application state. The action should contain the final shape of the data. The reducer <strong>should not </strong>perform any kind of manipulation on the action payload ({type: SET_USERS, ...})</li></ul><p>This separation will be useful in the design and implementation of flows.</p><h3>The end</h3><blockquote>Actions in…actions out…</blockquote><p>It’s all about <strong>actions!</strong> <strong>Consume </strong>actions and <strong>produce </strong>actions. The API middleware is a good example of how we can expose a service to the system through events. In the same fashion, we can implement a Router middleware that will handle the routing requests. This middleware will also emit ROUTE_INIT and ROUTE_EXIT events. By doing this we can handle route authorization at the middleware level, we can fetch data when a route enters, we can add cleanup logic if a route exists, etc.</p><p>How the system will handle <strong>authentication/authorization</strong>? Through a middleware, of course. This middleware should be responsible for managing the access tokens and, if any action requires authorization, it will <strong>inject </strong>them. A good practice is to publish an event if the <strong>authentication </strong>middleware has injected the required data because we want to track the flow of events straight from the logs.</p><p>The View layer should remain as simple as possible. It is not responsible for authorization or fetching data when a component has mounted. The middleware layer is responsible for these actions.</p><p>Thank you for reading! I’m will leave you with some common flow requirements that can be implemented really nicely by <strong>programming with actions</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BK_39DhEfJy1T_ZmQCUOSA.png" /><figcaption>Bring together Router and API events</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dc24a461c3a1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>