<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Öner Zafer</title>
    <description>The latest articles on DEV Community by Öner Zafer (@onerzafer).</description>
    <link>https://dev.to/onerzafer</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F129448%2F1e396898-6d63-4cb0-b033-76ffbdd1630f.jpeg</url>
      <title>DEV Community: Öner Zafer</title>
      <link>https://dev.to/onerzafer</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/onerzafer"/>
    <language>en</language>
    <item>
      <title>Understanding Micro Frontends</title>
      <dc:creator>Öner Zafer</dc:creator>
      <pubDate>Mon, 18 Feb 2019 10:46:01 +0000</pubDate>
      <link>https://dev.to/onerzafer/understanding-micro-frontends-1ied</link>
      <guid>https://dev.to/onerzafer/understanding-micro-frontends-1ied</guid>
      <description>

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tA1YtoHl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2ATmOhR6xBnpob2st-p77Weg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tA1YtoHl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2ATmOhR6xBnpob2st-p77Weg.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As a frontend developer, all these years you were developing monoliths, even though you already knew it was a bad practice. You divided your code into components, used &lt;em&gt;require&lt;/em&gt; or &lt;em&gt;import&lt;/em&gt; and defined npm packages in your package.json or mounted sub git repositories into your project, yet you ended up building a monolith. It’s time to change it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why is your code is a monolith?
&lt;/h3&gt;

&lt;p&gt;All frontend applications are a monolithic application in nature, except apps that already implemented micro frontends. The reason is if you are developing with the React library and if you have two teams both should be using the same React library and both teams should be in sync on deployments and always will be conflicting during code merges. They are not separated completely and most probably they are maintaining the same repository and have the same build system. The exit from a monolithic app is formulated as microservices. But it’s for backend! 😱&lt;/p&gt;

&lt;h3&gt;
  
  
  What is actually microservices?
&lt;/h3&gt;

&lt;p&gt;In general and the most simplistic explanation for microservices is, it is a development technique which allows developers to do independent deployments for different parts of the platform without harming other parts. The capability of independent deployment allows them to build isolated or loosely coupled services. To put this architecture on a more stable base there are some sets of rules to follow which can be summarized as follows: Each service should have only one task and it should be small. So the team who is responsible for this service should be small. About the size of the team and the project, one of the coolest explanation on the internet has been done by &lt;a href="https://martinfowler.com/articles/microservices.html#HowBigIsAMicroservice"&gt;James Lewis and Martin Fowler&lt;/a&gt; as below:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In our conversations with microservice practitioners, we see a range of sizes of services. The largest sizes reported follow Amazon’s notion of the &lt;strong&gt;Two Pizza Team&lt;/strong&gt; (i.e. the whole team can be fed by two pizzas), meaning no more than a dozen people. On the smaller size scale we’ve seen setups where a team of half-a-dozen would support half-a-dozen services.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I created a simple sketch to give a visual explanation for monolith and microservices:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vwxZvGwj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2ALwJvLwza7FDa7_C5kaBLpg.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vwxZvGwj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2ALwJvLwza7FDa7_C5kaBLpg.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can understand from the drawing above each service in microservices is a standalone application except UI. UI is still in one piece! When all services handled by one team and while the company is scaling up, the Frontend team will start struggling and won’t be able to keep up with it and this is the bottleneck of this architecture.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---1HNjNqo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AaSWnsL6W4VIKLqSGJPbYqg.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---1HNjNqo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AaSWnsL6W4VIKLqSGJPbYqg.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Additional to its bottleneck, this architecture will result in some organizational problems too. Assume that the company is growing and will adopt agile development methodologies which require &lt;em&gt;cross-functional&lt;/em&gt; small teams. On this common example, naturally, product owners will start to define the stories as frontend and backend tasks and the &lt;em&gt;cross-functional&lt;/em&gt; team will never be a real &lt;em&gt;cross-functional&lt;/em&gt; unit. It will be a shallow bubble which looks like an agile team but it will be separated deep inside. More on that managing this kind of team will be really a nail-biting duty. On each planning, there would be a question if there were enough frontend task or were there enough backend tasks in the sprint. To address all the problems described here and numerous others, a couple of years ago the &lt;strong&gt;micro frontends&lt;/strong&gt;  &lt;strong&gt;idea&lt;/strong&gt; has emerged and it started to gain popularity very quickly.&lt;/p&gt;

&lt;h3&gt;
  
  
  The solution to the bottleneck problem in microservices: Micro Frontends 🎉
&lt;/h3&gt;

&lt;p&gt;The solution is actually quite obvious, embrace the same principles which are working for backend services for many years: Divide the frontend monolith into small UI fragments. But UI is not quite similar to services, it is the interface between the end user and the product, it should be consistent and seamless. Even more, in the era of Single Page Applications, the whole application is running on the browser on the client side. They are not simple HTML files anymore, instead, they are sophisticated pieces of software reaching really complex levels. Now I feel like a definition of the micro frontend is necessary:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The idea behind Micro Frontends is to think about a website or web app as &lt;strong&gt;a composition of features&lt;/strong&gt; which are owned by &lt;strong&gt;independent teams&lt;/strong&gt;. Each team has a &lt;strong&gt;distinct area of business&lt;/strong&gt; or &lt;strong&gt;mission&lt;/strong&gt; it cares about and specialises in. A team is &lt;strong&gt;cross functional&lt;/strong&gt; and develops its features &lt;strong&gt;end-to-end&lt;/strong&gt; , from database to user interface. (&lt;a href="https://micro-frontends.org/"&gt;micro-fontend.org&lt;/a&gt;)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;From my experience so far, for many companies, it is really hard to directly adopt the architecture proposed above. Lots of others have a huge legacy burden which is nailing them down from migrating to a new architecture. For that reason a softer midway solution which is more flexible to allow easy adoption and secure migration is vital. After overviewing the architecture in more detail I will try to provide some insight into an architecture which is confirming the proposal above and allowing more flexible ways to follow. Before diving into the details, I need to build up some terminology.&lt;/p&gt;

&lt;h3&gt;
  
  
  The overall structure and some terminology
&lt;/h3&gt;

&lt;p&gt;Let’s imagine we are dividing the monolithic app structure vertically through business functionalities. We will end up with several smaller applications which has the same structure with the monolithic application. But if we add a special app on top of all these small monolithic apps, users will communicate with this new app and it will compose the old monolithic UI from each small app into one. This new layer can be named as &lt;strong&gt;stitching layer&lt;/strong&gt; because it gets the produced UI parts from each microservice and combines into one &lt;em&gt;seamless&lt;/em&gt; UI for the end user and this would be the most straight forward implementation of a micro frontend 🤩&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bh9oQFHo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/816/1%2AL9W7jz1jj3ur4XkWoUcgqg.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bh9oQFHo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/816/1%2AL9W7jz1jj3ur4XkWoUcgqg.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For a better understanding, I will refer to each small monolithic app as &lt;strong&gt;micro-app&lt;/strong&gt; since they all are standalone apps and not microservices only, they all have UI parts and each represents an end-to-end business functionality.&lt;/p&gt;

&lt;p&gt;As it’s already known, today’s frontend ecosystem is highly versatile and can be extremely complex. So this kind of straightforward solutions will not be sufficient enough when the time comes to implement it for a real product.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problems to solve
&lt;/h3&gt;

