<?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: valentin</title>
    <description>The latest articles on DEV Community by valentin (@lifaon74).</description>
    <link>https://dev.to/lifaon74</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%2F700123%2Ff243461f-f4be-4951-b045-f0ca2326c47b.jpeg</url>
      <title>DEV Community: valentin</title>
      <link>https://dev.to/lifaon74</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lifaon74"/>
    <language>en</language>
    <item>
      <title>May I humbly submit you my front-end framework ?</title>
      <dc:creator>valentin</dc:creator>
      <pubDate>Fri, 08 Mar 2024 07:31:12 +0000</pubDate>
      <link>https://dev.to/lifaon74/may-i-humbly-submit-you-my-front-end-framework--26ek</link>
      <guid>https://dev.to/lifaon74/may-i-humbly-submit-you-my-front-end-framework--26ek</guid>
      <description>&lt;p&gt;Hi everyone, I'll present you what I consider my best personal achievement so far: &lt;a href="https://dom.lirx.org/docs/documentation/getting-started/introduction/"&gt;a framework purely based on Reactive Programming&lt;/a&gt; (ak: Observables and Signals) to create great web applications.&lt;/p&gt;

&lt;p&gt;I did it because I was seeing the advantages and limits in React, Angular, and Svelte.&lt;/p&gt;

&lt;p&gt;So I wanted to create something taking the best of all: a performant framework, built with optimizations in mind, constrained component structure, and based on &lt;a href="https://en.wikipedia.org/wiki/Reactive_programming"&gt;reactive programing&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Maybe it's a genius idea, maybe it's not, but in any case, if it may inspire people, I'll be happy with that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction: what we really want ?
&lt;/h2&gt;

&lt;p&gt;First let's define what makes a great framework, and what we want as developers and companies.&lt;/p&gt;

&lt;h3&gt;
  
  
  Developers wants reactivity
&lt;/h3&gt;

&lt;p&gt;In front-end, our interfaces &lt;strong&gt;react&lt;/strong&gt; to changes: an event appends (like a click, a promise resoling, a websocket packet, a timer, etc...), and we have to update our interface (meaning update the DOM) reflecting the new data. So everything in front-end is fundamentally reactive.&lt;/p&gt;

&lt;p&gt;If we compare some frameworks:&lt;/p&gt;

&lt;p&gt;Angular inclues &lt;code&gt;rxjs&lt;/code&gt; but was not built with reactivity in mind. However, we may see a clear requirement from the developers: it has an &lt;code&gt;async&lt;/code&gt; pipe, recently &lt;code&gt;signal&lt;/code&gt; were introduced, and &lt;code&gt;Stores&lt;/code&gt; are common. Sadly, the framework itself is not optimized at all for reactivity. It was not built for it, and checking the source code highlight these limitations.&lt;/p&gt;

&lt;p&gt;React has &lt;code&gt;useState&lt;/code&gt; and &lt;code&gt;useEffect&lt;/code&gt; (with others) which are essentially &lt;em&gt;reactive&lt;/em&gt; variables and context. Now, we may consider it as a &lt;em&gt;reactive framework&lt;/em&gt;, however it was not initially though for it, and we must use tricks like memoization using &lt;code&gt;useCallback&lt;/code&gt; or &lt;code&gt;React.memo&lt;/code&gt; for example.&lt;/p&gt;

&lt;p&gt;Finally, Svelte is natively based on reactivity, which provides excellent performances.&lt;/p&gt;

&lt;h3&gt;
  
  
  Developers wants well-structured projects
&lt;/h3&gt;

&lt;p&gt;One of the best argument in favor of Angular is how it enforces a specific structure for the projects. When dealing with very large applications, it becomes very important to have a structured architecture having well organized files, directories and components. Something that the recommandations, best practices and &lt;code&gt;cli&lt;/code&gt; offers: an opinionated way to structure the apps, that works very well.&lt;/p&gt;

&lt;p&gt;This is in my opinion where React and Svelte struggle: they don't offer an &lt;code&gt;unique&lt;/code&gt; way of structuring the projects nor coding the components.&lt;/p&gt;

&lt;p&gt;It's up to us to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;create our own structure. &lt;/li&gt;
&lt;li&gt;split, the css, html and js. &lt;/li&gt;
&lt;li&gt;select what kind of css-or-js we want.&lt;/li&gt;
&lt;li&gt;choose the front-end tooling we want (nextjs, gatsby, etc...).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This flexibility is fantastic for small projects, but becomes a nightmare in very large applications: for X developers, it results in X ways of coding the components, X ways to organize them, X different ways to import css, etc. Something that becomes quickly impractical, and very hard to maintain. If you're working in big tech companies, you know the struggle.&lt;/p&gt;

&lt;p&gt;With big projects, we'll need to have very strict guidelines and documentation to organize projects, or it will become cumbersome and a horror to works on.&lt;/p&gt;

&lt;h3&gt;
  
  
  Companies wants stability
&lt;/h3&gt;

&lt;p&gt;A company frequently works with code that will probably last forever. We all have an example of a legacy code that nobody want to touch but is critical for the business (ex: a login page), or very large projects that are build in years. So having something that can resist time is important. This means: no-breaking changes, evolution that keeps retro-compatibility and avoid changing everything in the framework.&lt;/p&gt;

&lt;p&gt;This is something that is hard to stick to over time: new ideas often tend to radically change how we use a framework.&lt;/p&gt;

&lt;p&gt;React changed to &lt;em&gt;hooks&lt;/em&gt;: it was a revolution, but the "old way" kept working which is great for long term projects. Angular reworked entirely its engine, and it was mostly transparent for the developers. In my opinion, this is currently the most stable of all the frameworks, making it a very good choice for large companies.&lt;/p&gt;

&lt;p&gt;So stability is a critical point for any giant business.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Sadly, this is something I cannot provide yet, but somewhere in the future, with a bigger community, why not ?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Presentation of my framework
&lt;/h2&gt;

