Skip to main content
🧙‍♂️ Kris Kowal 🐘 🐙 🧛 📌 📬 Book Store

Functional Reactive Bindings, Part 4: What I Would Do Over

This is the last of four parts on Functional Reactive Bindings. Part 1 was the language, part 2 the combinators, and part 3 binders and how FRB met Montage, the DOM and Gutentag. This one is about what I would do differently, with FRB and Gutentag, if I did them over.

Shims #

Shimming JavaScript methods that later proved to be out of alignment with the eventual JavaScript specification is my greatest regret for Collections and FRB.

Collections defined its methods on Array.prototype whether or not the language already had them, and replaced what it found. Version 1 gave arrays a find(value, equals) that returned an index (shim-array.js). The find that arrived in ES2015 takes a predicate and returns the value. On any page that loaded Collections, find disagreed with the language and with every other library that trusted the language.

I would not shim any method that is not already standard. If I did shim, I would let the first shim win: install a method only where there is none, and never replace one. A shim like that can run safely inside a hardened JavaScript environment, where the shared prototypes are frozen and a shim that insists on writing to them throws.

I would not augment Object.prototype at all. If I did augment the built-ins, I would name the method with a registered symbol, from Symbol.for, which libraries can coordinate on in user space and which cannot be confused for a future well-known symbol like Symbol.iterator. And I would provide a module that calls through that symbol, in the style of a ponyfill: a function that takes the array as its first argument, so that code depends on the module and not on the prototype.

That would certainly be the case for Array.prototype.swap, which is necessary for large array splices. splice takes the values to insert as a variadic argument list, and the engine flattens that list onto the stack, so a splice tends to bump into a RangeError somewhere in the region of 10,000 values. swap(start, length, plus) takes the values as an array and splices them in batches (shim-array.js). FRB already carries the ponyfill half of this: a function, swap(array, start, minusLength, plus), backported from Collections 2, which the map combinator in part 2 uses on the arrays it keeps for itself. I would have every array go that way.

I would probably do the same for Array.prototype.set, which is necessary for observable replacement at an index, since an array cannot hear array[3] = x.

I would probably also use Object.isFrozen checks a great deal, to avoid installing observers on immutable arrays and objects. A frozen value will never change, so there is nothing to hear, and installing an observer would mean changing a prototype or a property that a frozen value refuses to change anyway.

I would probably not reach for Proxy. I might instead overlay an optional IDL for data types, from which to generate sealed and observable object shapes: every property known in advance, each with its own observable accessor, and nothing to patch afterward.

A purpose-built language #

I would dispense with using the HTML parser for Gutentag and just build something purpose-built.

Gutentag reads its tag modules with an HTML parser, and an HTML parser has opinions. The HTML5 parser carries tags it does not know out of a <table>, so a <repeat> cannot stand between rows, and Gutentag had to accept XML modules to get around it. HTML throws away nothing between tags, and a template wants to, so Gutentag trims the space between tags and needs an <sp> tag to keep it. A language built for declaring components would not have to work around a parser built for documents.

Compile in development, bundle for production #

I would continue to compile on the fly during development, on the client side, and continue to bundle for production, with all that implies for the sophistication of the module loader.

That much Gutentag already did. Its System loader translated HTML modules to JavaScript in the browser as it loaded them, so refreshing the page was the whole development cycle, and its bundle command translated every module ahead of time and rolled them up with a very small loader for production. The loader had to know how to find and run a translator for each kind of module, in the browser and at build time alike.

FRB in pieces #

And then I would also break FRB down into its own language that conditionally imports only the combinators it needs, with all the complications for the module loader that implies.

FRB comes with a complete runtime for every expression and operator the language supports. observers.js requires sorted arrays, sorted sets, maps, sets and heaps whether or not any binding sorts, groups, or takes a minimum, and the compiler’s table of makers from part 2 is how every one of them gets into every page. I wrote as much on codi.sh in 2015: the next rendition of reactive bindings would benefit from a JavaScript translator that only links the change operators, like map or filter, that an application needs.

The compiler from syntax trees to observers already knows, node by node, which maker each expression needs. A translator that runs that walk at build time instead of at run time can write out the imports instead of the closures, so a page that only maps and sums carries a map and a sum. The complication is that the set of combinators a page needs is only known once all of its binding sheets are loaded, so the loader has to resolve the language’s dependencies as well as JavaScript’s.

A language like CSS #

I would make FRB a language like CSS.

Developing FRB for Montage led me to conclude, as I put it on codi.sh, that bindings should be expressed in a way similar to CSS: a language that applies blocks of bindings to components based on their identifier. The grammar already had the beginnings of one, in 2013, in the sheet rule I showed in part 1:

@list < 'ui/list' {
    content <- @owner.items;
    on action -> @owner.handleAction
}

A block for each component label, with bindings, assignments and event handlers inside. FRB parsed sheets, but nothing compiled them. In the talk, the same idea appeared as a <script language="bindings"> at the top of a page, binding a TodoMVC’s checkboxes to the iterations of a repetition.

The System loader could hook arbitrary languages, load their dependencies, and translate them to JavaScript, and the next generation of FRB would have compiled a CSS-like language to bindings that way. Although bindings apply mostly to the properties of components, they could also manipulate style directly, possibly driving a CSSOM runtime, scoping style changes to an instance of a component.

One binary #

I might go so far as to make the Gutentag language compile the declarative component definitions, the style sheets, the binding sheets, and the data sources together, for a single HTTP server binary that hosts all of the static and dynamic content. It would progressively enhance static data to dynamic data based on database-side observability, such that an application generates both the optimized static content and gracefully upgrades to a reactive interface, with snapshots for edit contexts. It would be a piece of software that captures generations of web optimization experience and leaves very little for the developer to recapitulate.

FRB’s bindings live in the memory of one process. Within the memory of one process running on an event loop, it is always possible to have a consistent model at the end of every event, and there is no value in the compromise of eventual consistency unless state is spread across multiple processes. That is why FRB is synchronous, and effects changes recursively, like a Prolog. When I cataloged the pieces of a reactive system on codi.sh, the slot for asynchronous change propagation, for operational transforms and CRDTs, was a row of question marks.

Database-side observability is that slot filled. If the data source can report changes the way Collections reports range changes, as what was added, what was removed, and where, then the combinators of part 2 do not care whether a change came from a click or from a row committed on the server. The same expression that renders a page once, as static HTML, can keep it live once the page has a connection, and a snapshot gives an editor a consistent model to bind to while the data moves underneath.

What stays #

Most of what I would keep is in the contract from part 2. An observer takes a callback that returns a canceler, and returns a canceler of its own. Arrays are emitted once and then changed in place. A binding is an equation. Those held up, through FRB, through POP Observe and Gutentag, and through the rest of the patterns in the talk. What I would change is everything around them: the shims that got ahead of the language, the parser that was not mine, the runtime that came all at once, and the line between a page that is rendered and a page that is live.


Functional Reactive Bindings:

  1. The Language
  2. The Combinators
  3. Binders and Integration
  4. What I Would Do Over