&lt;p&gt;While this article was just an idea, I started a Reddit thread to discuss the idea. Thanks to the community and their responses I can list some problems to be addressed and I will try to describe them one by one.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;How to create a seamless and consistent UI experience when we have a totally independent standalone&lt;/em&gt; &lt;strong&gt;&lt;em&gt;micro-apps&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Well, there is no silver bullet answer to this question but one of the ideas is creating a shared UI library which is a standalone &lt;strong&gt;micro-app&lt;/strong&gt; itself too. By that way, all the other micro-apps will depend on that shared UI library micro-app. In that case, we just created a shared dependency and we killed the idea of standalone &lt;strong&gt;micro-apps&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Another idea can be sharing &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_variables"&gt;CSS custom variables&lt;/a&gt; on the :root level. The advantage of this solution would be the global configurable theme between apps.&lt;/p&gt;

&lt;p&gt;Or we may simply share some SASS variables and mixins between app teams. The downside of this approach would be the repetitive implementation of UI elements and the integrity of the designs of similar elements should be checked and validated always for all the &lt;strong&gt;micro-apps.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;How do we make sure that one team is not overriding the CSS written by another team?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;One solution is CSS scoping via CSS selector names which are carefully selected by the micro-app name. By putting this scoping task to the &lt;strong&gt;stitching layer&lt;/strong&gt; will reduce the development overhead but will increase the responsibility of the &lt;strong&gt;stitching layer&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Another solution can be forcing each &lt;strong&gt;micro-app&lt;/strong&gt; to be a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements"&gt;custom web component&lt;/a&gt;. The advantage of this solution is the scoping done by the browser, but it comes with a price: it is nearly impossible to do server-side rendering with shadow DOM. Additionally, there is no 100% &lt;a href="https://caniuse.com/#search=custom%20elements"&gt;browser support&lt;/a&gt; for custom elements yet especially if you have to support IE.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;How should we share the global information between micro-apps?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This question points out to one of the most concerned issues on this topic, but the solution is pretty easy: HTML 5 has pretty powerful functionalities which are not well known by the majority of frontend developers. For example, &lt;strong&gt;custom events&lt;/strong&gt; are one of them and it is the solution for sharing information within the micro-apps.&lt;/p&gt;

&lt;p&gt;Alternatively any shared pub-sub implementation or T39 observable implementation can do the trick. If we want a more sophisticated global state handler we can implement a shared miniature Redux, by that way we can achieve more reactive architecture.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If all micro-apps are standalone apps, how do we do client-side routing?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This problem is up to each implementation by design. All major modern frameworks are providing powerful routing mechanisms on the client side by using browser history state. The problem is which application is responsible for the routing and when.&lt;/p&gt;

&lt;p&gt;My current pragmatic approach is creating a shared client router which is responsible only from the top level routes and the rest belongs to the respective micro-app. Let’s say we have a /content/:id route definition. The shared router will resolve /content part and the resolved route will be passed into ContentMicroApp. ContentMicroApp is a standalone server and it will be called with /:id only.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;We must have the server-side rendering for sure but is it possible with micro-frontends?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Server-side rendering is a tricky problem. If you are considering iframes to stitch the &lt;strong&gt;micro-apps&lt;/strong&gt; then forget about server-side rendering. Similarly, web components for stitching task are not powerful than iframes. But if each &lt;strong&gt;micro-app&lt;/strong&gt; is able to render its content on the server side then the &lt;strong&gt;stitching layer&lt;/strong&gt; will be responsible only for concatenating the HTML fragments on the server side.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Integration with a legacy environment is vital! But how?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;To integrate a legacy system, I would like to describe the strategy of my own that I named as “&lt;em&gt;gradual invasion&lt;/em&gt;”.&lt;/p&gt;

&lt;p&gt;First, we have to implement the stitching layer and it should have a functionality of transparent proxy. Then we can define the legacy system as a &lt;strong&gt;micro-app&lt;/strong&gt; by declaring a wildcard route to it: &lt;em&gt;LegacyMicroApp&lt;/em&gt;. So all the traffic will hit the stitching layer and will be proxied to the legacy system transparently since we don’t have any other micro-apps yet.&lt;/p&gt;

&lt;p&gt;Next step will be our first &lt;em&gt;gradual invasion&lt;/em&gt; movement: We will take a small bite from the &lt;em&gt;LegacyMicroApp&lt;/em&gt; by deleting the main navigation and replacing it with a dependency. This dependency will be a &lt;strong&gt;micro-app&lt;/strong&gt; implemented with a shiny new technology: &lt;em&gt;NavigationMicroApp&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Now the stitching layer will resolve each route as &lt;em&gt;LegacyMicroApp&lt;/em&gt; and it will resolve the dependency as &lt;em&gt;NavigationMicroApp&lt;/em&gt; and serve them by concatenating these two.&lt;/p&gt;

&lt;p&gt;Then the next bite will come for the footer by following the same pattern with the main navigation.&lt;/p&gt;

&lt;p&gt;And then we will continue taking similar small bites from &lt;em&gt;LegacyMicroApp&lt;/em&gt; until nothing left from it.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;How to orchestrate the client side so we won’t need to reload the page each time?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Well, the &lt;strong&gt;stitching layer&lt;/strong&gt; solves the problems on the server side but not on the client side. On the client side, after loading already glued fragments as a seamless HTML, we don’t need to load all the parts each time on URL change. Therefore we have to have some mechanism which loads fragments asynchronously. But the problem is, these fragments may have some dependencies and these dependencies need to be resolved on the client-side. That means a micro frontend solution should provide a mechanism to load &lt;strong&gt;micro-apps&lt;/strong&gt; and also some mechanism for dependency injection.&lt;/p&gt;

&lt;p&gt;According to the questions and possible solutions above, I can summarize everything under the following topics:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Client-side&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Orchestration&lt;/li&gt;
&lt;li&gt;Routing&lt;/li&gt;
&lt;li&gt;Isolation of micro-apps&lt;/li&gt;
&lt;li&gt;App to app communication&lt;/li&gt;
&lt;li&gt;Consistency between micro-app UIs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Server-side&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Server-side rendering&lt;/li&gt;
&lt;li&gt;Routing&lt;/li&gt;
&lt;li&gt;Dependency management&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  A flexible and powerful yet simple architecture
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZAUBazok--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2APPBwPDargPYJY8G5B1WP0w.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZAUBazok--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2APPBwPDargPYJY8G5B1WP0w.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, it worthed the wait all along this article! The basic elements and requirements of a micro frontends architecture finally started to reveal itself!&lt;/p&gt;

&lt;p&gt;With the guidance of these requirements and concerns, I started to develop a solution which I named as &lt;strong&gt;&lt;em&gt;microfe&lt;/em&gt;&lt;/strong&gt;. 😎 Here I will describe the architectural goal of this project by underlining its main components in an abstract manner.&lt;/p&gt;

&lt;p&gt;It is easy to start with client-side and it has three separate backbone-structures: &lt;em&gt;AppsManager&lt;/em&gt;, &lt;em&gt;Loader&lt;/em&gt;, &lt;em&gt;Router&lt;/em&gt; and one extra &lt;em&gt;MicroAppStore.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mQOp99UH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/886/1%2AUufGRwX-NCeuQfcohrW6yQ.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mQOp99UH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/886/1%2AUufGRwX-NCeuQfcohrW6yQ.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  AppsManager
&lt;/h4&gt;

&lt;p&gt;AppsManager is the core of client-side micro-app orchestration. The main functionality of AppsManager is to create the dependency tree. When all of the dependencies of a micro-app are resolved, it instantiates the micro-app.&lt;/p&gt;

&lt;h4&gt;
  
  
  Loader
&lt;/h4&gt;

&lt;p&gt;Another important part of client-side micro-app orchestration is the Loader. The responsibility of the loader is fetching the unresolved micro-apps from the server-side.&lt;/p&gt;

&lt;h4&gt;
  
  
  Router