&lt;p&gt;I've named my framework &lt;code&gt;LiRX&lt;/code&gt; which is an abbreviation for &lt;code&gt;Light Reactive X&lt;/code&gt;. It is build on the following premises, based on the based on the previous observations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;reactivity as first-class citizen&lt;/strong&gt;: the components are compiled to pure javascript to increase drastically the performances, and the DOM is rendered from Observable changes 
with very fined grained and optimized pipelines. Only what changes is rendered and the minimal javascript execution is done each time.&lt;/li&gt;
&lt;li&gt;the framework follows an &lt;strong&gt;MVC structure&lt;/strong&gt;: each component has an &lt;code&gt;.html&lt;/code&gt;, &lt;code&gt;.scss&lt;/code&gt; and &lt;code&gt;.ts&lt;/code&gt; file. Separating these concerns is very important in large applications, in order to keep a coherent structure.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;performance&lt;/strong&gt; always in mind: everything is optimized in such a way that any change in the application triggers the minimal javascript execution time and path as possible. Moreover, it was build to use the full potential of minifiers (efficient tree-shaking, unroll when possible, pre-compile if possible).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It comes in different modules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://core.lirx.org/"&gt;@lirx/core&lt;/a&gt;: this is the library used to build Observables, Signals, and pipe them.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://dom.lirx.org/"&gt;@lirx/dom&lt;/a&gt;: this is the library used to &lt;strong&gt;build web applications&lt;/strong&gt; using Observables.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;More packages are available like a &lt;a href="https://github.com/lirx-js/router"&gt;router&lt;/a&gt; and &lt;a href="https://github.com/lirx-js/dom-material"&gt;material components&lt;/a&gt;, but are not complete now. It will be for another tutorial in the future.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reactive programming
&lt;/h3&gt;

&lt;p&gt;First, for people not familiar with Reactive Programming, I'll resume quickly:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Reactive Programming is about data streams and dynamic values (values changing over time).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Reactive Programming frameworks (like &lt;code&gt;@lirx/core&lt;/code&gt; or &lt;code&gt;RxJS&lt;/code&gt;) gives you tools to create such streams or dynamic values (ak: Observables/Signals), and combine them in complex pipelines. As opposed to imperative programming, values keep track of their changes, and automatically update any dependent.&lt;/p&gt;

&lt;p&gt;Here's a quick example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;b&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// 12&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If any of &lt;code&gt;b&lt;/code&gt; or &lt;code&gt;c&lt;/code&gt; change, then &lt;code&gt;a&lt;/code&gt; will change too.&lt;/p&gt;

&lt;p&gt;What's great is that you can listen to these changes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;effect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="c1"&gt;// asuming `b = 1` and `c = 2`, then `3` is displayed&lt;/span&gt;
&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// by setting `b = 10`, `a` is updated, and `12` is displayed&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thus, me may build a framework around such a concept, and only update the absolutely necessary DOM nodes when a change occurs in the app. All of this without the necessity to run a differ algorithm or generating a new Virtual DOM.&lt;/p&gt;

&lt;h3&gt;
  
  
  @lirx/dom
&lt;/h3&gt;

&lt;p&gt;Alright, to keep everything as short as possible (not an easy task for a framework), I'll give you a sneak peek and partial examples demonstrating the potential of &lt;a href="https://dom.lirx.org/docs/documentation/getting-started/introduction/"&gt;@lirx/dom&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The framework is based around &lt;a href="https://dom.lirx.org/docs/documentation/component/component/"&gt;components&lt;/a&gt;: a component is an &lt;code&gt;HTMLElement&lt;/code&gt; with its own template (html), style (css or scss), and javascript (actually typescript). When repeating (creating many components), they'll form a complete application. They are like the bricks forming a house.&lt;/p&gt;

&lt;h4&gt;
  
  
  Templates
&lt;/h4&gt;

&lt;p&gt;Templates are really similar to Angular. They are based on HTML with a special syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- app-main.component.html --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt;
  &lt;span class="na"&gt;(click)=&lt;/span&gt;&lt;span class="s"&gt;"$.onClickButton"&lt;/span&gt;
  &lt;span class="na"&gt;[disabled]=&lt;/span&gt;&lt;span class="s"&gt;"$.disabled"&lt;/span&gt;
  &lt;span class="na"&gt;[class.loading]=&lt;/span&gt;&lt;span class="s"&gt;"$.loading"&lt;/span&gt;
&lt;span class="nt"&gt;&amp;gt;&amp;lt;/button&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;*if=&lt;/span&gt;&lt;span class="s"&gt;"$.disabled"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  {{ $.disabledMessage }}
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;app-child-component&lt;/span&gt;
  &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="na"&gt;[message]=&lt;/span&gt;&lt;span class="s"&gt;"$.message"&lt;/span&gt;
&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;rx-slot&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"title"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    {{ $.title }}
  &lt;span class="nt"&gt;&amp;lt;/rx-slot&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;rx-slot&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"date"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    {{ $.date }}
  &lt;span class="nt"&gt;&amp;lt;/rx-slot&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/app-child-component&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's possible to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;bind: &lt;code&gt;properties&lt;/code&gt;, &lt;code&gt;attributes&lt;/code&gt;, &lt;code&gt;classes&lt;/code&gt;, &lt;code&gt;styles&lt;/code&gt;, &lt;code&gt;events&lt;/code&gt;, etc.&lt;/li&gt;
