JavaScript: From Closures to Autonomous Systems

Chapter 13. From MVC to the Reactive Graph

Part IV. Distributed Reactivity9 min

13

From MVC to the Reactive Graph

The most successful reactive program in history was written long before the word existed. In a spreadsheet you type a number into one cell and into another a formula that refers to it, and when you change the number, the formula recalculates by itself. The accountant does not tell the spreadsheet which cells to update; the spreadsheet knows without them. Interface programmers looked at accountants with envy for decades, because for them everything was the other way round.

In the classic scheme, where data is separated from presentation and controller code sits between them, every change in the data had to be carried to the screen by hand. The user adds an item to the cart, and the programmer updates the counter in the header, recalculates the total at the bottom, enables the checkout button and hides the «cart is empty» label. In the jQuery days this looked like a chain of commands of the form «find the element and change its text». Forget one of them, and the screen shows one thing while the data holds another. The bigger the screen, the more places you must not forget.


In the early 2010s Meteor proposed doing to the interface what a spreadsheet does to cells, and did it with a mechanism called Tracker. It worked like this. The programmer writes a function that displays something and hands it to Tracker:

Tracker.autorun(() => {
  counter.textContent = Session.get('cartSize');
});

Tracker runs the function and, while it runs, records which reactive sources it read, in our case Session.get(’cartSize’). When a source changes, Tracker runs the function again and records anew what it read this time, because with a different value the function might have taken a different branch. The programmer never said what depends on what. They wrote what to display, and who must update whom was worked out by the runtime by peeking at what the function reads. Peeking, as we saw in the second part, is its favourite way of learning about a program.

Tracker turned out to have a direct heir. Evan You started Vue before he joined Meteor, but he worked there for several years as one of the core developers, and in the description of the @vue/reactivity package, which is responsible for reactivity in Vue, Meteor Tracker comes first in the list of prior art. Evan You himself says in his lectures that it is one and the same basic dependency-tracking scheme shared by Knockout, Meteor, Vue and MobX. When someone on the Meteor forum ported Tracker onto Vue’s observation mechanism, he wrote that the difference between them was almost entirely in the function names.

Free, one step away

The rest of the book is free

Leave your email and I will send a link to confirm it. Once you click it, every chapter opens here, on this device, for a year. When a new book comes out I will write to this address; reply “stop” to any email and I will not write again.

Have an INITE account? Sign in and the book opens without an email.

I do not charge for the book. If you would like to support it, you can donate from any chapter.

How your email is stored