DEV Community

Cover image for Reactivity Practicality in Popular JS Libraries
Jin
Jin

Posted on • Originally published at mol.hyoo.ru

Reactivity Practicality in Popular JS Libraries

Let's now take what we know about reactivity and try to formulate what the most practical model of reactivity might look like. What properties should it have to make it pleasant to use, so that it gives us a minimum of problems, so that the user has everything stable, fast, and always understands what is going on..

Aspect โœ…Usable โŒUnusable
Style ๐Ÿค“Obj ๐ŸงProc ๐ŸคฏFunc ๐Ÿ˜ตCell
Watch ๐ŸคLinks ๐Ÿ”ŽPolling ๐ŸŽ‡Events
Dupes ๐ŸŽญEquality ๐Ÿ†”Identity ๐Ÿ‘ฏโ€โ™€๏ธEvery ๐Ÿ”€Reconcile
Origin ๐Ÿš‚Pull ๐ŸฅŒPush
Order ๐Ÿ‘จโ€๐Ÿ’ปCode ๐Ÿ“ฐSubscribe ๐ŸงจEvent ๐Ÿ“ถDeep
Flow ๐Ÿš•Auto ๐ŸฆฝManual
Tonus ๐ŸฆฅLazy ๐Ÿ”Instant โฐDefer
Error ๐ŸฆบStore โ›”Stop โฎRevert ๐ŸŽฒUnstable
Cycle ๐ŸŒ‹Fail ๐Ÿ’คInfinite ๐ŸŽฐLimbo ๐ŸšซUnreal
Depth ๐ŸŒŒHeap ๐Ÿ—ปStack ๐ŸšงLimit
Atomic ๐ŸฆถBase ๐Ÿคผโ€โ™‚๏ธFull ๐Ÿ‘ปAlone
Extern ๐Ÿ‡Async ๐ŸŠโ€โ™‚๏ธSync

Let's now take various well-known libraries and frameworks and see how close they are to the ideal... But first, a small note...

Default behavior

Below we consider only the default behavior and the code style recommended by the authors. It is clear that you can always somehow get around the problems. Somewhere the behavior can be changed with a config parameter. Somewhere you need to remember to write additional code here and there. Somewhere you need to get creative with hellish crutches. And in some cases you will have to abandon one library altogether and add another one on the side.

However, it is important to understand that the author of the library, even if he is deeply wrong, most likely has greater expertise than an ordinary application developer. In this topic in general, and in his library in particular. Therefore, most third-party code using it will be written in the canonical style, designed for default behavior. And any departure from the default will require additional code, which you must remember to write, and spend time writing it correctly.

๐ŸŽ“ Expert's choice
๐Ÿญ Minimum code
๐Ÿ‘€ Increased attention
๐Ÿ‘พ Third party code

Further comparison is useful not so much in order to understand which one should be taken urgently and which one should be thrown away immediately. But also in order to understand what you need to be prepared for when starting a project on a particular technology.

Some aspects may be completely unimportant to you. Some may turn out to be show stoppers. And some can be easily bypassed. And it would be nice to lay some straws in advance so as not to have to deal with painful debugging and optimization later.

Reactive libraries