&lt;li&gt;mutate the DOM nodes with prebuild helpers like: &lt;code&gt;*if&lt;/code&gt;, &lt;code&gt;*for&lt;/code&gt;, &lt;code&gt;{{ text }}&lt;/code&gt;, etc.&lt;/li&gt;
&lt;li&gt;provide templates to child components (ak: &lt;a href="https://dom.lirx.org/docs/documentation/syntax/custom-element/#slotting---ak-provide-templates-to-our-components"&gt;slots&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In a few words: it simply does what we expect from any front-end framework.&lt;/p&gt;

&lt;h4&gt;
  
  
  Styling
&lt;/h4&gt;

&lt;p&gt;Styling is done using the same css as the one used for web components. It uses &lt;code&gt;:host&lt;/code&gt; and &lt;code&gt;:host-context&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app-main.component.scss&lt;/span&gt;
&lt;span class="nd"&gt;:host&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="mh"&gt;#666&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, the style is not purely scoped to the component itself, allowing us to style child components from a parent if necessary. If we prefer to limit the scope of our css, we may use &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/@scope"&gt;@scope&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Component
&lt;/h4&gt;

&lt;p&gt;The &lt;strong&gt;core&lt;/strong&gt; of the Component is located inside its &lt;code&gt;.ts&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app-main.component.ts&lt;/span&gt;
&lt;span class="c1"&gt;// @ts-ignore&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;html&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./app-main.component.html?raw&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// @ts-ignore&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./app-main.component.scss?inline&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * COMPONENT: 'app-main'
 */&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;ITemplateData&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;onClickButton&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;disabled&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ISignal&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ISignal&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;IObservabe&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;AppMainComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app-main&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;compileReactiveHTMLAsComponentTemplate&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;html&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;
  &lt;span class="na"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;compileStyleAsComponentStyle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;)],&lt;/span&gt;
  &lt;span class="na"&gt;templateData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;ITemplateData&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;disabled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;loading&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;onClickButton&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Event&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;date&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;map$&lt;/span&gt;&lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;interval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toLocaleDateString&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;disabled&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;onClickButton&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="c1"&gt;// ...&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you may notice all the data given to the template are reactive (Signal, Observable or Observer):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;ITemplateData&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;onClickButton&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;disabled&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ISignal&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ISignal&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;IObservabe&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The framework subscribes to them to update only the relevant nodes of the DOM: if &lt;code&gt;disabled&lt;/code&gt; changes for example, then &lt;code&gt;@lirx/dom&lt;/code&gt; just sets the attribute &lt;code&gt;disabled&lt;/code&gt; on the button and does nothing more, so calling &lt;code&gt;disabled.set(true)&lt;/code&gt;, is as fast and direct as calling &lt;code&gt;button.disabled = true&lt;/code&gt;. Moreover, the engine takes care to properly unsubscribe of these "reactive values" when the DOM nodes leave the DOM, thus, avoiding memory leaks and side effects.&lt;/p&gt;




&lt;p&gt;For more examples, I have created a &lt;a href="https://github.com/lirx-js/dom-examples"&gt;dedicated repository&lt;/a&gt;, and it is as simple as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/lirx-js/dom-examples
&lt;span class="nb"&gt;cd &lt;/span&gt;dom-examples
yarn &lt;span class="c"&gt;# or npm i&lt;/span&gt;
yarn dev &lt;span class="c"&gt;# or npm run dev&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;blockquote&gt;
&lt;p&gt;This was just a quick peek, the tip of the iceberg.&lt;br&gt;
If you're already interested you may &lt;a href="https://dom.lirx.org/docs/documentation/getting-started/introduction/"&gt;start from here&lt;/a&gt;. Else, let's jump to a comparison with the other frameworks.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  How it differs from the others frameworks ?
&lt;/h4&gt;

&lt;p&gt;First of all &lt;code&gt;@lirx/dom&lt;/code&gt; was build for reactivity: it's deeply integrated in its core and in the manner we build the components. Thus, it's fast and efficient, where other frameworks added reactivity later, accepting compromises on performances.&lt;/p&gt;

&lt;p&gt;If we do a quick and dirty comparison between Angular (left) and &lt;code&gt;@lirx/dom&lt;/code&gt; (right), on an autogenerated app having 3900 components (73K lines):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd3sncx1g0715dbcyo364.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd3sncx1g0715dbcyo364.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We may see how &lt;code&gt;@lirx/dom&lt;/code&gt; only updates what's relevant and nothing more, &lt;strong&gt;out-of-the-box&lt;/strong&gt;, where such optimizations are only possible with other frameworks, if we follow specific (and sometimes painful) constraints. You may find a full dedicated &lt;a href="https://dom.lirx.org/docs/documentation/comparisons/benchmarks/complex-app/"&gt;benchmark comparison here&lt;/a&gt;. The goal is not to demean Angular, but to offer the best performances, by comparing and optimizing continuously as much as possible. Thus delivering the best possible user experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's next ?
&lt;/h2&gt;

&lt;p&gt;The next step is to get feedback from developers to improve the framework.&lt;/p&gt;

&lt;p&gt;Are you interested in such project ? Does a Reactive Programming framework feel appealing and simple enough for everyone ? Have you any idea of improvement, or suggestion ?&lt;/p&gt;

&lt;p&gt;If you want to help, test the framework, comment, etc., your opinion matters ! I consider that evey feedback may only be benefit and improve what I think is already a great concept.&lt;/p&gt;

&lt;p&gt;Thanks for reading me. I hoped, I've created some interest in the community.&lt;/p&gt;

</description>
      <category>signal</category>
      <category>reactive</category>
      <category>framework</category>
      <category>ui</category>
    </item>
    <item>
      <title>Get ready ! Signals for everyone.</title>
      <dc:creator>valentin</dc:creator>
      <pubDate>Mon, 04 Mar 2024 07:04:29 +0000</pubDate>
      <link>https://dev.to/lifaon74/get-ready-signals-for-everyone-6k6</link>
      <guid>https://dev.to/lifaon74/get-ready-signals-for-everyone-6k6</guid>
      <description>&lt;p&gt;Heard about Angular's signals, and want to play with them in any projet ?&lt;/p&gt;

&lt;p&gt;Well, you're at the right place !&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;If you've followed the recent javascript trends, you've probably heard about Signals.&lt;/p&gt;