&lt;/h4&gt;

&lt;p&gt;To solve client-side routing I introduced the Router into &lt;strong&gt;&lt;em&gt;microfe&lt;/em&gt;&lt;/strong&gt;. Unlike the common client-side routers, the &lt;strong&gt;&lt;em&gt;microfe&lt;/em&gt;&lt;/strong&gt; router has limited functionalities, It does not resolve the pages but micro-apps. Let’s say we have an URL /content/detail/13 and a &lt;em&gt;ContentMicroApp&lt;/em&gt;. In that case, the &lt;strong&gt;&lt;em&gt;microfe&lt;/em&gt;&lt;/strong&gt; router will resolve the URL up to /content/* and it will call &lt;em&gt;ContentMicroApp&lt;/em&gt; /detail/13 URL part.&lt;/p&gt;

&lt;h4&gt;
  
  
  MicroAppStore
&lt;/h4&gt;

&lt;p&gt;To solve micro-app to micro-app client-side communication I introduced MicroAppStore into &lt;strong&gt;&lt;em&gt;microfe.&lt;/em&gt;&lt;/strong&gt; It has the similar functionalities of Redux library with a difference: It is resilient to asynchronous data structure changes and reducer declarations.&lt;/p&gt;

&lt;p&gt;The server-side part can be a little bit more complicated in implementation but simpler in structure. It consists of only two main part &lt;em&gt;StitchingServer&lt;/em&gt; and lots of &lt;em&gt;MicroAppServer&lt;/em&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  MicroAppServer
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9yqgWUxI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2Aa7mbRuahRiOvQQI-yYe3IA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9yqgWUxI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2Aa7mbRuahRiOvQQI-yYe3IA.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Bare minimum functionality of a &lt;em&gt;MicroAppServer&lt;/em&gt; can be summarized as &lt;em&gt;init&lt;/em&gt; and &lt;em&gt;serve&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;While a &lt;em&gt;MicroAppServer&lt;/em&gt; booting up first thing it should do is calling &lt;em&gt;SticthingServer&lt;/em&gt; register endpoint with a &lt;em&gt;micro-app declaration&lt;/em&gt; which defines the micro-app &lt;em&gt;dependencies&lt;/em&gt;, &lt;em&gt;type&lt;/em&gt;, and &lt;em&gt;URL schema&lt;/em&gt; of &lt;em&gt;MicroAppServer&lt;/em&gt;. I think there is no need to mention about serve functionality since there is nothing special about it.&lt;/p&gt;

&lt;h4&gt;
  
  
  StitchingServer
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IubQnzfU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/930/1%2AYARt2ca7edLB675_QfkkGA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IubQnzfU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/930/1%2AYARt2ca7edLB675_QfkkGA.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;StitchingServer&lt;/em&gt; provides a &lt;em&gt;register&lt;/em&gt; endpoint for &lt;em&gt;MicroAppServers.&lt;/em&gt; When a &lt;em&gt;MicroAppServer&lt;/em&gt; registers itself to &lt;em&gt;StichingServer&lt;/em&gt;, &lt;em&gt;StichingServer&lt;/em&gt; records the declaration of the &lt;em&gt;MicroAppServer.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Later the &lt;em&gt;StitchingServer&lt;/em&gt; uses the declaration to resolve the &lt;em&gt;MicroAppServers&lt;/em&gt; from the requested URL.&lt;/p&gt;

&lt;p&gt;After resolving a &lt;em&gt;MicroAppServer&lt;/em&gt; and all of its dependencies, all relative paths in CSS, JS and HTML will be prefixed with related &lt;em&gt;MicroAppServer&lt;/em&gt; public URL. One additional step is prefixing the CSS selectors with a unique identifier of &lt;em&gt;MicroAppServer&lt;/em&gt; to prevent collision between micro-apps on client-side.&lt;/p&gt;

&lt;p&gt;Then the main responsibility of &lt;em&gt;StitchingServer&lt;/em&gt; comes into the scene: composing and returning a seamless HTML page from all collected parts.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Glimpse of Other Implementations out There
&lt;/h3&gt;

&lt;p&gt;Even before it was called micro frontends by 2016, lots of big companies were trying to solve similar problems such as Facebook with its &lt;a href="https://www.facebook.com/notes/facebook-engineering/bigpipe-pipelining-web-pages-for-high-performance/389414033919/"&gt;BigPipe&lt;/a&gt;. Nowadays the idea is gaining momentum. The companies with different sizes are interested in the subject and investing time and money on it. For instance, &lt;em&gt;Zalando&lt;/em&gt; open-sourced its solution which is called &lt;a href="https://www.mosaic9.org/"&gt;Project Mosaic&lt;/a&gt;. I can say that &lt;strong&gt;&lt;em&gt;microfe&lt;/em&gt;&lt;/strong&gt; and Project Mosaic are following similar approaches with some vital differences. While microfe embraces full decentralized route definitions to empower more independence for each micro-app, Project Mosaic prefers centralized route definition and layout definitions for each route. By that way, Project Mosaic allows easy A/B testing and dynamic Layout generation on the fly.&lt;/p&gt;

&lt;p&gt;There are some other approaches to the subject such as using iframes as stitching layer which is obviously not on the server-side but on the client-side. This is a very simple solution which does not require so much server structure and DevOps involvement. The job can be done by the frontend team only, so it creates a less organizational burden on the company and also it is less costly.&lt;/p&gt;

&lt;p&gt;There is a framework already out there called &lt;a href="https://single-spa.js.org/"&gt;&lt;em&gt;single-spa&lt;/em&gt;&lt;/a&gt;. The project relies on naming conventions of each app to resolve and load &lt;strong&gt;micro-apps&lt;/strong&gt;. Easy to grasp the idea and follow the patterns. So it can be a good initial introduction for experimenting the idea on your own local environment. But the downside of the project is you have to build each &lt;strong&gt;micro-app&lt;/strong&gt; in a specific way so they can play nice with the framework.&lt;/p&gt;

&lt;h3&gt;
  
  
  Final Thoughts
&lt;/h3&gt;

&lt;p&gt;I believe that micro frontends topic will be discussed more frequently in time. If the topic manages to get the attention of more and more companies, it will be the defacto way of development in large teams. It can be really beneficial in the close future for any frontend developer to grasp some insights and experience on this architecture.&lt;/p&gt;

&lt;h4&gt;
  
  
  Consider contributing
&lt;/h4&gt;

&lt;p&gt;I’m heavily experimenting on micro frontends with a noble goal in my mind: Creating a micro frontend framework which can solve the majority of problems without compromising of the performance and ease of development and testability. If you have any bright ideas to show, do not hesitate to visit my repositories, open an issue or reach me out via comments below or &lt;a href="https://twitter.com/onrzfr"&gt;Twitter DM&lt;/a&gt;. I will be there to help you! 🙂&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/onerzafer/micro-fe-registry"&gt;onerzafer/micro-fe-registry&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/onerzafer/micro-fe"&gt;onerzafer/micro-fe&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;





</description>
      <category>javascript</category>
      <category>microservices</category>
      <category>agiledevelopment</category>
      <category>frontend</category>
    </item>
    <item>
      <title>DIY Redux with RxJS: Part 3</title>
      <dc:creator>Öner Zafer</dc:creator>
      <pubDate>Tue, 15 Jan 2019 11:51:00 +0000</pubDate>
      <link>https://dev.to/onerzafer/diy-redux-with-rxjs-part-3-2ndb</link>
      <guid>https://dev.to/onerzafer/diy-redux-with-rxjs-part-3-2ndb</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tqdchS1j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2ArBO29FpCIE8eeZ_hhBhVSA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tqdchS1j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2ArBO29FpCIE8eeZ_hhBhVSA.png" alt=""&gt;&lt;/a&gt;Photo by &lt;a href="https://unsplash.com/photos/iVGevPcaJzk?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Steve Halama&lt;/a&gt; on &lt;a href="https://unsplash.com/search/photos/code?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;So far, in the previous two posts, I covered the topics “How to Create a Redux Library with RxJS” and “How to Write Redux Middlewares”. Before starting the 3rd and the last part of the series, I would recommend you to check the first 2 parts below:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/onerzafer/diy-redux-with-rxjs--rxdx-1oa1"&gt;DIY Redux with RxJS =&amp;gt; RxDx&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/onerzafer/diy-redux-with-rxjs-part-2-2lpg"&gt;DIY Redux with RxJS: Part 2&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this part, I will create a &lt;strong&gt;HOC&lt;/strong&gt; (Higher Order Component) to connect &lt;strong&gt;RxDx&lt;/strong&gt; with &lt;strong&gt;React.Component&lt;/strong&gt;. I assume that you already know about React.Component, so that I won’t be explaining it again. But I need to explain &lt;strong&gt;HOC&lt;/strong&gt; s which is the heart of what I will demonstrate below.&lt;/p&gt;

&lt;h3&gt;
  
  
  What the heck are these Higher-Order Components?
&lt;/h3&gt;

&lt;p&gt;Basically, HOC is a class &lt;strong&gt;decorator&lt;/strong&gt;. But what is a class decorator? Sometimes while we are coding we start feeling that all classes we create have the same basic properties. At that point, we realize that we are repeating ourselves which is completely against the &lt;strong&gt;DRY&lt;/strong&gt; rule. So we start trying to find a way to isolate the repeating parts. Or while we are coding we need to add a feature to our classes without changing the implementation of it. In both cases, our purpose is to achieve code sharing between classes. So the very best answer to our problem described above is kind of a &lt;strong&gt;class factory function&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Class factory functions are functions which returns a class. (welcome to the weirdness of the javascript world).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// the most primitive description of a class factory function
function **giveMeClass** (someArguments) {
   return class **SomeDynamicallyGeneratedClass** {}
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The primitive class factory function is not a class decorator yet. To be a class decorator, a class factory function requires a class as an argument and the returning/produced class will be an extended version of the input class:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//enhance decorator
function **ehance** (targetClass) {
   return class **EnhancedClass** extends **targetClass** {
      constructor() {
         this.isEnhanced = true;
      }
}

//usage in enviroment where experimental decorators allowed
@enhace
class **Normal** {}

console.log(new Normal().isEnhanced) // true

//usage in enviroment where experimental decorators not allowed
class **Normal** {}

const **Enhanced** = enhance(Normal);

console.log(new Enhanced().isEnhanced) // true
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;As it’s mentioned before, &lt;strong&gt;HOC&lt;/strong&gt; s are also decorators and works exactly the same way as a class decorator. A &lt;strong&gt;HOC&lt;/strong&gt; takes a &lt;strong&gt;React&lt;/strong&gt; component -which is a class- and returns another &lt;strong&gt;React&lt;/strong&gt; component. For detailed documentation of &lt;strong&gt;HOC&lt;/strong&gt; s you may follow the link below but if you are in the mood TL;DR I will continue with a summary of it:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://reactjs.org/docs/higher-order-components.html"&gt;Higher-Order Components - React&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now it’s time to extend the decorator example above to make it a &lt;strong&gt;HOC&lt;/strong&gt; by replacing class input and output with a &lt;strong&gt;React Component&lt;/strong&gt;. I have to mention that using React component as input/output gives us a couple of opportunities, but I will continue with recommended one which is wrapping the original component with another:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// a primitive HOC which makes A component great nothing :)
function **makeGreat** (WrappedComponent) {
   return class **EnhancedComponent** extends **React.Component** {
      constructor() {
         this.state = {isGreat: true}
      }

      render() {
         return &amp;lt; **WrappedComponent** {...this.props, ...this.state} /&amp;gt;
      }
   }
}

// usage
class **LameComponent** extends **React.Component** {
   render() {
      return &amp;lt;div&amp;gt;{this.props.isGreat? 'Great': 'Lame'}&amp;lt;/div&amp;gt;
   }
}

const **GreatComponent** = **makeGreat** (LameComponent);

&amp;lt; **LameComponent** /&amp;gt; // this will render &amp;lt;div&amp;gt;Lame&amp;lt;/div&amp;gt;
&amp;lt; **GreatComponent** /&amp;gt; // this will render &amp;lt;div&amp;gt;Great&amp;lt;/div&amp;gt;

// alternative usage if experimental decorators are allowed
@makeGreat
class **LameComponent** extends **React.Component** {
   render() {
      return &amp;lt;div&amp;gt;{this.props.isGreat? 'Great': 'Lame'}&amp;lt;/div&amp;gt;
   }
}

&amp;lt; **LameComponent** /&amp;gt; // this will render &amp;lt;div&amp;gt;Great&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;I believe we need to go deeper right now, so buckle up. At this point, you still don’t know about &lt;strong&gt;RxDx&lt;/strong&gt; you may check it from this &lt;a href="https://github.com/onerzafer/rxdx"&gt;link&lt;/a&gt; because I will explain how to connect &lt;strong&gt;RxDx&lt;/strong&gt; to &lt;strong&gt;React.Component&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Stitching the RxDx with a React Component: A real-life example of HOC
&lt;/h3&gt;

&lt;p&gt;Did you remember the &lt;strong&gt;selectors&lt;/strong&gt; from &lt;a href="https://dev.to/onerzafer/diy-redux-with-rxjs-part-2-2lpg"&gt;Part 2&lt;/a&gt; and &lt;strong&gt;store.select&lt;/strong&gt; function from &lt;a href="https://dev.to/onerzafer/diy-redux-with-rxjs--rxdx-1oa1"&gt;Part 1&lt;/a&gt;? This is the moment they will come in handy since we will use them as the cement between &lt;strong&gt;RxDx&lt;/strong&gt; and &lt;strong&gt;React.Component&lt;/strong&gt;. Store.select get a selector or a series of strings and return an observable to allow us to be notified of state updates. We can subscribe to all of these observables in &lt;strong&gt;componentDidMount&lt;/strong&gt; and on each update we can re-render the component by using &lt;strong&gt;forceUpdate&lt;/strong&gt; and to prevent memory leaks we have to unsubscribe from all subscriptions in &lt;strong&gt;componentWillUnmount:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class **SomeUglyComponent** extends **Component** {
**componentDidMount** () {
      this.subscription = store
          .select(someSelector)
          .subscribe((update) =&amp;gt; {
             this.setState({data: update});
             this.forceUpdate();
   }
**componentWillUnmount** () {
      this.subscription.unsubscribe();
   }
**render** () {
      return (&amp;lt;div&amp;gt;{ this.state.data }&amp;lt;/div&amp;gt;);
   }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Ok, I have to admit that this will work but &lt;strong&gt;SomeUglyComponent&lt;/strong&gt; is not that ugly as we have only one of it. Let’s imagine we have to implement such components tens of, hundreds of times. Now it seems uglier, right? A generalized solution for this repeating task is required and the &lt;strong&gt;SomeUglyComponent&lt;/strong&gt; should be as simple as possible so we can call it &lt;strong&gt;SimpleAndNiceComponent&lt;/strong&gt;. The desired &lt;strong&gt;SimpleAndNiceComponent&lt;/strong&gt; would be as follows:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const **SimpleAndNiceComponent** = **connect** ({
   data: store.select(someSlector)
})(({data}) =&amp;gt; &amp;lt;div&amp;gt; {data} &amp;lt;/div&amp;gt;);

//or
@ **connect** ({
   data: store.select(someSlector),
})
class **SimpleAndNiceComponent** extends **Component** {
**render** () {
      const {data} = this.props;
      return &amp;lt;div&amp;gt; {data} &amp;lt;/div&amp;gt;;
   }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;So how do we achieve this simplicity and how we wire observables to props? The answer is hidden inside the &lt;strong&gt;HOC&lt;/strong&gt; s. We need one step further than simple &lt;strong&gt;HOC&lt;/strong&gt; , what we need is a &lt;strong&gt;HOC&lt;/strong&gt; factory. I will implement a &lt;strong&gt;connect&lt;/strong&gt; function which takes observables and returns a &lt;strong&gt;HOC&lt;/strong&gt; which takes a &lt;strong&gt;React.Component&lt;/strong&gt; and returns an enhanced/wrapped &lt;strong&gt;React.Component:&lt;/strong&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;For the sake of clarity, the example above is simplified. For sure, if we need to use it for production, we have to think more about &lt;em&gt;edge usage cases&lt;/em&gt; and &lt;em&gt;debuggability&lt;/em&gt; and also &lt;em&gt;error prevention and recovery&lt;/em&gt; strategies. Even though it’s a simplification this code piece will do the trick and will allow us to write simpler components with a generalized connect mechanism with async nature of observables and sync nature of &lt;strong&gt;React.Component&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Final, final, final words
&lt;/h3&gt;

&lt;p&gt;Yes, finally we have all in one &lt;strong&gt;RxJS&lt;/strong&gt; powered &lt;strong&gt;Redux&lt;/strong&gt; equivalent library. All I can say that I learned a lot working on this experiment and I wanted to share it hoping that my journey will help others. Next steps will be using it, growing it and loving it!&lt;/p&gt;

&lt;p&gt;If you managed to read up to this point and bared with me through this experiment, I will be extremely glad. &lt;strong&gt;A big thanks to you&lt;/strong&gt;! Please don’t forget to &lt;strong&gt;clap&lt;/strong&gt; or drop your &lt;strong&gt;comment below&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you want to contribute to my experiment and help me to make it a real library which can possibly solve real-life problems, don’t hesitate to click the link below and contribute. You will be only welcomed!&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vJ70wriM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/github-logo-ba8488d21cd8ee1fee097b8410db9deaa41d0ca30b004c0c63de0a479114156f.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/onerzafer"&gt;
        onerzafer
      &lt;/a&gt; / &lt;a href="https://github.com/onerzafer/rxdx"&gt;
        rxdx
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      redux like library for react based on rxjs
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;h1&gt;
RxDx&lt;/h1&gt;
&lt;p&gt;RxDx is a Rxjs base Redux like state management library and it icludes all necessary tooling inside.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;heaviliy inspired from great libraries &lt;a href="https://github.com/reduxjs/redux"&gt;redux&lt;/a&gt;, &lt;a href="https://github.com/reduxjs/react-redux"&gt;react-redux&lt;/a&gt;, &lt;a href="https://github.com/redux-observable/redux-observable"&gt;redux-observable&lt;/a&gt; and &lt;a href="https://github.com/ngrx/platform"&gt;ngrx&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
Motivation&lt;/h2&gt;
&lt;p&gt;Basic idea behind this library  is bringin the full power of RxJS into react world with a familiar interface of Redux withiot relying on some middleware jungle of libraries.&lt;/p&gt;
&lt;h2&gt;
Installation&lt;/h2&gt;
&lt;div class="highlight highlight-source-shell"&gt;&lt;pre&gt;npm install rxdx --save&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;div class="highlight highlight-source-shell"&gt;&lt;pre&gt;yarn add rxdx&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;
How to Use&lt;/h2&gt;
&lt;h3&gt;
Store&lt;/h3&gt;
&lt;p&gt;Unlike react-redux RxDx does not rely on context so no need to introduce Provider on App.js. But on the contrary it requires an exported Store to function. In these sample codes it is suggested to have a saperate store.js for initializing the Store and improting it when neccessary. Intentionally the interface of createStore is just like react-redux interface.&lt;/p&gt;
&lt;div class="highlight highlight-source-js"&gt;
&lt;pre&gt;&lt;span class="pl-c"&gt;//store.js&lt;/span&gt;
&lt;span class="pl-k"&gt;import&lt;/span&gt; &lt;span class="pl-kos"&gt;{&lt;/span&gt;
  &lt;span class="pl-s1"&gt;createStore&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt;
  &lt;span class="pl-s1"&gt;applyMiddleware&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt;
  &lt;span class="pl-s1"&gt;combineReducers&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt;
  &lt;span class="pl-s1"&gt;devToolsMiddleware&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt;
  &lt;span class="pl-s1"&gt;effectsMiddleware&lt;/span&gt;
&lt;span class="pl-kos"&gt;}&lt;/span&gt; &lt;span class="pl-k"&gt;from&lt;/span&gt; &lt;span class="pl-s"&gt;"rxdx/lib/rxdx"&lt;/span&gt;&lt;span class="pl-kos"&gt;;&lt;/span&gt;
&lt;span class="pl-k"&gt;import&lt;/span&gt; &lt;span class="pl-kos"&gt;{&lt;/span&gt; &lt;span class="pl-s1"&gt;todoEffects&lt;/span&gt;&lt;/pre&gt;…&lt;/div&gt;
&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/onerzafer/rxdx"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;





</description>
      <category>redux</category>
      <category>react</category>
      <category>rxjs</category>
      <category>javascript</category>
    </item>
    <item>
      <title>DIY Redux with RxJS: Part 2</title>
      <dc:creator>Öner Zafer</dc:creator>
      <pubDate>Mon, 07 Jan 2019 06:31:59 +0000</pubDate>
      <link>https://dev.to/onerzafer/diy-redux-with-rxjs-part-2-2lpg</link>
      <guid>https://dev.to/onerzafer/diy-redux-with-rxjs-part-2-2lpg</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tqdchS1j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2ArBO29FpCIE8eeZ_hhBhVSA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tqdchS1j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2ArBO29FpCIE8eeZ_hhBhVSA.png" alt=""&gt;&lt;/a&gt;Photo by &lt;a href="https://unsplash.com/photos/iVGevPcaJzk?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Steve Halama&lt;/a&gt; on &lt;a href="https://unsplash.com/search/photos/code?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In case you didn’t read the part one I would recommend you to read it. The first part is covering how to implement the core functionalities of Redux.&lt;/em&gt;&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/onerzafer" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--azPc8Nm_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--S2F2BDi_--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/129448/1e396898-6d63-4cb0-b033-76ffbdd1630f.jpeg" alt="onerzafer image"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/onerzafer/diy-redux-with-rxjs--rxdx-1oa1" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;DIY Redux with RxJS = RxDx&lt;/h2&gt;
      &lt;h3&gt;Öner Zafer ・ Jan 17 '19 ・ 5 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#redux&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#rxjs&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#react&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;At DIY Redux with RxJS =&amp;gt; RxDx Part 2, I will reveal the internals of some most popular and widely used &lt;strong&gt;Redux&lt;/strong&gt; middlewares. As I mentioned in the first part of the article -which you can find above-, my goal is to do an experiment to combine RxJS and all widely used Redux middlewares into a library which can be found below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vJ70wriM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/github-logo-ba8488d21cd8ee1fee097b8410db9deaa41d0ca30b004c0c63de0a479114156f.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/onerzafer"&gt;
        onerzafer
      &lt;/a&gt; / &lt;a href="https://github.com/onerzafer/rxdx"&gt;
        rxdx
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      redux like library for react based on rxjs
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;h1&gt;
RxDx&lt;/h1&gt;
&lt;p&gt;RxDx is a Rxjs base Redux like state management library and it icludes all necessary tooling inside.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;heaviliy inspired from great libraries &lt;a href="https://github.com/reduxjs/redux"&gt;redux&lt;/a&gt;, &lt;a href="https://github.com/reduxjs/react-redux"&gt;react-redux&lt;/a&gt;, &lt;a href="https://github.com/redux-observable/redux-observable"&gt;redux-observable&lt;/a&gt; and &lt;a href="https://github.com/ngrx/platform"&gt;ngrx&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
Motivation&lt;/h2&gt;
&lt;p&gt;Basic idea behind this library  is bringin the full power of RxJS into react world with a familiar interface of Redux withiot relying on some middleware jungle of libraries.&lt;/p&gt;
&lt;h2&gt;
Installation&lt;/h2&gt;
&lt;div class="highlight highlight-source-shell"&gt;&lt;pre&gt;npm install rxdx --save&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;div class="highlight highlight-source-shell"&gt;&lt;pre&gt;yarn add rxdx&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;
How to Use&lt;/h2&gt;
&lt;h3&gt;
Store&lt;/h3&gt;
&lt;p&gt;Unlike react-redux RxDx does not rely on context so no need to introduce Provider on App.js. But on the contrary it requires an exported Store to function. In these sample codes it is suggested to have a saperate store.js for initializing the Store and improting it when neccessary. Intentionally the interface of createStore is just like react-redux interface.&lt;/p&gt;
&lt;div class="highlight highlight-source-js"&gt;
&lt;pre&gt;&lt;span class="pl-c"&gt;//store.js&lt;/span&gt;
&lt;span class="pl-k"&gt;import&lt;/span&gt; &lt;span class="pl-kos"&gt;{&lt;/span&gt;
  &lt;span class="pl-s1"&gt;createStore&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt;
  &lt;span class="pl-s1"&gt;applyMiddleware&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt;
  &lt;span class="pl-s1"&gt;combineReducers&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt;
  &lt;span class="pl-s1"&gt;devToolsMiddleware&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt;
  &lt;span class="pl-s1"&gt;effectsMiddleware&lt;/span&gt;
&lt;span class="pl-kos"&gt;}&lt;/span&gt; &lt;span class="pl-k"&gt;from&lt;/span&gt; &lt;span class="pl-s"&gt;"rxdx/lib/rxdx"&lt;/span&gt;&lt;span class="pl-kos"&gt;;&lt;/span&gt;
&lt;span class="pl-k"&gt;import&lt;/span&gt; &lt;span class="pl-kos"&gt;{&lt;/span&gt; &lt;span class="pl-s1"&gt;todoEffects&lt;/span&gt;&lt;/pre&gt;…&lt;/div&gt;
&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/onerzafer/rxdx"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Before diving into the subject, it is always good to review the required signature of redux middlewares.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(storeAPI) =&amp;gt; (next) =&amp;gt; (action) =&amp;gt; state
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;em&gt;Before I start, I need to confess, when I finished the createStore function I thought with bold confidence that&lt;/em&gt; &lt;strong&gt;&lt;em&gt;all available middlewares&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;of redux will work for my version of it. Actually, it worked but not for most of them, not for widely used ones :( So I had to provide these libraries/middlewares by re-implementing them or at least implementing similar ones. Here, we go!&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  A different approach to redux-observable
&lt;/h3&gt;

&lt;p&gt;Under normal conditions, &lt;strong&gt;Redux&lt;/strong&gt; does not implement/has no intention to implement &lt;a href="https://github.com/tc39/proposal-observable"&gt;TC39 Observable&lt;/a&gt; therefore it is not compatible with RxJS by default.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Here I have to mention the use cases of observables; if you need to operate on streams of events, then it is probably better to use an observable. What kind of operations am I talking on? Merging two streams into one, filtering, mapping or redirecting a stream. For more technical detail check &lt;em&gt;[_here&lt;/em&gt;](&lt;a href="https://rxjs-dev.firebaseapp.com/guide/observable"&gt;https://rxjs-dev.firebaseapp.com/guide/observable&lt;/a&gt;)&lt;/em&gt;._&lt;/p&gt;

&lt;p&gt;When implementing a &lt;strong&gt;Redux&lt;/strong&gt; store you will definitely notice that you have to create lots of actions. I have to admit that actions are pretty similar to events and on the runtime, they will be triggered hundreds of times. So this must be the point where &lt;strong&gt;redux-observable&lt;/strong&gt; catches &lt;strong&gt;Redux&lt;/strong&gt; as a streaming event source. By doing so, it creates a good opportunity for developers who are willing to create some asynchronous side effects on each selected action. For detailed documentation on &lt;strong&gt;redux-observable&lt;/strong&gt; , you may check &lt;a href="https://redux-observable.js.org/"&gt;here&lt;/a&gt;. TL;DR The magic happens in a function called &lt;strong&gt;createEppicsMiddleware&lt;/strong&gt; that has the signature below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const createEpicsMiddleware = (rootEpic) =&amp;gt; (storeAPI) =&amp;gt; (next) =&amp;gt; (action) =&amp;gt; state
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;To be honest, when I checked the documentation, I didn’t like the way we need to consume this library. For me, the usage of &lt;a href="https://ngrx.io/guide/effects"&gt;&lt;strong&gt;Effect&lt;/strong&gt;&lt;/a&gt; from NgRx is more classy :) So I decided not to mimic the API of redux-observable, yet the functionality should be there. So I ended up something as follows.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;As I mentioned before I really like NgRx style usage and I borrowed &lt;strong&gt;Effect&lt;/strong&gt; decorator and &lt;strong&gt;ofType&lt;/strong&gt; tappable function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Effect&lt;/strong&gt; decorator is as simple as adding a type on the original variable. And if your environment supports experimental decorators the usage can be as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Effect()
const someCoolAsyncSideEffect = action$
   .pipe(ofType('SomeCoolAction', 'OrEvenCoolerAction'))
   .pipe(map(() =&amp;gt; {type: 'SomeOtherCoolAction'}));
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Unfortunately, if your environment does not support experimental decorators this code piece won’t look that much handsome.&lt;/p&gt;
&lt;h3&gt;
  
  
  Reselect
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;While I was implementing the createStore function in the&lt;/em&gt; &lt;a href="https://dev.to/onerzafer/diy-redux-with-rxjs--rxdx-1oa1"&gt;&lt;em&gt;first part&lt;/em&gt;&lt;/a&gt; &lt;em&gt;of this story, I implemented a select function in the exposed store object by trusting my gut feeling. This decision revealed itself as pretty useful in implementing the&lt;/em&gt; &lt;strong&gt;&lt;em&gt;reselect&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;library of&lt;/em&gt; &lt;strong&gt;&lt;em&gt;RxDx&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;. To be frank,&lt;/em&gt; &lt;strong&gt;&lt;em&gt;reselect&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;is completely compatible with&lt;/em&gt; &lt;strong&gt;&lt;em&gt;RxDx&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;, yet I was experimenting and I needed to challenge myself to combine all libraries into it&lt;/em&gt; &lt;strong&gt;&lt;em&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What does &lt;strong&gt;reselect&lt;/strong&gt; do? It takes the state or a state portion and returns a portion of it. So what’s the deal? We can achieve the same functionality without it like the example below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const reselect = (state, key) =&amp;gt; state[key];
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The example above totally undermines the power of this simple yet incredibly useful library. The deal is, the &lt;strong&gt;reselect&lt;/strong&gt; provides a simple repeatable function which has &lt;strong&gt;memoization&lt;/strong&gt; inside to speed up our -possibly complex- filtering on states.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;This function will have a key functionality while we are consuming the &lt;strong&gt;RxDx&lt;/strong&gt; store in react components. The usage of a selector as simple as follows:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Redux Dev Tools Middleware
&lt;/h3&gt;

&lt;p&gt;I thought that the absence of the integration to redux dev tools would be a big missing part of &lt;strong&gt;RxDx&lt;/strong&gt;. Then I started reading the API documentation of dev tools and I found what I need __REDUX_DEVTOOLS_EXTENSION__.connect&lt;/p&gt;

&lt;p&gt;After this point, it was super easy to combine everything into a middleware:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Finally, I reached a point which I can create a store, add some reducers, apply built-in optional middlewares and select a specific state child with memoization:&lt;/strong&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Final words until next time
&lt;/h3&gt;

&lt;p&gt;I strongly recommend such experiments for anybody who wants to learn more about any library or any kind of design patterns or coding styles. It will cause you to question lots of things you do during normal daily coding tasks of yours.&lt;/p&gt;

&lt;p&gt;Next time I will continue my experiment with how to combine a redux-like-library with react components and it will include lots of information about &lt;strong&gt;HOCs&lt;/strong&gt; and &lt;strong&gt;decorators&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you are also a self-learner like me and a little bit impatient on top of it, let’s just try it yourself until I release the 3rd and the last part of the RxDx article.&lt;/p&gt;

&lt;p&gt;Here is my repo to start your own experiment:&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vJ70wriM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/github-logo-ba8488d21cd8ee1fee097b8410db9deaa41d0ca30b004c0c63de0a479114156f.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/onerzafer"&gt;
        onerzafer
      &lt;/a&gt; / &lt;a href="https://github.com/onerzafer/rxdx"&gt;
        rxdx
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      redux like library for react based on rxjs
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;h1&gt;
RxDx&lt;/h1&gt;
&lt;p&gt;RxDx is a Rxjs base Redux like state management library and it icludes all necessary tooling inside.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;heaviliy inspired from great libraries &lt;a href="https://github.com/reduxjs/redux"&gt;redux&lt;/a&gt;, &lt;a href="https://github.com/reduxjs/react-redux"&gt;react-redux&lt;/a&gt;, &lt;a href="https://github.com/redux-observable/redux-observable"&gt;redux-observable&lt;/a&gt; and &lt;a href="https://github.com/ngrx/platform"&gt;ngrx&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
Motivation&lt;/h2&gt;
&lt;p&gt;Basic idea behind this library  is bringin the full power of RxJS into react world with a familiar interface of Redux withiot relying on some middleware jungle of libraries.&lt;/p&gt;
&lt;h2&gt;
Installation&lt;/h2&gt;
&lt;div class="highlight highlight-source-shell"&gt;&lt;pre&gt;npm install rxdx --save&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;div class="highlight highlight-source-shell"&gt;&lt;pre&gt;yarn add rxdx&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;
How to Use&lt;/h2&gt;
&lt;h3&gt;
Store&lt;/h3&gt;
&lt;p&gt;Unlike react-redux RxDx does not rely on context so no need to introduce Provider on App.js. But on the contrary it requires an exported Store to function. In these sample codes it is suggested to have a saperate store.js for initializing the Store and improting it when neccessary. Intentionally the interface of createStore is just like react-redux interface.&lt;/p&gt;
&lt;div class="highlight highlight-source-js"&gt;
&lt;pre&gt;&lt;span class="pl-c"&gt;//store.js&lt;/span&gt;
&lt;span class="pl-k"&gt;import&lt;/span&gt; &lt;span class="pl-kos"&gt;{&lt;/span&gt;
  &lt;span class="pl-s1"&gt;createStore&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt;
  &lt;span class="pl-s1"&gt;applyMiddleware&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt;
  &lt;span class="pl-s1"&gt;combineReducers&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt;
  &lt;span class="pl-s1"&gt;devToolsMiddleware&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt;
  &lt;span class="pl-s1"&gt;effectsMiddleware&lt;/span&gt;
&lt;span class="pl-kos"&gt;}&lt;/span&gt; &lt;span class="pl-k"&gt;from&lt;/span&gt; &lt;span class="pl-s"&gt;"rxdx/lib/rxdx"&lt;/span&gt;&lt;span class="pl-kos"&gt;;&lt;/span&gt;
&lt;span class="pl-k"&gt;import&lt;/span&gt; &lt;span class="pl-kos"&gt;{&lt;/span&gt; &lt;span class="pl-s1"&gt;todoEffects&lt;/span&gt;&lt;/pre&gt;…&lt;/div&gt;
&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/onerzafer/rxdx"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;If you are interested in RxDx and enjoyed the article so far, please don’t forget show your appreciation. If you share your ideas and feedback below, I would only be glad!&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This post originally published on hanckernoon.com. On dev.to it is slightly adapted to platform&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://hackernoon.com/diy-redux-with-rxjs-part-3-87eaf23d4092"&gt;DIY Redux with RxJS: Part 3 on hackernoon&lt;/a&gt;&lt;/p&gt;




</description>
      <category>redux</category>
      <category>rxjs</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>DIY Redux with RxJS = RxDx</title>
      <dc:creator>Öner Zafer</dc:creator>
      <pubDate>Sat, 29 Dec 2018 13:14:46 +0000</pubDate>
      <link>https://dev.to/onerzafer/diy-redux-with-rxjs--rxdx-1oa1</link>
      <guid>https://dev.to/onerzafer/diy-redux-with-rxjs--rxdx-1oa1</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--q_fDQX38--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2Advz733Mj3DVx2ytegmBW-g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--q_fDQX38--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2Advz733Mj3DVx2ytegmBW-g.png" alt=""&gt;&lt;/a&gt;Photo by &lt;a href="https://unsplash.com/photos/iVGevPcaJzk?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Steve Halama&lt;/a&gt; on &lt;a href="https://unsplash.com/search/photos/code?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;State management is a must if you are developing a huge app, and using proper state management can be a real lifesaver. On React, Redux is our lifeguard, and on Angular, we have NgRx for this role.&lt;/p&gt;

&lt;p&gt;I think, on the architectural level both have similar boilerplates and if you grasp one already, your learning curve for the other will be smoother. What I really like on NgRx is not the library itself but RxJS, which is really the most amazing library for handling the streaming events.&lt;/p&gt;

&lt;p&gt;While I was investigating on Redux and NgRx, I realized that there are hardly any people who use Redux by itself without lots of helper libraries, but on the other hand the case is not similar for NgRx as it provides all in one solution in most of the cases. &lt;strong&gt;So I decided to make an experiment to combine RxJS and all Redux helper libraries for creating a super duper state management library for React with similar fingerprints like Redux.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;At this point, the recipe was obvious: Firstly, I reimplemented Redux with RxJS and added some middleware salt on top of it and it’s ready to serve! So with this post, I will try to explain how I did it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hint: I released the alpha version of the resulting library already and you can check it ** &lt;a href="https://github.com/onerzafer/rxdx"&gt;&lt;strong&gt;here&lt;/strong&gt;&lt;/a&gt; **.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 0: Deconstruct Redux and Related Libraries
&lt;/h3&gt;

&lt;p&gt;Redux provides mainly a &lt;strong&gt;createStore&lt;/strong&gt; function which obviously creates a store. This createStore takes a &lt;strong&gt;reducer,&lt;/strong&gt;  &lt;strong&gt;initial state,&lt;/strong&gt; and an &lt;strong&gt;enhancer&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reducer&lt;/strong&gt; is a function which takes a state and returns a state. Quite simple, right? But wait a minute, I will need more than one reducer, which means I will need a helper function to combine all reducers into one big fat reducer.&lt;/p&gt;

&lt;p&gt;For a similar purpose, Redux has &lt;strong&gt;combineReducers&lt;/strong&gt; function which is very handy. An &lt;strong&gt;initial state&lt;/strong&gt; is an object which may be an empty or undefined state at all. Even simpler! The &lt;strong&gt;enhancer&lt;/strong&gt; is a little bit weird which has the following signature:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(createStore) =&amp;gt; (reducer, initialState) =&amp;gt; store
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Again I will need more than one enhancer and again I will need a helper function to combine all enhancers into one which is called &lt;strong&gt;applyMiddleware.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When we run createStore function it returns an object similar to the simplified object below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
   getState: () =&amp;gt; state,
   subscribe: () =&amp;gt; unsubscribe,
   dispatch: (action) =&amp;gt; void
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;getState&lt;/strong&gt; returns the current state. &lt;strong&gt;Subscribe&lt;/strong&gt; returns an &lt;strong&gt;unsubscribe&lt;/strong&gt; object but if I’m planning to use RxJS I can use its own subscription and unsubscription. As a result, I will have a simpler implementation. &lt;strong&gt;Dispatch&lt;/strong&gt; functions actually pass the action to the reducer. And again RxJS will become handy here which has its own dispatching mechanism.&lt;/p&gt;

&lt;p&gt;So, I have created a TODO list to achieve my goal:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Implement &lt;code&gt;createStore(reducer, initialState, enhancer) =&amp;gt; store&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Implement &lt;code&gt;combineReducer(structuredReducersObject) =&amp;gt; reducer&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Implement &lt;code&gt;applyMiddleware(createStore) =&amp;gt; (reducer, initialState) =&amp;gt; store&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s start!&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 1: createStore with the help of RxJS BehaviourSubject and operators
&lt;/h3&gt;

&lt;p&gt;At any time a &lt;strong&gt;store&lt;/strong&gt; consumer calls &lt;strong&gt;getState,&lt;/strong&gt; store should return the current state back. If I use &lt;strong&gt;BehaviourSubject&lt;/strong&gt; as state source the implementation of internal store state will be easier since similar behavior from RxJS observables belongs to &lt;strong&gt;BehaviourSubject&lt;/strong&gt; by calling &lt;strong&gt;BehaviourSubject.value&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is how it’s done:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;I will rephrase what I did here in HUMAN words. In the &lt;strong&gt;createStore&lt;/strong&gt; function, I created a &lt;strong&gt;Store&lt;/strong&gt; with given &lt;strong&gt;reducer&lt;/strong&gt; and &lt;strong&gt;initialState&lt;/strong&gt;. If there is an &lt;strong&gt;enhancer&lt;/strong&gt; I pass a fake &lt;strong&gt;createStore&lt;/strong&gt; function which will return already created &lt;strong&gt;Store&lt;/strong&gt; , as a result, it will return enhanced &lt;strong&gt;Store&lt;/strong&gt;. Then I dispatch the first action and return the created or enhanced store. So when the &lt;strong&gt;createStore&lt;/strong&gt; function is called, we can use the &lt;strong&gt;store reference&lt;/strong&gt; in our app. Maybe this reference can be passed to some react context provider.&lt;/p&gt;

&lt;p&gt;As you can see, the most complicated part is the &lt;strong&gt;select&lt;/strong&gt; function and it is not a must-have feature at all. I added this part to have an interface such as &lt;strong&gt;reselect&lt;/strong&gt;. As I can understand so far, the &lt;strong&gt;subscribe&lt;/strong&gt; function of Redux is not a widely used feature of it but react-redux library uses it to get the updates and rerender the component (if anybody uses it directly please comment it below, because in my experiment I couldn't find any use case for my subscribe function).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So, the createStore function so far should be able to work for one reducer and one enhancer, which means the backbone of my RxJS based Redux library is ready to be named: From now on I will call it RxJs + Redux = RxDx.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: How to create a combineReducers function?
&lt;/h3&gt;

&lt;p&gt;When I checked the &lt;strong&gt;combineReducers&lt;/strong&gt; source code from Redux, I realized that it takes an object (which can be nested) of which the keys have a reducer and as a result returns a bigger reducer.&lt;/p&gt;

&lt;p&gt;Let start with the following initial code:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;For now, I will avoid error prevention and error handling (I will trust whoever uses this function). The idea here is to loop over the keys of reducers object and to call the attached reducer with related state part and the action.&lt;/p&gt;

&lt;p&gt;As a result, all reducers will update only the related part of the state and I will combine the results in the same structure and return them. While I was trying to code this function I noticed the name of the function has the solution itself: &lt;strong&gt;Array.reduce.&lt;/strong&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Step 3: The Magical applyMiddleware function
&lt;/h3&gt;

&lt;p&gt;If you take a closer look into the Redux implementation, the biggest magic happens with only one function named &lt;strong&gt;compose&lt;/strong&gt;. What does the compose function do? Actually, it is pretty simple as it takes a list of functions, wraps each of them by passing the second wrapped one to the first, the third to the second and so on, and finally returns this weird nested function:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can see above, the functions a,b,c,d,e becomes A(B(C(D(E(…args))))). This behavior is crucial because all enhancers should be able to be called on each action and all of them should be able to alter the action or dispatch a new one or even delay the original one. There is one more restriction to enhancers as they should follow the signature below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// sample enhancer signature
const enhancer =&amp;gt; (createStore) =&amp;gt; (next) =&amp;gt; (action) =&amp;gt; state
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Finally, I ended up with the following function by omitting the error handling and error prevention parts:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h3&gt;
  
  
  Final Words
&lt;/h3&gt;

&lt;p&gt;Actually, RxDx currently includes lost of other functionalities and if you are willing to check them out and all the example usages, you may check the &lt;a href="https://github.com/onerzafer/rxdx"&gt;repo&lt;/a&gt; and &lt;a href="https://github.com/onerzafer/rxdx-sample-app"&gt;sample todo app repo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This is the core part of RxDx and I am planning to explain it further how to connect the RxDx to React components with a follow-up article to this one.&lt;/p&gt;

&lt;p&gt;With the follow-up article, I am hoping to shed some light into the inner structure of react-redux and then I will move forward to react-observable, then reselect libraries.&lt;/p&gt;

&lt;p&gt;If you are interested in RxDx, and enjoyed the article so far, please don’t forget show your appreciation. If you share your ideas and feedback below, I would only be glad! Thanks for reading.&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/onerzafer" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--azPc8Nm_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--S2F2BDi_--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/129448/1e396898-6d63-4cb0-b033-76ffbdd1630f.jpeg" alt="onerzafer image"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/onerzafer/diy-redux-with-rxjs-part-2-2lpg" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;DIY Redux with RxJS: Part 2&lt;/h2&gt;
      &lt;h3&gt;Öner Zafer ・ Jan 17 '19 ・ 5 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#redux&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#rxjs&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#react&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;&lt;em&gt;This article is published at hackernoon.com. This version is slightly adapted to dev.to&lt;/em&gt;&lt;br&gt;
&lt;a href="https://hackernoon.com/diy-redux-with-rxjs-part-2-f9d4c53fa230"&gt;DIY Redux with RxJS: Part 2 on Hackernoon&lt;/a&gt;&lt;/p&gt;




</description>
      <category>redux</category>
      <category>javascript</category>
      <category>rxjs</category>
      <category>react</category>
    </item>
  </channel>
</rss>