Lib Style Watch Dupes Origin Tonus Order Flow Error Cycle Depth Atomic Extern
$mol_wire ๐Ÿค“ โœ… ๐Ÿค โœ… ๐ŸŽญ โœ… ๐Ÿš‚ โœ… ๐Ÿฆฅ โœ… ๐Ÿ‘จโ€๐Ÿ’ป โœ… ๐Ÿš• โœ… ๐Ÿฆบ โœ… ๐ŸŒ‹ โœ… ๐Ÿ—ป โŒ ๐Ÿ‘ป โŒ ๐Ÿ‡ โœ…
CellX ๐Ÿค“ โœ… ๐Ÿค โœ… ๐Ÿ†” โŒ ๐Ÿš‚ โœ… ๐Ÿฆฅ โœ… ๐Ÿ‘จโ€๐Ÿ’ป โœ… ๐Ÿš• โœ… ๐Ÿฆบ โœ… ๐ŸŒ‹ โœ… ๐Ÿ—ป โŒ ๐Ÿฆถ โœ… ๐Ÿ‡ โœ…
MobX ๐Ÿค“ โœ… ๐Ÿค โœ… ๐Ÿ†” โŒ ๐Ÿš‚ โœ… ๐Ÿฆฅ โœ… ๐Ÿ‘จโ€๐Ÿ’ป โœ… ๐Ÿš• โœ… ๐Ÿฆบ โœ… ๐ŸŒ‹ โœ… ๐Ÿ—ป โŒ ๐Ÿ‘ป โŒ ๐ŸŠโ€โ™‚๏ธ โŒ
ChronoGraph ๐Ÿง โŒ ๐Ÿค โœ… ๐Ÿ†” โŒ ๐Ÿš‚ โœ… โฐ โŒ ๐Ÿ‘จโ€๐Ÿ’ป โœ… ๐Ÿš• โœ… โฎ โŒ ๐ŸŒ‹ โœ… ๐ŸŒŒ โœ… ๐Ÿคผโ€โ™‚๏ธ โŒ ๐ŸŠโ€โ™‚๏ธ โŒ
Reatom ๐Ÿคฏ โŒ ๐Ÿค โœ… ๐Ÿ†” โŒ ๐Ÿš‚ โœ… ๐Ÿฆฅ โœ… ๐Ÿงจ โŒ ๐Ÿฆฝ โŒ โฎ โŒ ๐Ÿšซ โŒ ๐Ÿ—ป โŒ ๐Ÿคผโ€โ™‚๏ธ โŒ ๐ŸŠโ€โ™‚๏ธ โŒ
Effector ๐Ÿคฏ โŒ ๐Ÿค โœ… ๐Ÿ†” โŒ ๐ŸฅŒ โŒ ๐Ÿ” โŒ ๐Ÿ“ฐ โŒ ๐Ÿฆฝ โŒ ๐ŸŽฒ โŒ ๐Ÿ’ค โŒ ๐ŸŒŒ โœ… ๐Ÿ‘ป โŒ ๐ŸŠโ€โ™‚๏ธ โŒ
RxJS ๐Ÿคฏ โŒ ๐Ÿค โœ… ๐Ÿ‘ฏโ€โ™€๏ธ โŒ ๐ŸฅŒ โŒ ๐Ÿ” โŒ ๐Ÿ“ฐ โŒ ๐Ÿฆฝ โŒ โ›” โŒ ๐Ÿšซ โŒ ๐Ÿ—ป โŒ ๐Ÿ‘ป โŒ ๐ŸŠโ€โ™‚๏ธ โŒ

There are two main camps here: โ€œImperative Reactive Programmingโ€ and โ€œFunctional Reactive Programmingโ€. As you can see, the currently fashionable functional approach is not very practical, unlike the more old-school imperative approach.

Itโ€™s also worth noting that RxJS itself is not about reactivity. It is fundamentally about controlling the flow of execution. However, with its help it is possible to describe invariants connecting states, and then we get a reactive system.

Many thanks to the library authors for their help in preparing this table. Write to me if you want to add yours to the comparison. I will try to keep this sign up to date, if the community, of course, helps me keep track of all the news.

$mol_wire

The leader, of course, turned out to be $mol_wire, which I implemented after this analysis. There is a detailed article about why it is good and why it works the way it does, which I highly recommend reading, because at the end there will be some pleasant surprises waiting for you..

Designing an ideal reactivity system

Its predecessor $mol_atom2 had all the same qualities, so in the $mol framework we quite quickly completely moved to the new implementation of reactivity. And by the way, about frameworks...

Reactive frameworks