&lt;p&gt;Else, don't worry I'll recap quickly:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A &lt;em&gt;Signal&lt;/em&gt; is a wrapper around a value that &lt;strong&gt;can notify interested consumers when that value changes&lt;/strong&gt;. Signals can contain any value, from simple primitives to complex data structures.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Adopting Signals, will usually &lt;strong&gt;greatly simplify the design&lt;/strong&gt; of your applications: if you have variables depending on other variables (computed), variables that mutates other time (updated) or large state management (ak: &lt;code&gt;Store&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;In addition, using Signals to hold dynamic values is easier that managing them yourself: updating manually your computed variables requires more code and quickly becomes error-prone, where Signals handle it automatically, with a short and elegant syntax.&lt;/p&gt;

&lt;p&gt;Other front-end frameworks provide something similar:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Angular&lt;/code&gt; recently &lt;a href="https://angular.io/guide/signals"&gt;introduced Signals&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;React&lt;/code&gt; uses &lt;code&gt;useState&lt;/code&gt; which is similar.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Vue&lt;/code&gt;  or &lt;code&gt;Svelte&lt;/code&gt; have their own internal implementation of such Signals.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So it's something truly essential in the front-end framework ecosystem.&lt;/p&gt;

&lt;p&gt;Thus, I've created my own implementation of Signals specially optimized for Reactive Programming and working in any javascript runtime: &lt;a href="https://core.lirx.org/docs/documentation/signals/introduction/"&gt;@lirx/core&lt;/a&gt;. This library follows the Angular Signals' API for compatibility, with little differences, but uses a totally different implementation. &lt;strong&gt;Is not tied to any specific framework&lt;/strong&gt;, and can be used in any project.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This project aims to give to everyone the opportunity to use Signals and learn Reactive Programming,&lt;br&gt;
by giving to developers universal tools to build complex applications.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It's not restricted to Angular, but it has a close API, in consequence the following part comes directly&lt;br&gt;
from the &lt;a href="https://angular.io/guide/signals"&gt;Angular Signals' tutorial page&lt;/a&gt;. If you're already familiar with Signals, you may jump to the differences.&lt;/p&gt;
&lt;h2&gt;
  
  
  Signal
&lt;/h2&gt;

&lt;p&gt;A Signal is created using the &lt;code&gt;signal&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@lirx/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// signals are getter functions - calling them reads their value.&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;The count is: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To change the value of a writable signal, you can either &lt;code&gt;.set()&lt;/code&gt; it directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or use the &lt;code&gt;.update()&lt;/code&gt; operation to compute a new value from the previous one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// increment the count by 1.&lt;/span&gt;
&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Computed
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;computed signal&lt;/strong&gt; derives its value from other signals. Define one using &lt;code&gt;computed&lt;/code&gt; and specifying a derivation function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;computed&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@lirx/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;doubleCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This library provides the sames functionalities and optimizations as the Angular's one: computed signals are both lazily evaluated and memoized.&lt;/p&gt;

&lt;p&gt;As a result, it's safe to perform computationally expensive derivations in computed signals, such as filtering arrays.&lt;/p&gt;

&lt;h2&gt;
  
  
  Effects
&lt;/h2&gt;

&lt;p&gt;Signals are useful because they can notify interested consumers when they change. An &lt;strong&gt;effect&lt;/strong&gt; is an operation that runs whenever one or more signal values change. You can create an effect with the &lt;code&gt;effect&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;effect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`The current count is: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Effects always run at least once. When an effect runs, it tracks any signal value reads. Whenever any of these signal values change, the effect runs again. Similar to computed signals, effects keep track of their dependencies dynamically, and only track signals which were read in the most recent execution.&lt;/p&gt;

&lt;h2&gt;
  
  
  Differences between this library and the Angular's Signals
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Close but not identical
&lt;/h3&gt;

&lt;p&gt;The  Angular Signals' API reflects the constraints of the framework. In another hand, &lt;code&gt;@lirx/core&lt;/code&gt; is not limited to any framework, so any part related to Angular is removed, meaning that the API is not totally identical, but extremely close. In 99% cases, you won't have to change anything, and it will work right out of the box.&lt;/p&gt;

&lt;h3&gt;
  
  
  Faster in some cases
&lt;/h3&gt;

&lt;p&gt;The library is focused on performances, and we have done &lt;a href="https://core.lirx.org/docs/documentation/performances/vs-angular/"&gt;benchmarks&lt;/a&gt; to compare both frameworks. We believe that &lt;code&gt;effect&lt;/code&gt;s are the most important part of Signals, and, in this specific aspect, &lt;code&gt;@lirx/core&lt;/code&gt; outperforms Angular by a factor of 8. Combined with a framework like &lt;a href="https://dom.lirx.org/docs/documentation/getting-started/introduction/"&gt;@lirx/dom&lt;/a&gt; (alpha), this promises amazing applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  @lirx/core includes Observables
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;@lirx/core&lt;/code&gt; includes all the tools for Reactive Programming in a single package.&lt;br&gt;
It's designed to create complex and consistent Reactive Applications (mostly front-end), like a breeze.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;If you've searched for the perfect Reactive Programming library, don't look further: &lt;code&gt;@lirx/core&lt;/code&gt; is probably the right candidate. You won't have anymore to update manually your computed/derived variables, you'll be able to reflect their changes immediately, and you will be able to create complex and optimized data pipelines in just a few lines of code.&lt;/p&gt;

&lt;p&gt;All of this working independently of any framework !&lt;/p&gt;

&lt;p&gt;It's perfect for front-end application where updating the DOM must be done in a simple and elegant manner, while keeping the application consistent.&lt;/p&gt;

&lt;p&gt;Coding in Reactive Programming opens you a whole new world of possibilities. I hope I have piqued your interest with this presentation.&lt;/p&gt;

&lt;h2&gt;
  
  
  About @lirx/core
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://core.lirx.org/docs/documentation/getting-started/introduction/"&gt;@lirx/core&lt;/a&gt; &lt;strong&gt;is a Reactive Programming framework&lt;/strong&gt; with a lot of prebuild functions, clear documentations and many examples to teach you how to become a master of RP. It allows you to develop complex applications and pipelines that scale easily. Moreover, it is the &lt;a href="https://core.lirx.org/docs/documentation/performances/introduction/"&gt;fastest and smallest&lt;/a&gt; javascript library for &lt;code&gt;Reactive Programming&lt;/code&gt;. So it's a good candidate to start your journey with Observables and Signals.&lt;/p&gt;

&lt;p&gt;Feel free to test this library, share it and give your feedbacks.&lt;/p&gt;

</description>
      <category>signal</category>
      <category>reactivity</category>
      <category>typescript</category>
      <category>stream</category>
    </item>
    <item>
      <title>Reactive Programming: a gentle introduction</title>
      <dc:creator>valentin</dc:creator>
      <pubDate>Mon, 20 Feb 2023 09:54:15 +0000</pubDate>
      <link>https://dev.to/lifaon74/reactive-programming-a-gentle-introduction-3ai6</link>
      <guid>https://dev.to/lifaon74/reactive-programming-a-gentle-introduction-3ai6</guid>
      <description>&lt;p&gt;In this post I want to introduce you to Reactive Programming, it's benefits, and why should you care.&lt;/p&gt;




&lt;p&gt;Even developers already using Reactive Programming Frameworks (ex: Angular) struggle with this concept.&lt;br&gt;
It is something &lt;em&gt;mysterious&lt;/em&gt;, and &lt;em&gt;complex&lt;/em&gt;, often limiting its usage. However, coding in RP gives very &lt;strong&gt;powerful powers&lt;/strong&gt;, that are clearly underestimated. So today, I will try to clear up this mystery.&lt;/p&gt;
&lt;h2&gt;
  
  
  Reactive programming
&lt;/h2&gt;

&lt;p&gt;Let's begin with a simple definition:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Reactive programming is a declarative programming paradigm concerned with &lt;strong&gt;data streams&lt;/strong&gt; and the &lt;strong&gt;propagation of change&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In Reactive Programming, we will focus on the data flow, and if something changes, then the flow updates itself.&lt;/p&gt;

&lt;p&gt;I'll illustrate this with a switch and a light bulb:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fktu53x02snveixwp9dxc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fktu53x02snveixwp9dxc.png" alt="proactive-switch-light" width="400" height="240"&gt;&lt;/a&gt;&lt;br&gt;
Credit: Dan Lew&lt;sup id="fnref1"&gt;1&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://en.wikipedia.org/wiki/Imperative_programming" rel="noopener noreferrer"&gt;imperative programming&lt;/a&gt;  (this is probably your main way of coding), the switch modifies the state of the bulb. In this case, the switch is &lt;strong&gt;proactive&lt;/strong&gt;, pushing new states to the bulb; whereas the bulb is &lt;strong&gt;passive&lt;/strong&gt;, simply receiving commands to change its state.&lt;/p&gt;

&lt;p&gt;However, in reactive programming it's the opposite: the bulb listen to the state of the switch, and then modify itself accordingly. In this model, the bulb is &lt;strong&gt;reactive&lt;/strong&gt;, changing its state based on the switch's state; whereas the switch is &lt;strong&gt;observable&lt;/strong&gt;, in that others can observe its state changes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flhlct3fmr4mvzti3yrjy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flhlct3fmr4mvzti3yrjy.png" alt="reactive-switch-light" width="400" height="240"&gt;&lt;/a&gt;&lt;br&gt;
Credit: Dan Lew&lt;sup id="fnref1"&gt;1&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;This approach makes more sense when working with streams and flows, because you won't have to update the source (the switch) to add more sinks (more light bulbs). Each light bulb is self-managed, and doesn't rely on a modification of the code of the switch to update itself.&lt;/p&gt;

&lt;p&gt;Maybe you've seen it, but you're already familiar with this concept: everytime you use the method &lt;code&gt;addEventListener&lt;/code&gt;, you're subscribing to Events and update other variables, elements, etc... So, you're actually doing reactive programming. Here, we'll push this concept further, to create amazing possibilities of interactions between data, objects, sources, etc...&lt;/p&gt;
&lt;h2&gt;
  
  
  Imperative programming
&lt;/h2&gt;

&lt;p&gt;Now, we will compare &lt;em&gt;imperative&lt;/em&gt; vs &lt;em&gt;reactive&lt;/em&gt; programming &lt;sup id="fnref2"&gt;2&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;Let's explain it with a simple assignment: &lt;code&gt;a = b + c&lt;/code&gt;. &lt;code&gt;a&lt;/code&gt; is being assigned the result of &lt;code&gt;b + c&lt;/code&gt;. This is done &lt;em&gt;in the instant&lt;/em&gt; in &lt;strong&gt;imperative programming&lt;/strong&gt;: the expression is evaluated, and later, the values of &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt; can be changed with no effect on the value of &lt;code&gt;a&lt;/code&gt;. This is how we code most of the time. &lt;/p&gt;

&lt;p&gt;On the other hand, in &lt;strong&gt;reactive programming&lt;/strong&gt;, the value of &lt;code&gt;a&lt;/code&gt; is automatically updated whenever the values of &lt;code&gt;b&lt;/code&gt; or &lt;code&gt;c&lt;/code&gt; change, without the program having to explicitly re-execute the statement &lt;code&gt;a = b + c&lt;/code&gt; to determine the presently assigned value of &lt;code&gt;a&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Imperative programming
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 3 (not 12 because "=" is not a reactive assignment operator)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Reactive programming
&lt;/h3&gt;

&lt;p&gt;Now imagine you have a special operator &lt;code&gt;$=&lt;/code&gt; that changes the value of a variable (executes code on the right side of the operator and assigns result to left side variable) not only when explicitly initialized, but also when referenced variables (on the right side of the operator) are changed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;
&lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 12&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Such an operator would be extremely useful: &lt;code&gt;a&lt;/code&gt; would be always up-to-date. We may start to imagine new langues and frameworks using this paradigm, and having quite powerful features.&lt;/p&gt;

&lt;p&gt;This is especially true for &lt;a href="https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller" rel="noopener noreferrer"&gt;MVC&lt;/a&gt; architectures, where reactive programming can facilitate changes in an underlying model that are reflected automatically in an associated view (typically an UI framework like Angular of React for a web application).&lt;/p&gt;

&lt;h2&gt;
  
  
  @lirx/core as a framework
&lt;/h2&gt;

&lt;p&gt;The most popular reactive framework is &lt;a href="https://reactivex.io/" rel="noopener noreferrer"&gt;ReactiveX&lt;/a&gt;, and it's javascript implementation &lt;a href="https://rxjs.dev/" rel="noopener noreferrer"&gt;RxJS&lt;/a&gt;. However, this library has the following issues:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it lacks of clear documentation (you need a master in reactive programming, to understand even the simplest functions), and some parts of the documentation are outdated, or simply don't exist.&lt;/li&gt;
&lt;li&gt;it is based on &lt;code&gt;states&lt;/code&gt; ('next', 'error' and 'complete') instead of pure data flow, which restrains many operators, and confuse users when building complex interactions. &lt;/li&gt;
&lt;li&gt;its design limits its performance as well as the resulting bundle size.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead, I'll use &lt;a href="https://core.lirx.org/docs/documentation/getting-started/introduction/" rel="noopener noreferrer"&gt;@lirx/core&lt;/a&gt;, and  show you how simple is it to implement the previous example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;$b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b$&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;$c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c$&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;a$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;add$&lt;/span&gt;&lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b$&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c$&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 

&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;

&lt;span class="nf"&gt;$b&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// a$ =&amp;gt; 12&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I will break down the code, to explain each part in detail:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;$b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b$&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;a href="https://core.lirx.org/docs/reference/let/" rel="noopener noreferrer"&gt;let$$&lt;/a&gt; function allows us to create a &lt;em&gt;mutable reactive variable&lt;/em&gt;. We usually split the result in two constants:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;$b&lt;/code&gt;, an &lt;strong&gt;&lt;a href="https://core.lirx.org/docs/reference/observer/" rel="noopener noreferrer"&gt;Observer&lt;/a&gt;&lt;/strong&gt;, to &lt;strong&gt;set&lt;/strong&gt; the value&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;b$&lt;/code&gt;, an &lt;strong&gt;&lt;a href="https://core.lirx.org/docs/reference/observable/" rel="noopener noreferrer"&gt;Observable&lt;/a&gt;&lt;/strong&gt;, to &lt;strong&gt;read&lt;/strong&gt; it&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Moreover, as you probably guess, we've initialed &lt;code&gt;b&lt;/code&gt; with the value &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We repeat the process for &lt;code&gt;c&lt;/code&gt;, and have now two reactive variables.&lt;/p&gt;

&lt;p&gt;Then we have to perform the sum:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;a$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;add$&lt;/span&gt;&lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b$&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c$&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we declare &lt;code&gt;a$&lt;/code&gt; as the addition of &lt;code&gt;b$&lt;/code&gt; and &lt;code&gt;c$&lt;/code&gt;, using the function &lt;a href="https://core.lirx.org/docs/reference/reactive-add/" rel="noopener noreferrer"&gt;add$$&lt;/a&gt;. All of these variables are Observables. In consequence, if any of &lt;code&gt;b$&lt;/code&gt; or &lt;code&gt;c$&lt;/code&gt; changes, then &lt;code&gt;a$&lt;/code&gt; changes too.&lt;/p&gt;

&lt;p&gt;Since &lt;code&gt;a$&lt;/code&gt; is an Observable, we have to &lt;strong&gt;subscribe&lt;/strong&gt; to it in order to read its value (because this one changes over time):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt; already have a value, as a result, we'll immediately see &lt;code&gt;3&lt;/code&gt; logged.&lt;/p&gt;

&lt;p&gt;Finally, if we update &lt;code&gt;b&lt;/code&gt; writing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;$b&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;a$&lt;/code&gt; will instantly change to &lt;code&gt;12&lt;/code&gt;, and this value will be logged.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This is &lt;strong&gt;just the tip of the iceberg&lt;/strong&gt;. As you probably guess, adopting a reactive programming approach gives you very powerful tools to design &lt;strong&gt;clear&lt;/strong&gt; and &lt;strong&gt;organized&lt;/strong&gt; data flows.&lt;/p&gt;

&lt;p&gt;A stream can be used as an input to another one. Even multiple streams can be used as inputs to another stream. You can merge two streams. You can filter a stream to get another one that has only those events you are interested in. You can map data values from one stream to another new one. Etc.&lt;/p&gt;

&lt;p&gt;This way you'll be able to create well-defined and &lt;strong&gt;robust&lt;/strong&gt; streams of events and data, and you'll have the tools to create flow that match perfectly and instinctively the requirements of stable and large applications perfectly fitting your needs.&lt;/p&gt;

&lt;p&gt;I highly recommend you to stop thinking purely in imperative programming and start practicing reactive programming to discover an amazing world of new possibilities.&lt;/p&gt;

&lt;p&gt;I guarantee you won't regret it 🦸&lt;/p&gt;

&lt;h2&gt;
  
  
  About @lirx/core
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://core.lirx.org/docs/documentation/getting-started/introduction/" rel="noopener noreferrer"&gt;@lirx/core&lt;/a&gt; &lt;strong&gt;is a Reactive Programming framework&lt;/strong&gt; with a lot of prebuild functions,&lt;br&gt;
clear documentations and many examples to teach you how to become a master of RP.&lt;br&gt;
It allows you to develop complex applications and pipelines that scale easily.&lt;br&gt;
Moreover, it is the &lt;a href="https://core.lirx.org/docs/documentation/performances/" rel="noopener noreferrer"&gt;fastest and smallest&lt;/a&gt;&lt;br&gt;
javascript library for &lt;code&gt;Reactive Programming&lt;/code&gt;.&lt;br&gt;
So it's a good candidate to start your journey with Observables.&lt;/p&gt;

&lt;p&gt;Feel free to test this library, share it and give your feedbacks.&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;&lt;a href="https://blog.danlew.net/2017/07/27/an-introduction-to-functional-reactive-programming/" rel="noopener noreferrer"&gt;Dan Lew - An Introduction to Functional Reactive Programming&lt;/a&gt; ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Reactive_programming" rel="noopener noreferrer"&gt;Wikipedia - Reactive programming&lt;/a&gt; ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>Why Reactive Programming is so awesome ?</title>
      <dc:creator>valentin</dc:creator>
      <pubDate>Tue, 20 Dec 2022 06:56:59 +0000</pubDate>
      <link>https://dev.to/lifaon74/why-reactive-programming-is-so-awesome--73a</link>
      <guid>https://dev.to/lifaon74/why-reactive-programming-is-so-awesome--73a</guid>
      <description>&lt;p&gt;In this post I want to share my personal experience with Reactive Programming and why you should consider using it.&lt;/p&gt;

&lt;p&gt;This article was originally published on &lt;a href="https://core.lirx.org/blog/reactive-programming/"&gt;https://core.lirx.org/blog/reactive-programming/&lt;/a&gt;. Take a look to get a better reader experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problematic
&lt;/h2&gt;

&lt;p&gt;Let's imagine this simple use case:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Two &amp;lt;input&amp;gt; in which the user may enter two numbers to perform an addition.&lt;br&gt;
The result is displayed right below everytime the user type a new value.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;We may:&lt;/strong&gt; &lt;em&gt;implement it from scratch&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Listening to changes in the two &amp;lt;input&amp;gt;, compute the addition, and update the result. This solution is &lt;strong&gt;performant&lt;/strong&gt;. However, increasing the number of inputs tends to exponentially scale the complexity of the code.&lt;br&gt;
Its usually ends up in &lt;strong&gt;bugs&lt;/strong&gt; and nightmares to maintain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Or we may:&lt;/strong&gt; &lt;em&gt;use a front-end framework&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Like React with &lt;code&gt;useState&lt;/code&gt; or Angular with its &lt;code&gt;ChangeDetector&lt;/code&gt;. We'll win in &lt;strong&gt;code readability&lt;/strong&gt;, at the cost of &lt;strong&gt;performance lost&lt;/strong&gt; (when running the change detection algorithm). Moreover, the app will have a bigger size.&lt;/p&gt;

&lt;p&gt;More generally, this problematic is applicable to &lt;strong&gt;every evolving values&lt;/strong&gt;, which are nothing more than &lt;strong&gt;streams of data&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Some examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the current date and time updated every seconds&lt;/li&gt;
&lt;li&gt;a translated text, that changes with the user locale (and maybe some &lt;code&gt;&amp;lt;select&amp;gt;&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;the state of a submit button (enabled/disabled), if a form is valid/invalid&lt;/li&gt;
&lt;li&gt;a data stream coming from the backend (ex: live stats)&lt;/li&gt;
&lt;li&gt;and many more...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Handling the refresh, the combination, the transformation, etc. of such streams, is generally complex. It creates code with low readability, frequent uncovered cases, and inconsistent states.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;What if we could have all benefits: performance, code readability, and consistent states ?&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The answer
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Reactive Programming&lt;/strong&gt; is the perfect answer to manage streams of data, async flows, and evolving values.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With RP, we build streams of values called &lt;strong&gt;Observables&lt;/strong&gt;. And, we will have an &lt;strong&gt;amazing toolbox&lt;/strong&gt; of functions to combine, create and filter any of those streams. That's where the "functional" magic kicks in. A stream can be used as an input to another one. Even multiple streams can be used as inputs to another stream. We can merge two streams. We can filter a stream to get another one that has only those events we are interested in. We can map data values from one stream to another new one. Etc.&lt;/p&gt;

&lt;p&gt;Actually, you're probably already familiar with async data streams: Promises, EventListeners, setInterval, etc.&lt;br&gt;
Reactive programming is all about this but on &lt;strong&gt;steroids&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You'll be able to create Observables of anything, not just from click and hover events: variables, user inputs, properties, tasks, data structures,...&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hhF3KXIn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fjj4890m7ikq3fjr2c1b.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hhF3KXIn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fjj4890m7ikq3fjr2c1b.jpg" alt="Image description" width="400" height="370"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Reactive Programming by example
&lt;/h2&gt;

&lt;p&gt;You've probably heard of &lt;a href="https://rxjs.dev/"&gt;RxJS&lt;/a&gt; which is the most commonly used RP library in javascript. However, I will promote here &lt;strong&gt;&lt;a href="https://core.lirx.org/docs/documentation/getting-started/introduction/"&gt;@lirx/core&lt;/a&gt;&lt;/strong&gt;, a library strongly outperforming RxJS in performances, having a better documentation and trying to be simpler.&lt;/p&gt;

&lt;p&gt;Ok, let's start:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;onInput$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fromEventTarget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;input&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It creates an &lt;em&gt;Observable&lt;/em&gt; for an &amp;lt;input&amp;gt;, listening and emitting Events of type &lt;code&gt;input&lt;/code&gt;. It triggers, when the user types something in this input.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;An Observable is simply a source emitting values.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;An Observable is not started until we subscribe to it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;unsubscribe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;onInput$&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The provided function is called an &lt;em&gt;Observer&lt;/em&gt;, and it receives the values from our Observable.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;An Observer is a sink receiving values from an Observable&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When the user types in the &amp;lt;input&amp;gt;, its value is simply logged in the console.&lt;/p&gt;

&lt;p&gt;Subscribing to an Observable returns an &lt;code&gt;unsubscribe&lt;/code&gt; function. When called, it notifies the Observable to stop sending more values, and usually free the Observable's resources.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onkeydown&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Escape&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;unsubscribe&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, when pressing the &lt;code&gt;Escape&lt;/code&gt; key, we terminate our Observable, and stop listening to the &lt;code&gt;input&lt;/code&gt;'s Events.&lt;/p&gt;

&lt;p&gt;At this point, you may notice than subscribing/unsubscribing from streams of data is extremely simple with Observables. We don't have to keep a reference to the callback function of &lt;code&gt;.addEventListener&lt;/code&gt; for example. It's handled by the Observable itself.&lt;/p&gt;

&lt;p&gt;So, we have now an Observable sending &lt;code&gt;input&lt;/code&gt;'s Events, but what if we want the input's value ? It would be more convenient:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;inputValue$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;map$$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;onInput$&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;map$$&lt;/code&gt; function transforms the incoming Events into the input's value. Such a function is called an ObservablePipe.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;An ObservablePipe is a function which transform an Observable to another one&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So now, when the user types in the &amp;lt;input&amp;gt;, &lt;code&gt;inputValue$&lt;/code&gt; emits this input's value instead of an Event.&lt;/p&gt;

&lt;p&gt;And this is where Observables shine: we can combine them, transform their values, create complex data flows, etc. Keeping the complex transformations to &lt;code&gt;@lirx/core&lt;/code&gt; and focusing only on the interdependence of our data.&lt;/p&gt;




&lt;p&gt;Let's assemble everything to create a custom function building an Observable from an input's value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fromInput&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;HTMLInputElement&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;IObservable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="nx"&gt;reference&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nx"&gt;map$$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fromEventTarget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;input&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here I have extended a little our first Observable with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;reference(() =&amp;gt; input.value)&lt;/code&gt;: this creates an Observable which emits the input's value when subscribed.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;merge&lt;/code&gt;: subscribes to the list of Observables and re-emits all their values.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here, we will combine &lt;code&gt;merge&lt;/code&gt; and &lt;code&gt;reference&lt;/code&gt; because the &lt;code&gt;input&lt;/code&gt; event is not immediately triggered when we subscribe to our Observable, so we need another way to retrieve the initial input's value.&lt;/p&gt;

&lt;p&gt;In short, subscribing to this Observable sends the initial input's value and next values typed by the user.&lt;/p&gt;

&lt;p&gt;And if we want to consume our Observable, we simply write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;inputValue$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fromInput&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;inputValue$&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Alright, let's finish by solving the initial example:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Two &amp;lt;input&amp;gt; in which the user may enter two numbers to perform an addition.&lt;br&gt;
The result is displayed right below everytime the user type a new value.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;First we have to convert the input's values to numbers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fromNumberInput&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;HTMLInputElement&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;IObservable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;pipe$$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fromInput&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;map$$$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nx"&gt;filter$$$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;isNaN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
  &lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;pipe$$&lt;/code&gt; function applies many &lt;em&gt;ObservablePipes&lt;/em&gt; to an Observable.&lt;/p&gt;

&lt;p&gt;We start by applying the &lt;code&gt;map$$$&lt;/code&gt; ObservablePipe to convert the input's values to numbers.&lt;/p&gt;

&lt;p&gt;Then,  through the ObservablePipe &lt;code&gt;filter$$$&lt;/code&gt;, we filter these numbers to keep only the valid ones. We have to do it to handle the cases where the user types nothing or strings which are not numbers.&lt;/p&gt;

&lt;p&gt;Finally, to perform an addition from two Observables, we may use &lt;code&gt;add$$&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;add$$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;fromNumberInput&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inputA&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nx"&gt;fromNumberInput&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inputB&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And Voilà ! We have created an Observable performing the addition of two &amp;lt;input&amp;gt;.&lt;/p&gt;

&lt;p&gt;Let's recap:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fromInput&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;HTMLInputElement&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;IObservable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="nx"&gt;reference&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nx"&gt;map$$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fromEventTarget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;input&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fromNumberInput&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;HTMLInputElement&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;IObservable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;pipe$$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fromInput&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;map$$$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nx"&gt;filter$$$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;isNaN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
  &lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;add$$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;fromNumberInput&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inputA&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nx"&gt;fromNumberInput&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inputB&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;result$&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://stackblitz.com/edit/typescript-oee9wm?file=index.ts"&gt;You can find a demo here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thinking in Reactive Programming gives us the opportunity to focus on our data flow, and not the complex implementation. Doing the same thing from scratch, requires more code and easily conducts to bugs and poor maintainability.&lt;/p&gt;

&lt;p&gt;This example is just the tip of the iceberg: we can apply the same operations on different kinds of streams, for instance, on a stream of API responses, a stream of bytes to compress, a string to translate dynamically, etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why you should consider adopting RP ?
&lt;/h2&gt;

&lt;p&gt;Reactive Programming raises the level of abstraction of our code, so we can focus on the interdependence of events that define the business logic, rather than having to constantly fiddle with a large amount of implementation details. &lt;strong&gt;Code in RP will likely be more concise.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Dynamic values are managed in a different way: instead of using some kind of &lt;code&gt;refresh&lt;/code&gt; function (to update for example many DOM nodes, if something changes), the variables (here some &lt;em&gt;Observables&lt;/em&gt;) are always fresh and updated. So, we won't have to remember to call a &lt;code&gt;refresh()&lt;/code&gt; when a change occurs, meaning less bugs and less code in our application.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The benefits are really important in web applications, where we have to deal with a multitude of UI events, async requests, etc... and react to these changes, like updating the DOM.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Moreover, it handles for us any &lt;strong&gt;cancellation&lt;/strong&gt;: from aborting a fetch, to cancelling an eventListener or a setInterval. This point, is frequently ignored by developers creating memory-leaks, or very difficult bugs to fix (like an async tasks that should have been cancelled, but continues to run until it break something - most of the time updating something unwanted).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Cancelling, any pending tasks (ex: from a destroyed html component) is extremely simple with RP.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  The drawbacks and advantages of RP
&lt;/h3&gt;

&lt;p&gt;Reactive Programming feels like it is essentially for &lt;strong&gt;purists and advanced programmers&lt;/strong&gt;: it's difficult for beginners and involves complex and abstract logic (everything is a stream, and we must think async).&lt;/p&gt;

&lt;p&gt;But in my opinion, the Observables are just like Promises in their time. Do you remember the first time you learned how Promises worked ? This was a new way of thinking our code, and I've seen many new developers struggling to understand how to use them. However, now, I'm pretty sure, you're using Promise &lt;strong&gt;frequently without difficulties&lt;/strong&gt;.&lt;br&gt;
They became &lt;strong&gt;essential&lt;/strong&gt; for everyone, because they help so much with async coding. They radically changed our way of developing and thinking our applications.&lt;/p&gt;

&lt;p&gt;Well Observables, are just like Promises: at first glance, they seem difficult to handle, but they radically solve every problem related to async algorithms or data streams. They are a very good solution for front-end applications, simplifying fetch calls, dynamic variables in templates, user events, and much more...&lt;/p&gt;

&lt;p&gt;And the best part of it: it doesn't impact negatively the performances. On the contrary, when tuned properly, you'll see an uge improvement in your application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Now, you are ready to begin with Reactive Programming, and discover an amazing world of streams, pipes, and dynamic values.&lt;/p&gt;

&lt;p&gt;I hope you enjoyed the beauty of this approach, and maybe I converted you to this awesome way of coding. Even if you're not convinced yet, you should do a quick try, as mastering RP will open you a new world of possibilities.&lt;/p&gt;

&lt;p&gt;Create a demo, share it with others, and help to build the web of tomorrow.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rpHFfM7t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e2owo7ohrcvsxwlaf76q.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rpHFfM7t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e2owo7ohrcvsxwlaf76q.jpg" alt="Image description" width="400" height="460"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  About @lirx/core
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://core.lirx.org/docs/documentation/getting-started/introduction/"&gt;@lirx/core&lt;/a&gt; &lt;strong&gt;is a Reactive Programming framework&lt;/strong&gt; with a lot of prebuild functions, clear documentations and many examples to teach you how to become a master of RP. It allows you to develop complex applications and pipelines that scale easily. Moreover, it is the &lt;a href="https://core.lirx.org/docs/documentation/performances/"&gt;fastest and smallest&lt;/a&gt;&lt;br&gt;
javascript library for &lt;code&gt;Reactive Programming&lt;/code&gt;. So it's a good candidate to start your journey with Observables.&lt;/p&gt;

&lt;p&gt;Feel free to test this library, share it and give your feedbacks.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>reactive</category>
      <category>observable</category>
    </item>
  </channel>
</rss>