Lib Style Watch Dupes Origin Tonus Order Flow Error Cycle Depth Atomic Extern
Vue ๐Ÿ˜ต โŒ ๐Ÿค โœ… ๐Ÿ†” โŒ ๐Ÿš‚ โœ… ๐Ÿฆฅ โœ… ๐Ÿ‘จโ€๐Ÿ’ป โœ… ๐Ÿš• โœ… ๐Ÿฆบ โœ… ๐ŸŽฐ โŒ ๐Ÿ—ป โŒ ๐Ÿ‘ป โŒ ๐ŸŠโ€โ™‚๏ธ โŒ
Ember ๐Ÿค“ โœ… ๐Ÿ”Ž โŒ ๐Ÿ†” โŒ ๐Ÿš‚ โœ… ๐Ÿฆฅ โœ… ๐Ÿ‘จโ€๐Ÿ’ป โœ… ๐Ÿš• โœ… ๐ŸŽฒ โŒ ๐ŸŒ‹ โœ… ๐Ÿšง โŒ ๐Ÿ‘ป โŒ ๐ŸŠโ€โ™‚๏ธ โŒ
Solid ๐Ÿง โŒ ๐Ÿค โœ… ๐Ÿ†” โŒ ๐Ÿš‚ โœ… ๐Ÿ” โŒ ๐Ÿ‘จโ€๐Ÿ’ป โœ… ๐Ÿš• โœ… โ›” โŒ ๐Ÿšซ โŒ ๐Ÿ—ป โŒ ๐Ÿ‘ป โŒ ๐ŸŠโ€โ™‚๏ธ โŒ
React ๐Ÿง โŒ ๐Ÿ”Ž โŒ ๐Ÿ‘ฏโ€โ™€๏ธ โŒ ๐ŸฅŒ โŒ โฐ โŒ ๐Ÿ‘จโ€๐Ÿ’ป โœ… ๐Ÿฆฝ โŒ โ›” โŒ ๐ŸŒ‹ โœ… ๐Ÿšง โŒ ๐Ÿ‘ป โŒ ๐Ÿ‡ โœ…
Angular ๐Ÿง โŒ ๐Ÿ”Ž โŒ ๐Ÿ†” โŒ ๐ŸฅŒ โŒ โฐ โŒ ๐Ÿ‘จโ€๐Ÿ’ป โœ… ๐Ÿš• โœ… ๐ŸŽฒ โŒ ๐ŸŽฐ โŒ ๐Ÿšง โŒ ๐Ÿ‘ป โŒ ๐ŸŠโ€โ™‚๏ธ โŒ
Svelte ๐Ÿง โŒ ๐Ÿ”Ž โŒ ๐Ÿ†” โŒ ๐ŸฅŒ โŒ โฐ โŒ ๐Ÿ‘จโ€๐Ÿ’ป โœ… ๐Ÿš• โœ… โ›” โŒ ๐Ÿšซ โŒ ๐ŸŒŒ โœ… ๐Ÿ‘ป โŒ ๐ŸŠโ€โ™‚๏ธ โŒ

Analysis of frameworks in terms of reactivity is somewhat arbitrary. It usually manifests itself in two aspects: invariants between the states of one component, and the connection between the state of one component and the parameters of another.

As you can see, proceduralism is more popular here, which is also not the most practical approach. And the most practical thing here is Vue. Only $mol is cooler than it, but there is no point in considering it separately as a framework, because it simply uses the $mol_wire library as a circulatory system, and we have already analyzed it earlier.

It is important to note that you should not blindly trust these plates, because they are compiled manually. I, of course, tried to reflect everything as accurately as possible, but I could have messed up somewhere. That's why..

More about Reactivity

Artyom (author of Reatom) has an interesting project to classify state managers using tests. This is a slightly broader topic, since, for example, Redux is a state manager, but it is not reactive. This is just a transactional change in the state tree and thatโ€™s it, no cascading invariants between states. If you are interested in this topic, then get involved in writing tests - it will be useful for the entire community.

In a series of articles from Chris Garrett, you can see how the reactive model was rethought in the Ember framework. I have left a link to the most relevant one.

An in-depth article by Kris Kowal looks at the issue of reactivity from a different perspective. In my opinion, he is wrong, but you can read it to broaden your horizons. But the one I agree with is the author of SolidJS - Ryan Carniato, who talks about how to make a practical reactive system.

Finally: a couple of my speeches examining the advantages of ORP and the mechanics of implementing asynchronous invariants will help to delve even deeper into the topic.

More from Me

For 10 years now I have been actively sharing knowledge, ideas, and open source. And each of my materials is an original, and sometimes radical, idea, tested in practice. So it's worth checking out all of this, even if you don't plan to use it.

Contribution

This is the fifth year that I have stopped working, but am engaged in computer science and open-source projects. So if you find my work useful, you can thank me by donating or even patronizing me..

So, you can help simply by sharing a link to my articles with your friends. I don't have a very large media resource, although my articles could be useful to many developers.

We, in the HyperDev Guild, have many interesting projects that should soon change the world. So the most valuable contribution you can make is not even money, but participation in projects.

We want to create an ecosystem of tightly integrated web services using the most advanced and cheap technologies. And to displace the open source of the current Internet giants. Join our little revolution!

Last Words

That's all for sure now. Thank you for your attention. I hope this analysis was useful to you.

And now, letโ€™s boost our reactive engines and fly into a bright future!

Top comments (1)

Collapse
 
gooseob profile image
Edvin Dulko

ะฏัะฝะพ ะพะฟัั‚ัŒ ั€ะตะบะปะฐะผะฐ ะผะพะปะฐ