Build A Frontend Web Framework
Build A Frontend Web Framework
MEAP V06
1. MEAP_VERSION_6
2. Welcome
3. 1_Are_frontend_frameworks_magic_to_you?
4. 2_Vanilla_JavaScript—like_in_the_old_days
5. 3_Rendering_and_the_virtual_DOM
6. 4_Mounting_and_destroying_the_virtual_DOM
7. 5_State_management_and_the_application’s_lifecycle
8. 6_Publishing_and_using_your_framework’s_first_version
9. 7_The_reconciliation_algorithm:_diffing_virtual_trees
10. 8_The_reconciliation_algorithm:_patching_the_DOM
11. Appendix._Setting_up_the_project
MEAP VERSION 6
Welcome
Thank you for purchasing the MEAP for Build a Frontend Web Framework
(From Scratch). I hope you enjoy reading this book as much as I’ve enjoyed
writing it. I’m looking forward to you getting the same feeling of
accomplishment as I did when you see your own frontend framework—
written from the ground up—power web applications. There’s something
magical about building your own tools and seeing them working.
Throughout the book, you’ll learn the most important concepts behind what
makes frontend frameworks such useful tools, but not through lots of theory,
but by writing all the code yourself. To make the most out of this book, I’ll
assume that you’ve got a decent understanding of JavaScript, Node JS and
the Document API in the browser.
After just two chapters (chapters three and four) you’ll have a working
framework, a very simple one, but a framework that you can use in small
applications nevertheless. From there onwards, you’ll keep adding
improvements to it, such as a Single Page Application (SPA) router that
changes the page without reloading it, or a Webpack loader to transform
HTML templates into render functions.
By the end of the book, you’ll have written a pretty decent framework of your
own. Not only can you use this framework for your own projects (something
that’s extraordinarily satisfactory), but you’ll share it with the rest of the
world via NPM. You heard me well! You will be publishing it the same way
the big frameworks get released. Sure, the framework won’t be even close to
competing with the big frameworks out there, but you’ll have a good
understanding of how they work that you can use to keep improving yours.
But more important than creating a framework that can compete is the
amount of fun, gratification and learning that you’ll get from working on this
project.
There’s one last thing I want to mention. I’m writing this book for you, so it
doesn’t matter how much effort I put into it if my explanations aren’t clear
enough for you to follow. If you get lost, it’s definitely not because you don’t
know enough, it’s because I’m not expressing my thoughts as clearly as I
could. So, if you do get lost or think I could be explaining things in more
detail, I’d love to hear from you. Conversely, if you think I spend too much
time explaining things you already know, also let me know. You, as a MEAP
reader, have a very important role in making this book useful for the readers
that’ll come after you. Oh! and if you’re enjoying the book as is, please do
tell me as well. Your feedback is essential in developing the best book
possible, so please visit the liveBook's Discussion Forum and leave your
comments.
In this book
How you ever wondered how the frontend frameworks you use work
internally? Are you curious about how they decide when to re-render a
component, and how they do to only update the parts of the DOM that
change? Isn’t it interesting how a single HTML page can change its content
without reloading? That the URL in the browser’s address bar changes
without requesting the new page to a server? The inner workings of frontend
frameworks are fascinating, and there is no better way to learn about them
than by building one yourself, from scratch. But why would you want to learn
how frontend frameworks work? Is’t it enough to just know how to use them?
—you might ask.
Good cooks know their tools; they can use knives skillfully. Great chefs go
beyond that: they know the different types of knives, when to use each one,
and how to keep their blade sharp. Carpenters know how to use a saw to cut
wood, but great carpenters also understand how the saw works and can fix it
in case it breaks. Electrical engineers not only understand that electricity is
the flow of electrons through a conductor, but also have a deep understanding
of the instruments they use to measure and manipulate it; for example, they
could build a multimeter themselves. If their multimeter breaks, they can
disassemble it, figure out what’s wrong, and repair it.
Frameworks and libraries are different. When you use a library, you import
its code and call its functions. When you use a framework, you write code
that the framework executes. The framework is in charge of running the
application, and it executes your code when appropriate trigger happens.
Conversely, you call the library’s functions when you need them. Angular is
an example of a frontend framework, whereas React claims to be a library
that you can use to build UIs.
Let me tell you a little story about a personal experience to illustrate this.
1.1.1 "Do you understand how that works?"
When I was a little kid I went to one of my cousins' house to hang out. He
was a few years older than me and a handyman. His cabinets were full of
cables, screwdrivers, and other tools, and I’d spend hours just observing how
he fixed all kinds of appliances. I remember once bringing a remote control
car with me so we could play with it. He stared at it for some time, then asked
me a question that got me by surprise: "Do you understand how this thing
works?" I didn’t; I was just a kid with zero electronics knowledge. He then
said, "I like to know how the stuff I use works, so what do you say we take it
apart and see what’s inside?" I sometimes still think about that.
So now, let me ask you a question similar to that my cousin asked me: you
use frontend frameworks every day, but do you really understand how they
work? You write the code for your components, then hand it over to the
framework for it to do its magic. When you load the application into the
browser, it just works. It renders the views and handles user interactions,
always keeping the page updated (in sync with the application’s state). For
most frontend developers—and this includes me from years ago—how this
happens is a mystery. Is the frontend framework you use a mystery to you?
Sure, most of us have heard about that thing called the "virtual DOM," and
that there needs to be a "reconciliation algorithm" that decides what is the
smallest set of changes required to update the browser’s DOM. We also know
that single-page applications (SPAs for short) modify the URL in the
browser’s address bar without reloading the page, and if you’re the curious
kind of developer, you might have read about how the browser’s history API
is used to achieve this. But do you really understand how all of this works
together? Have you disassembled and debugged the code of the framework
you use? Don’t feel bad if you have not; most developers haven’t, including
some very experienced ones. This reverse-engineering process isn’t easy; it
requires lots of effort (and motivation).
The framework we’ll build borrows ideas from a few existing frameworks,
most notably Vue, Mithril, Svelte, React, Preact, Angular and Hyperapp. Our
goal is to build a framework that’s simple enough to understand, but that at
the same time includes the typical features you’d expect from a frontend
framework. I also wanted it to be representative of some of the most relevant
concepts that are behind the source code of the most popular frameworks.
For example, not all frameworks use the virtual DOM abstraction (Svelte in
particular considers it to be "pure overhead," and the reasons are simply
brilliant—I recommend you read their blog post), but a big portion of them
do. I chose our framework to implement a virtual DOM so that’s
representative of the framework you’re likely using today. In essence, I chose
the approach that I thought would result in the most learning for you, the
reader. I’ll be covering the virtual DOM in detail in chapter 3, but in a
nutshell, it’s a lightweight representation of the DOM that’s used to calculate
the smallest set of changes required to update the browser’s DOM. For
example, the following HTML markup:
<div class="name">
<label for="name-input">Name</label>
<input type="text" id="name-input" />
<button>Save</button>
</div>
would have a virtual DOM representation like that in figure 1.1. (Note that
the saveName() event handler in the diagram doesn’t appear in the HTML
markup: event handlers are typically not shown in the HTML markup, but
added programmatically.) I’ll be using these diagrams a lot throughout the
book to illustrate how the virtual DOM and the reconciliation algorithm
works. The reconciliation algorithm is the process that decides what changes
need to be made to the browser’s DOM to reflect the changes in the virtual
DOM, which is the topic of chapters 7 and 8.
Your framework will have some shortcomings that make it not an ideal
choice for complex production applications, but definitely fit for your latest
side project. For example, it’ll only support the standard HTML namespace,
which means that SVG won’t be supported. Most of the popular frameworks
support this namespace, but we’ll leave it out for simplicity. There are other
features that—for the sake of keeping the book to a reasonable size and the
project fun to build—we’ll leave out as well. For example, we’ll leave out
component-scoped CSS support, which is a feature that’s present in most of
the popular frameworks, one way or another.
1.2.1 Features
Your framework will have the following features, which you’ll build from
scratch:
As you can see, it’s a pretty complete framework. It’s not a full-blown
framework like Vue or React, but it’s enough to understand how they work.
And the neat thing is that you’ll build it line by line, so you’ll understand
how it all fits together. I’ll use lots of diagrams and illustrations to help you
understand the concepts that might be harder to grasp. I recommend you to
write the source code yourself as you read the book. Try to understand it line
by line, take your time, debug it, and make sure you understand the decisions
and trade-offs we make along the way.
Figure 1.2 shows the architecture of the framework we’ll build. It includes all
the parts of the framework you’ll implement, and how they interact with each
other.
Stateful components
Next, you’ll allow components to have their own state, which makes state
management much simpler. The application will no longer need to hold the
entire state; it’ll be split among components instead. Pure functions will turn
into classes that implement a render() method, and each component will be
its own little application with its own lifecycle.
Lifecycle hooks
You’ll then add lifecycle hooks to the components, which make possible
executing code at certain moments in time, like when the component is
mounted into the DOM. An example of using a lifecycle hook is to fetch data
from a remote server when the component is mounted.
You’ll also add support for slots, which allow you to render content inside a
component, making it more reusable and customizable.
SPA router
You’ll then implement a router, which allows the application to navigate
between different pages without reloading the page.
Note
Most frameworks don’t come with a router bundled; it’s typically a plugin or
a separate library that you have to install in your project. For the sake of
learning, you’ll include our router in the framework itself.
HTML templates
Finally, you’ll add a Webpack loader module that reads HTML templates and
compiles them into JavaScript render functions, making it more convenient to
write our components' view in HTML. So, instead of writing this:
function render() {
return h('div', { class: 'container' }, [
h('h1', {}, ['Look, Ma!']),
h('p', {}, ["I'm building a framework!"])
])
}
Isn’t that much nicer? Don’t worry if you don’t understand what the h() call
is; we’ll devote the whole chapter 3 to explaining it. In a nutshell, it’s a
function that creates virtual DOM nodes.
SSR requires a server to run, but serving static files is generally cheaper than
rendering pages as users request them. As you know, all good things come
with a price.
Now that you know what you’ll build and you have a plan, let’s take a quick
look at how frontend frameworks work.
Another benefit of using Node JS is that all Node JS projects follow the same
structure and conventions, something that helps other developers to
understand how to work with our codebase.
Once the developer is satisfied with the application, the code needs to be
bundled into fewer files than were originally written, so the browser can load
the application using fewer requests to the server. The files can also be
minified, that is, made smaller by removing whitespace and comments, and
renaming variables to shorter names. This process of turning the application’s
source source code into the files that are shipped to the users is called
building.
Note
There are many different ways an application can be built, resulting in a wide
variety of bundle formats. Here, I’ll explain a build process that encapsulates
some of the most common practices.
So, a typical build process results in four (or more, in the case of larger apps)
files:
These files are uploaded to a server, and the application is ready to be served
to the user. When a user requests the website, the HTML, JS, and CSS files
are statically served.
Note
When a file is statically served, the server doesn’t need to do anything before
sending it to the user. The server simply reads the file from disk and sends it.
In contrast, when the application is rendered on the server, the server
generates the HTML file before sending it to the user’s browser.
Figure 1.4 shows a diagram of the build process I just described. Note that, a
typical build process is more complex than the one shown in the figure, but
this is enough to understand the concepts. I’ve included a step that transforms
the JavaScript code. This is a generic step that refers to any transformation
that needs to be done to the code before it’s bundled, like for example
transpiling it using Babel or TypeScript.
When the user navigates to the application by writing its URL, the browser
requests the page’s HTML file (1), which is returned by the server (2), as
illustrated in figure 1.5.
The browser loads the JavaScript and CSS files referenced in the HTML file
(3) and parses the JavaScript code. This is depicted in figure 1.6.
Figure 1.6. The browser loads the JavaScript and CSS files referenced in the HTML
The browser is still blank at this point. It has rendered the HTML file, but this
file’s <body> element is mostly empty (except maybe for a <div id="app">
tag that some frameworks use to render the app). The view of an SPA is
created dynamically by the framework’s JavaScript code, which is what
happens next.
The framework JavaScript code (living in the vendors bundle) finds the
components defined in the application’s code that need to be rendered (4) and
creates the application’s view (5). This is depicted in figure 1.7. This initial
rendering is called mounting the application.
Figure 1.7. The framework creates the application’s view using the Document API
To create the application’s HTML programmatically, the framework uses the
Document API. The Document API allows the creation of HTML elements
programmatically, using JavaScript. Let’s take a quick detour to see how this
works.
For example, given an empty HTML <body> element like the following:
<body></body>
Going back to how SPAs work: what happens when the user interacts with
the application?
When the user interacts with the application (6), the framework handles the
event and updates the view accordingly. This is depicted in figure 1.8. The
framework handles the event from the browser by executing the event
handling code defined in the application’s code (7) and then updating the
view to reflect the changes in the application’s state (8).
A single change made to the DOM by the framework is called a patch. The
process of updating the view to reflect the changes in the application’s state is
called patching the DOM.
To better understand why the changes to the DOM are expensive in terms of
computation, I recommend you read web.dev/critical-rendering-path-render-
tree-construction. This article explains how the browser renders the
document, and gives you an overview of everything that happens under the
hood.
Svelte understands the ways the view can be updated at compilation time, and
produces JavaScript code to update the exact parts of the view that need to be
patched for each possible state change. Svelte is remarkably performant
because it does only the least amount of work in the browser to update the
view.
The last step we need to cover is how the framework handles navigation
between routes.
When the user clicks a link (9), the framework’s router prevents the default
behavior of reloading the page, and instead renders the component that’s
configured for the new route (10 and 11). The router is also in charge of
changing the URL (12) to reflect the new route. This is depicted in figure 1.9.
Figure 1.10. The complete flow of a single-page application rendered in the browser
Now that you know how single-page applications work, let’s compare them
with an application that’s server-side rendered.
When the user types the application’s URL into their browser, the browser
asks the server for the HTML file (1). The server sends back a complete page
that is created each time someone requests it. To create the page, the server
uses the application’s router to figure out which components to show based
on the requested route and instantiates them (2). Then, each component loads
data from other servers or databases and executes its mounting code before
being rendered. Finally, the components are turned into HTML (3) and sent
to the user (4). This is illustrated in figure 1.11.
The HTML document instructs the browser to load the application JavaScript
files and CSS style sheets (5)—the same as in the case of an SPA. Once the
JavaScript code is parsed, the framework code needs to connect the existing
HTML—produced in the server—to the component’s virtual DOM, as well
as attach event handlers (6). This is called the hydration process, and it’s
depicted in figure 1.12.
When the user interacts with the page (7), the framework’s event handlers are
triggered (8), and—same as in the SPA case—the framework patches the
parts of the HTML that need to be updated (9). All of this happens in the
browser, so the server isn’t involved in this process, as you can appreciate in
figure 1.13.
When the user clicks a link (10), the URL is changed by the browser (the
framework doesn’t do anything in the browser side this time), and the page is
reloaded. A new HTML page is requested from the server, and the process
starts again from step 1.
Figure 1.15. The complete flow of a SSR application—rendered in the server and hydrated in the
browser
And this is how both single-page applications and server-side rendered
applications work. What about building a simple application yourself,
without using a framework?
1.4 Summary
Building a frontend framework from scratch is a great way to learn how
they work.
Frontend frameworks bundle the application’s code into a single
JavaScript file, the third party dependencies into another file, and the
CSS styles into yet another file. If the application is large, the
framework might split the application’s code into multiple bundles that
are loaded "lazily," that is, just as they’re needed.
The Document API allows the creation of HTML elements
programmatically, using JavaScript. The Document API is used by
frontend frameworks to create the application’s view.
Single-page applications consist on a single HTML file that’s loaded by
the browser and updated by the framework to reflect the application’s
state. When the route changes, it’s the framework changing the view.
The browser doesn’t reload the page.
The hydration process is the process of connecting the existing HTML
markup, rendered in the server, to the component’s state and event
listeners.
2 Vanilla JavaScript—like in the old
days
This chapter covers
Building an application using vanilla JavaScript and HTML
Programmatically creating DOM elements
Using the Document API to manipulate the DOM
In the old days (I’m not that old, it’s just that technology evolves fast), we’d
write applications using only vanilla JavaScript and HTML. JQuery was the
best we had: it provided a nice API to interact with the DOM, hiding away
the browser differences. But we’d still have to write code down to the level of
working with the DOM, and to be fair, it wasn’t that bad. That is, until we
used our first modern frontend framework (it was Angular, in my case). Now
there’s no going back; we’ve been there; we know how much simpler it’s
become to write JavaScript applications.
In this chapter, you’ll do the cleaning yourself; that is, you’ll build a simple
application from scratch using only vanilla JavaScript and HTML. The
cleaning personnel—the existing frontend frameworks—will be on strike.
Despite the simplicity of the app, you’ll notice how the code operates at a low
abstraction level by directly manipulating the DOM, and it’s very imperative
in nature. You’ll need to write code explicitly to update the HTML document
with every change in the application state. It’s evident that not using a
framework will become a challenge as the complexity and size of an
application increase. However, the purpose of this chapter is for you to
realize this on your own by experiencing the process of creating an
application without framework support.
Important
Before you go any further, go to appendix A and follow the instructions to set
up the project where you’ll be writing the code. Bear in mind that appendix A
will be a little detour from the main topic of this chapter, but it’s necessary to
set up the project. When you finish and without further ado, let’s begin the
application you’ll be building in this chapter.
You can find the code you’ll be writing in this chapter in the GitHub
repository, inside the examples/ch02 directory:
https://github.com/angelsolaorbaiceta/fe-fwk-book/tree/main/examples/ch02
Main idea: keep a list of the things you need to do (to-dos) in a day.
A to-do can be marked as done, so it’s removed from the list.
A to-do can be modified, for the cases where the user makes a typo or
wants to change the description.
Vue.js implemented one, which can be found from the earliest versions,
inside the examples directory of the framework’s old repository:
github.com/vuejs/vue/tree/v0.7.0/examples/todomvc. React did as well, as
early as in its initial public release, v0.3.0, which can be found inside the
examples directory:
github.com/facebook/react/tree/v0.3.0/examples/todomvc. And one more
example is Mithril’s, which can be found in the examples directory of the
framework’s repository as well:
github.com/MithrilJS/mithril.js/tree/v1.0.0/examples/todomvc.
What you do next is talk with the design team to get the mockups for the
application. They love the idea—although they swear they’ve seen something
similar before—and they come up with a quick wireframe design that looks
like figure 2.1.
Figure 2.1. Wireframe design for the TODOs app
You show it to the client, and they love it. Time to get down to business.
You figure out that part of the HTML markup is static—it won’t change as
the user interacts with the application—and part of needs to be dynamically
generated, because it depends on the application’s current state. For example,
the list of to-dos will be programmatically generated from JavaScript because
we can’t know in advance what TODOs the user will write. In contrast, the
title "My TODOs," the input box where the user writes a new to-do, its label,
and the Add button will always be the same. Figure 2.2 shows the static and
dynamic parts of the application.
State
The state is the information that the application keeps track of that makes it
look and behave the way it does at a particular moment in time.
The application will look different when there are no to-dos than when there
are some, for example. This means that the list of to-dos is part of the
application’s state, so you’ll need an array of strings to keep track of the
existing to-dos. The strings represent the to-dos descriptions.
Last, before you start coding, you think about the application’s behavior.
Based on the requirements, the design, and a short conversation you had with
the user-experience specialist, you decide that the application will behave as
follows:
When the user writes a new to-do and clicks Add, the to-do is added to
the list of to-dos.
If the user presses the Enter key while the input field is focused, the to-
do is appended to the list of to-dos as well.
Don’t allow the user to add to-dos that are shorter than three characters.
To edit a to-do, the user has to double-click on it.
If the user discards the changes by clicking the Cancel button, the to-do
is restored to its previous state; the changes are lost.
When a to-do is marked as done, it’s removed from the list of to-dos.
With this in mind, it’s time to start writing the code. Let’s start by setting up
the project.
You’ll write the vanilla JavaScript version of the TODOs application inside
the examples directory in your project. (Make sure you’ve completed the set
up from appendix A.) First, create a new folder inside the examples directory
for this chapter: ch02.
$ cd examples
$ mkdir ch02
You want to move your terminal’s working directory back to the project’s
root directory, so you can run the serve:examples script from there:
$ cd ..
$ npm run serve:examples
Your browser should open the examples directory and show the ch02
directory; click on it. Then click on the todos.html file to open it. It should
show an empty page because you haven’t written any HTML markup yet.
Let’s write the static part of the HTML markup now.
The first thing you need to do is load the todos.js file as an ES module inside
the <head> of the document. This is done by adding the type="module"
attribute to the <script> element. ES modules are supported by all modern
browsers, and a neat feature of them is that they are deferred by default,
which means that it won’t start executing the JavaScript code until the HTML
document has been parsed. That’s why we can load the JavaScript file at the
top of the document, the <head>, and still be sure the HTML markup is
already available when the JavaScript code starts executing.
Tip
Read more about how ES modules are different from classic scripts in the
browser in V8’s blog at v8.dev/features/modules. It’s a good read that
clarifies a lot of concepts around ES modules and their behavior in the
browser, written by the people who work on V8 itself.
Then, you’ll add the input field, its label, and the Add button inside a <div>
element. Note that we need to add id attributes to the input field and the
button so we can reference them from the JavaScript code. The same goes for
the <ul> element that will contain the to-dos, which is below the <div>
element and is empty to start with. The to-dos will be rendered
programmatically by the JavaScript code, as <li> elements inside the <ul>
element.
Open the todos.html file and add the markup in listing 2.1.
Listing 2.1. The static HTML markup for the TODOs app (todos.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>My TODOs</title>
<body>
<h1>My TODOs</h1>
<div> #2
<label for="todo-input">New TODO</label>
<input type="text" id="todo-input" />
<button id="add-todo-btn" disabled>Add</button>
</div>
<ul id="todos-list"></ul> #3
</body>
</html>
If you now refresh the browser window, you should see something like
Figure 2.3.
Figure 2.3. The HTML markup for the TODOs app
Exercise 2.1
Add some CSS styles to the title, the input field, and the button. Here’re some
suggestions:
Use a nicer font, instead of the default one. You can choose one from
Google Fonts (they’re free to use), like Roboto and apply it to the
document.
Center the application in the middle of the page, horizontally.
Make the title bigger.
Place the input field’s label above the input field and make it italic.
Give the input and button some padding.
First of all, you want to define the application’s state, which is a list of to-dos
—an array of strings. You’ll add some to-dos already populated in the array,
so that when you open the page in the browser, you can see some to-dos
already rendered. Then you want to grab references to the DOM elements
that you need to interact with, using the document.getElementById()
function from the Document API.
Open the todos.js file and write the code in listing 2.2.
So far, your application doesn’t do anything when you type a new to-do
description in the input field and click the Add button. The to-do items in the
state aren’t rendered either. This is because you have neither added event
listeners nor written the code that renders the to-dos. Let’s fix that.
Now, you want to initialize the view of the application—that is, dynamically
generate the HTML markup that depends on the application’s state—and
attach event listeners to the DOM elements that need them. To initialize the
view, you iterate over the to-dos in the application’s state and render each
one using a function you’ll call renderTodoInReadMode(). Each element is
then append it to the <ul> element using the todosList element append()
method.
Rendering
To render means to transform some data into a visual representation;
something we can see.
In this context, when we render a to-do, what we’re doing is creating the
HTML elements that represent the to-do in our application.
After rendering the to-dos in read mode you need to add a few event listeners
to the DOM elements. First, you’ll add a listener on the <input> field’s
input event—fired every time the user types something in the input field.
This handler function should check if the input field has less than three
characters, in which case the button is kept disabled to prevent the user from
adding empty (or very short) to-do items. The button is enabled—by
removing the disabled attribute—when the to-do has at least three characters
(figure 2.4). If you remember from the HTML markup (see listing 2.1), the
Add <button> element is disabled by default.
Figure 2.4. The field is disabled when the input field has fewer than three characters
Then, you’ll add a listener on the <input> field’s keydown event, which fires
every time the user presses a key—any key. But you’re not interested in
responding to every key the user presses, only the "Enter" key is relevant. For
this, you want to check if the key pressed is "Enter", and if so, call a function
you’ll name addTodo(), which you’ll implement in a minute an will be used
to add a new to-do to the application’s state, and render it in the HTML.
Finally, you need a listener on the Add <button> element’s click event. The
event handler is the same as the one for the keydown event: it calls the
addTodo() function, clears the input field and disables the Add button
addTodoInput.addEventListener('input', () => { #2
addTodoButton.disabled = addTodoInput.value.length < 3
})
addTodoButton.addEventListener('click', () => { #4
addTodo()
})
// Functions
function renderTodoInReadMode(todo) {
// TODO: implement me!
}
function addTodo() {
// TODO: implement me!
}
Figure 2.5 shows a visual representation of the events you’ve added to the
static part of the HTML markup.
Add a CSS transition to the color property of the button when it’s enabled or
disabled. This will make the button’s color change smoothly when it’s
enabled or disabled.
You can use the transition CSS property. Go ahead and read about it
in the MDN documentation if you need a refresher.
You can use the :disabled pseudo-class to style the button when it’s
disabled.
You can use the :enabled pseudo-class to style the button when it’s
enabled.
The to-do items are inside an unordered list element (<ul>), so each to-
do should go inside a list item element (<li>).
The to-do itself is a simple text that you can render inside a <span>
element.
Then, the user should be able to mark a to-do as done, so you need a
button to do that.
Figure 2.6 depicts the HTML markup for a to-do in read mode.
Figure 2.6. A TODO in "read mode" is rendered as a <li> element containing a <span> element
with the to-do text and a button to mark it as done
The to-do description can be added as the textContent property of the
<span> element. We could have created a text node and appended it to the
<span> element (for example doing: span.append(todo)), but setting the
textContent is a bit more concise.
The <span> element needs to have a listener attached to its dblclick event
that’s going to replace the to-do in read mode with the "edit mode" version.
To accomplish this, you’ll call the replaceChild() method on the <li>
DOM node. This method removes the entire <li> element and its children
from the list of to-dos and renders the "edit mode" version of the to-do in its
place. The rendering is done by calling the renderTodoInEditMode()
function, which you’ll implement in the next section.
The replaceChild() method from the DOM API is used to replace a child
node of a DOM node (the one upon which the method is called) with another
node. It accepts two arguments:
Lastly, you want to attach an event listener to the <button> element’s click
event that’s going to remove the to-do from the list of to-dos. For that, you’ll
write a removeTodo() function that you’ll also need to fill in later on.
Now that you know what the plan is, fill in the renderTodoInReadMode()
function as in listing 2.4.
function renderTodoInReadMode(todo) {
const li = document.createElement('li') #1
todosList.replaceChild( #4
renderTodoInEditMode(todo),
todosList.childNodes[idx]
)
})
li.append(span)
return li
}
function removeTodo(index) {
// TODO: implement me!
}
Figure 2.7 shows a visual representation of the events you’ve added to the to-
dos in read mode.
Figure 2.7. The event listeners added to the to-dos in "read mode"
Let’s now implement the renderInEditMode() function.
The to-do in edit mode is also part of the unordered list of to-dos, thus it
should also appear inside a <li> element. But this time, the <li> element
should contain an <input> element instead of a <span> element, so that the
user can modify the to-do description. And instead of having one button, we
need two: one to save the changes and another to cancel them. Figure 2.8
shows the HTML markup for a to-do in "edit mode".
Figure 2.8. A TODO in "edit mode" is rendered as a <li> element containing an <input> element
with the to-do text and two buttons to save or cancel the changes
When the user clicks on the save button, a function that you’ll write later
named updateTodo() will modify the to-do description in the state, and
replace the to-do in edit mode with the read mode version (once the user is
done editing the to-do, we want them to see the updated version back in read
mode). When the user clicks on the Cancel button instead, you just need to
call the renderTodoInReadMode() function.
function renderTodoInEditMode(todo) {
const li = document.createElement('li') #1
return li
}
The code is very similar to the one for the read mode version of the to-do.
Figure 2.9 shows the events you’ve added to the to-dos in "edit mode".
The addTodo() function reads the description for the new to-do from the
<input> element’s value property, and pushes it into the array of to-dos.
Then it calls the renderTodoInReadMode() function to render the HTML for
the new to-do and appends it to the todosList element. Lastly, it clears the
<input> element’s value property so that the user can enter a new to-do
description, and disables the add button.
The removeTodo() function removes the to-do from the array of to-dos and
the <li> element from the document. To remove the <li> element from its
parent <ul>, it calls the remove() method on the target node, which you can
locate by index inside the childNodes array of the <ul> element.
The updateTodo() function needs two parameters passed to it: the index of
the to-do to update, and the new description for the to-do. The passed
description overwrites whatever is in the array of to-dos at the given index.
Then, using the renderTodoInReadMode() function, you can render the
HTML for the updated to-do, and finally replace the to-do at the given index
inside the todosList element’s childNodes array with the new HTML.
Listing 2.6 shows the code for the addTodo(), removeTodo() and
updateTodo() functions.
function addTodo() { #1
const description = addTodoInput.value
todos.push(description)
const todo = renderTodoInReadMode(description)
todosList.append(todo)
addTodoInput.value = ''
addTodoButton.disabled = true
}
function removeTodo(index) { #2
todos.splice(index, 1)
todosList.childNodes[index].remove()
}
If you refresh the page now, you should be able to add, remove and update
to-dos. Your application should look similar to Figure 2.10. You’ve written a
web application without a framework. It wasn’t that painful, was it?
Exercise 2.3
Imagine that the customer tells you that they don’t want "done" to-dos to be
removed from the list, but instead crossed out. Can you implement this
feature for them?
Exercise 2.4
The customer tried the application and loved it! But they realized they can
add the same to-do multiple times—unacceptable! Can you fix this bug, so
that if the user tries to add a to-do that already exists, the application doesn’t
add it again and warns the user about it?
Exercise 2.5
The customer came with a challenging request for you. To aid their users
with a hearing impairment, they’d like the application to read out loud a to-do
when it’s added to the list. Can you implement this feature for them?
Psss… here’s a little tip from a senior developer at your company. You might
want to read about the Web Speech API.
The first thing we want our framework to take care for us is the usage of the
Document API to create and manipulate the DOM; that’s the part that’s the
most burdensome to write. If we can abstract away the manipulation of the
DOM, we can focus on the application logic, which is what makes our
applications useful. Think about it: the time spent working on manipulating
the DOM doesn’t really add value to the final user. We need to do it for the
application to be interactive, but it’s not what the user cares about. A good
framework should allow us to forget about dealing with the DOM and focus
on the application logic. That’s exactly what we’ll do in the next chapter.
To add some nice CSS, first create a styles.css file in the examples/ch02
directory and load it from the HTML file’s <head> tag, like so:
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
Then, add the following CSS rules to the styles.css file to use the Roboto
font:
@import url('https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,400
body {
font-family: 'Roboto', sans-serif;
width: 500px;
margin: 2em auto;
}
Add some CSS rules to center the application, add some padding to the input
field and the button, and to place the label on top of the input field, displayed
in italic font:
body {
font-family: 'Roboto', sans-serif;
width: 500px;
margin: 2em auto;
}
label {
display: block;
font-style: italic;
margin-bottom: 0.25em;
}
h1 {
font-size: 3.5rem;
}
button,
input {
font-family: inherit;
padding: 0.25em 0.5em;
font-size: 1rem;
}
Feel free to experiment and add your own styles to the application. The result
of using the styles above is shown in Figure 2.12.
button:enabled {
transition: color 0.5s ease-out;
}
If you start typing in the input field, once you’ve typed three characters,
you’ll see how the button’s text color changes from gray to black. That’s the
transition you’ve just added. When you remove the characters from the input
field, the button’s text color smoothly transitions back to gray.
Then you need to change the code in all the places where the sate is used. For
example, inside the renderTodoInReadMode() you need to change the
following line:
span.textContent = todo
span.textContent = todo.description
You also need to edit the code in the addTodo() and updateTodo() functions.
I leave this part as an exercise for you to do.
Then, when the Done button is clicked, the removeTodo() function needs to
set the done property of the to-do to true instead of removing it from the
array:
function removeTodo(index) {
todos.splice(index, 1)
todosList.childNodes[index].remove()
todos[index].done = true
}
if (todo.done) {
span.classList.add('done')
}
if (!todo.done) {
span.addEventListener('dblclick', () => {
const idx = todos.indexOf(todo)
todosList.replaceChild(
renderTodoInEditMode(todo),
todosList.childNodes[idx]
)
})
}
li.append(span)
if (!todo.done) {
const button = document.createElement('button')
button.textContent = 'Done'
button.addEventListener('click', () => {
const idx = todos.indexOf(todo)
removeTodo(idx)
})
li.append(button)
}
return li
}
And add the following CSS rule to the styles.css file (or inside a <style> tag
in the document’s <head>) to cross out the text:
.done {
text-decoration: line-through;
}
To prevent the user from adding the same to-do multiple times, you need to
check if the to-do already exists in the array before adding it. The comparison
should be done against the trimmed and lowercased version of the to-do’s
description. In case the to-do already exists, you can show an alert to the
user:
function addTodo() {
const description = addTodoInput.value
if (todoExists(description)) {
alert('Todo already exists')
return
}
todos.push(description)
const todo = renderTodoInReadMode(description)
todosList.append(todo)
addTodoInput.value = ''
addTodoButton.disabled = true
}
function todoExists(description) {
const cleanTodos = todos.map((todo) => todo.trim().toLowerCase())
return cleanTodos.includes(description.trim().toLowerCase())
}
If you try now to add a to-do that’s already in your list, you’ll see an alert like
the one in Figure 2.14.
Figure 2.14. Alert shown when trying to add a to-do that already exists
2.3.5 Exercise 2.5
You can use the Web Speech API to read out loud the to-dos when they’re
added to the list. To read a text out loud, you need to create a
SpeechSynthesisUtterance object and set its text property to the text you
want to read. Then you need to set the voice property to one of the voices
available in the browser. You can get the list of voices available in the
browser using the speechSynthesis.getVoices() method. Last, call the
speakingSynthesis.speak() method to read the text out loud:
function addTodo() {
const description = addTodoInput.value
todos.push(description)
const todo = renderTodoInReadMode(description)
todosList.append(todo)
addTodoInput.value = ''
addTodoButton.disabled = true
readTodo(description)
}
function readTodo(description) {
const message = new SpeechSynthesisUtterance()
message.text = description
message.voice = speechSynthesis.getVoices()[0]
speechSynthesis.speak(message)
}
Enjoy!
2.4 Summary
Nothing prevents us from writing complete frontend applications
without a framework, but doing so can easily result in code that’s a mix
of application logic and DOM manipulation, that is, using the Document
API to modify the browser’s document.
Using vanilla JavaScript to write a fronted application, every event that
changes the state of the application forces us to write the code that
updates the DOM to reflect the new state. This code tends to be very
imperative, and verbose.
3 Rendering and the virtual DOM
This chapter covers
What the virtual DOM is
What problem the virtual DOM solves
Implementing functions to create virtual DOM nodes
Defining the concept of a stateless component
Manipulating the DOM results is very imperative code, that is, code that
describes how to do something, step by step. This is in contrast with
declarative code, which describes what to do, without specifying how to do it
—those details are implemented somewhere else. Also, manipulating the
DOM is a very low level operation, that is, it requires a lot of knowledge of
the Document API, and sits below the application logic. Contrast this with
higher level application code, which is framed in a language that is close to
the business, and anyone working in the project can—should—understand.
For the sake of productivity, the architect focuses on the "what" needs to be
built, and let the construction company take care of the "how" it’s built.
Similarly, we want the application developer to focus on the "what" (what the
view should look like), and let the framework take care of the "how" (how to
assemble the view using the Document API).
Important
You can find all the listings in this chapter in the listings/ch03 directory of
the book’s repository.
The code you write in this chapter is for the framework’s first version, which
you’ll publish in chapter 6. Therefore, the code in this chapter can be checked
out from the ch6 label:
$ git switch ch6
Figure 3.1. So far, all the code is written together as part of the application
What we want to accomplish in this chapter is separating the code that
describes the view—the application’s code—from the code that uses the
Document API to manipulate the DOM and create the view—the
framework’s code. There’s a term widely used in the software industry:
separation of concerns.
Separation of concerns
Separating concerns means splitting the code so that the parts of it that carry
out different responsibilities can be found separated from each other, which
helps the developer understand the code and maintain it.
Figure 3.2 shows the separation of concerns we want to achieve: splitting the
application code from the framework code that deals with the DOM
manipulation and keeps track of the state. We will be focusing on rendering
the view in this and the next chapter, and leave the state management for
chapter 5.
Figure 3.2. By the end of next chapter, you’ll have separated the code that describes the view
from the code that manipulates the DOM
The main objective of this separation of concerns is to simplify the
application’s developer job: they only need to focus on the application logic,
and let the framework take care of the DOM manipulation. This results in
three clear benefits:
Developer productivity—the application developer doesn’t need to write
DOM manipulation code, they can instead focus on the application
logic. They have to write less code, and that makes them ship value
faster.
Code maintainability—the DOM manipulation and application logic
aren’t mixed together, and that makes the code more succinct and easier
to understand.
Framework performance—the framework author, who’s likely to
understand how to produce efficient DOM manipulation code better than
the application developer, can optimize how the DOM is manipulated to
make the framework more performant.
Going back to the blueprint analogy: how do you define how the view should
look like, the same way the architect does with the blueprints? The answer is
the virtual DOM.
Virtual DOM
The nodes in the actual DOM are heavy objects that have hundreds of
properties, whereas the virtual nodes are lightweight objects that only contain
the information needed to render the view. Virtual nodes are cheap to create
and manipulate.
Let’s imagine that we want to produce the following HTML:
<form action="/login" class="login-form">
<input type="text" name="user" />
<input type="password" name="pass" />
<button>Log in</button>
</form>
The HTML consists of a <form> with three child nodes: two <input> and a
<button>. A virtual DOM representation for this HTML needs to contain the
same information as the DOM, namely:
For example, it’s important that the virtual DOM includes the <form> as the
root node, and that the two <input> and <button> are its children. The form
has an action and a class attribute, and the button—although not visible in
the HTML—has an onclick event handler. The type and name attributes of
the <input> elements are also crucial: it’s not the same an <input> of type
text and an <input> of type password. Also, the relative position of the
form’s children is important: the button should go below the inputs. The
framework needs all this information for the view to be rendered correctly.
Each node in the virtual DOM is an object with a type property that identifies
what kind of node it is. In this example, there are two types of nodes:
We’ll see one more type of node later in the chapter, the fragment node: a
node used to group other nodes together, but has no semantic meaning of its
own.
Each type of node has its own set of properties that describe it. Text nodes,
for example, have one property apart from the type:
And as we’ll see, fragment nodes have a children array of nodes, similar to
the children array of element nodes.
Using this virtual DOM representation allows the developer to describe how
the view of their application—the rendered HTML—should look like. You—
the framework author—implement the code that takes that virtual DOM
representation and builds the real one in the browser. This way, you
effectively separate the code that describes the view from the code that
manipulates the DOM.
Figure 3.3. The virtual DOM is a representation of the DOM made of JavaScript objects
Note
As you can see in the diagram 3.3, the nodes of the tree have a title indicating
their type. The HTML elements are written in all lowercase letters and
between angle brackets, such as "<form>" or "<input>." Text nodes are
inside a box whose title is simply "Text."
This representation of an application’s view holds all the information that we
need to know in order to unequivocally build the DOM. It maintains the
hierarchy of the elements, the attributes, event handlers and position of the
child elements. If you are given such a virtual DOM, you can derive the
corresponding HTML markup without any ambiguity.
Important
Creating virtual trees manually is a tedious task that can result in errors, such
as misspelling property names. To simplify the process of defining an
application’s view, you will write functions that generate each type of virtual
node instead of having the developer do it manually. Although using these
functions makes the virtual DOM definition process less painful, it’s still not
as convenient as writing HTML templates or JSX. However, it’s a starting
point. Towards the end of the book, you will implement a template engine
that streamlines the view definition process. For now, let’s focus on creating
the virtual DOM creation functions.
Exercise 3.1
Given the HTML markup below, can you draw the corresponding virtual
DOM tree diagram (similar to figure 3.3)?
<div id="app">
<h1>TODOs</h1>
<input type="text" placeholder="What needs to be done?">
<ul>
<li>
<input type="checkbox">
<label>Buy milk</label>
<button>Remove</button>
</li>
<li>
<input type="checkbox">
<label>Buy eggs</label>
<button>Remove</button>
</li>
</ul>
</div>
You want to create a file called h.js inside the src/ directory. This file is
where you’ll write most of the code in this chapter. Also create a utils/
directory inside src/, and add a file called arrays.js inside it. Here you’ll write
an utility function to filter null and undefined values from an array.
Your runtime package should look like this (the configuration files have been
omitted, and in bold font are the files you just created):
runtime/
└── src/
├──utils/
│ └── arrays.js
├── h.js
└── index.js
You have defined three constants, one for each type of node:
Let’s now implement the functions that create the virtual nodes, starting with
element nodes.
You’ll now implement a function h() to create element nodes taking three
arguments:
tag—the element’s tag name.
props—an object with its attributes (that we’ll call props, for
properties).
children—an array of its children nodes.
The name h() is short for hyperscript, or a script that creates hypertext.
(Recall that HTML is acronym for HyperText Markup Language.) The name
h() for the function is a common one used in some frontend frameworks,
probably because it’s short and easy to type, which is important because
you’ll be using it a lot.
Other frameworks, such as Vue, do name the virtual node producing function
h(). Mithril, for example, gives the user a function called m() to create the
virtual DOM, but it’s the same idea. Internally, Mithril implements the virtual
node creating function named as hyperscript(). The user facing function
has a nice and short name (m()), but the internal function has this more
descriptive name.
The h() function should return a virtual node object with the passed in tag
name, props, and children, plus a type property set to DOM_TYPES.ELEMENT.
You want to give default values to the props and children parameters, so
that you can call the function with only the tag name, as in h('div'). This
should be equivalent to calling h('div', {}, []).
Some child nodes might come as null (I’ll explain why in a minute), so you
want to filter them out. To filter null values from an array, you’ll write a
function called withoutNulls() in the next section (you’ll see that function
imported in the next listing, but it’s not implemented yet).
Some child nodes inside the passed in children array might happen to be
strings, not objects representing virtual nodes. In this case, you want to
transform them into virtual nodes of type DOM_TYPES.TEXT. You’ll do this
using a function called mapTextNodes() that you’ll write later.
Let’s now implement the h() function. In the h.js file, write the code in bold
in listing 3.1.
Listing 3.1. The h() function to create element virtual nodes (h.js)
When using conditional rendering, that is, rendering nodes only when a
condition is met, some children might be null in the array, and this means
that they shouldn’t be rendered at all. We want these null values to be
removed from the array of children.
Let’s use our TODO app as an example. Recall that the add new to-do button
is disabled when there’s no text in the input, or the text is too short. If instead
of disabling the button you decided to remove it from the page, you’d have a
conditional like the following:
{
tag: 'div',
children: [
{ tag: 'input', props: { type: 'text' } },
addTodoInput.value.length > 2
? { tag: 'button', children: ['Add'] }
: null
]
}
This null value means that the button shouldn’t be added to the DOM. The
simplest way to make this work is to filter out null values from the children
array when a new virtual node is created, so that a null node isn’t passed
around the framework:
{
tag: 'div',
children: [
{ tag: 'input', props: { type: 'text' } }
]
}
Note the usage of the != operator, as opposed to using !==: this is so you
remove both null and undefined values. You aren’t expecting undefined
values, but this way you’ll remove them if they appeared—just in case. (Your
linter might complain about this if you have the eqeqeq rule enabled, but you
can disable it for this line; tell the linter you know what you’re doing.)
3.5.2 Mapping strings to text nodes
After filtering out the null values from the children array, you pass the result
to a the mapTextNodes() function. We said that this function transforms
strings into text virtual nodes. Why do we want to do this? Well, just as a
convenience for creating text nodes, so instead of doing:
h('div', {}, [hString('Hello '), hString('world!')])
we can do:
h('div', {}, ['Hello ', 'world!'])
As you can anticipate, you’ll use text children a lot, so this will make your
life easier—if only a little bit. Let’s now write that missing mapTextNodes()
function. In the h.js file, below the h() function, write the code for the
function as follows:
function mapTextNodes(children) {
return children.map((child) =>
typeof child === 'string' ? hString(child) : child
)
}
You’ve used the hString() function to create text virtual nodes out of
strings, but that function doesn’t exist yet. That’s the next thing you’ll do:
implement the function that creates text virtual nodes.
Creating text nodes is the simplest of the three types of virtual nodes. A text
virtual node is simply an object with the type property set to
DOM_TYPES.TEXT, and the value property set to the text content. In the h.js
file, write the hString() function like so:
export function hString(str) {
return { type: DOM_TYPES.TEXT, value: str }
}
That was easy! You’re just missing the hFragment() function to create
fragment virtual nodes.
Note
Fragments exist in the Document API, they’re used to create subtrees of the
DOM that can be appended to the document at once. They’re represented by
the DocumentFragment class, and can be created using the
document.createDocumentFragment() method. We won’t be using the
DocumentFragment to insert the virtual fragment nodes into the DOM, but it’s
good to know that they exist.
Same as before, you’ve filtered out the null values from the array of
children, and then you’ve mapped the strings in the children array to text
virtual nodes. That’s all there is to it!
You can now use the h(), hString(), and hFragment() functions to create
virtual DOM representations of the view of your application. What we’ll
implement next is the code that takes in a virtual DOM and creates the real
DOM for it, but first, let’s put the virtual DOM functions to the test. Let’s use
the h() function to define the view of a login form:
h('form', { class: 'login-form', action: 'login' }, [
h('input', { type: 'text', name: 'user' }),
h('input', { type: 'password', name: 'pass' }),
h('button', { on: { click: login } }, ['Login'])
])
This will create a virtual DOM depicted in figure 3.4. Arguably, using the
h() functions is more concise than defining the virtual DOM manually, as a
tree of JavaScript objects.
The <button> element doesn’t have a click event handler rendered in the
HTML markup. This is because the framework will add the event handler to
the button programmatically, when it is attached to the DOM. The event
handlers added from JavaScript aren’t shown in the HTML.
As you can imagine, you typically don’t define the virtual DOM for your
entire application in one place; that can get unwieldy as the application grows
in size. What you do instead is split the view into subparts, each of which we
call a component. Components are the cornerstone of frontend frameworks:
they allow us to break down a large application into smaller, more
manageable pieces, each of which in charge of a specific part of the view.
Exercise 3.2
Using the h() function, define the virtual DOM equivalent to the following
HTML markup:
<h1 class="title">My counter</h1>
<div class="container">
<button>decrement</button>
<span>0</span>
<button>increment</button>
</div>
Exercise 3.3
This function might come in handy when you’re building a UI and need some
placeholder text to fill in the space, so you can see how the UI looks like with
real content.
Let’s see what makes a component in your early version of the framework.
Let’s take a small detour from the implementation of the virtual DOM to
understand how you’ll decompose the view of your application into
components using your first version of the framework.
Figure 3.5. The view of an application is a function of the state. When a new to-do is added to the
state, the virtual DOM is re-evaluated and the DOM is updated with the new to-do
This means that, to produce the virtual DOM representing the view, the
current state of the application must be taken into account, and that, when the
state changes, the virtual DOM must be re-evaluated. If we generate the
virtual DOM that represents the view of the application by calling a function
that receives the state as parameter, we can easily re-evaluate it when the
state changes. For example, in the case of our TODOs application, the virtual
DOM for the list of to-dos (consisting of only the to-do description, for the
sake of simplicity) could be generated by a function like this:
function TodosList(todos) {
return h('ul', {}, todos.map((todo) => h('li', {}, [todo])))
}
the function would return the following virtual DOM (the empty props
objects have been omitted for brevity):
{
tag: 'ul',
type: 'element',
children: [
{ tag: 'li', children: [{ type: 'text', value: 'Walk the dog' }] },
{ tag: 'li', children: [{ type: 'text', value: 'Water the plants' }] }
]
}
This virtual DOM representing the list of to-dos would be rendered by the
framework into the following HTML:
<ul>
<li>Walk the dog</li>
<li>Water the plants</li>
</ul>
You can see this process summarized in figure 3.6: the application code—
written by the developer—generates the virtual DOM describing the view,
then the framework—written by yourself—creates the real DOM from the
virtual DOM and inserts it into the browser’s document.
Figure 3.6. The application creates the virtual DOM that the framework renders into the DOM
One nice property of using functions to produce the virtual DOM, receiving
the application’s state as argument, is that we can break the view into smaller
parts. Pure functions—functions which don’t produce any side effects and
always return the same result for the same input arguments—can be easily
composed to build more complex views.
Exercise 3.4
The function should return a virtual DOM that represents a message box with
the message and the corresponding CSS class depending on the level. The
CSS classes are message—info, message—warning, and message—error for
the different levels, and the markup should look like this:
<div class="message message--info">
<p>This is an info message</p>
</div>
Pure functions have the nice property that they can be composed nicely to
build more complex functions. If we generate the virtual DOM to represent
the view of the application by composing smaller functions, we can easily
break the view into smaller parts. These sub-functions represent a part of the
view, the components. The arguments passed to a component are known as
props, as we’ve already mentioned.
Components v1.0
Components are functions that generate the virtual DOM for a part of the
application’s view. They take the state of the application, or part of it, as their
argument. The arguments passed to a component, the data coming from
outside the component, are known as props.
Let’s work an example with the TODOs application view. If we don’t break
the view into smaller parts, we’ll have a single function that generates the
virtual DOM for the entire application. Such a function would be long and
hard to understand by other developers—and probably by ourselves as well.
But we can clearly distinguish two parts in the view: the form to add a new
to-do and the list of to-dos (figure 3.7); those can be generated by two
different functions. That is, we can break the view into two sub-components.
Figure 3.7. The TODOs application can be broken into two sub-components: CreateTodo() and
TodoList()
So the virtual DOM for the whole application could be created by a
component like the following:
function App(state) {
return hFragment([
h('h1', {}, ['My TODOs']),
CreateTodo(state),
TodoList(state)
])
}
Note that in this case, there isn’t a parent node in the virtual DOM that
contains the header of the application and the two sub-components: we use a
fragment to group the elements together. Also note the naming convention:
the functions that generate the virtual DOM are written in PascalCase. This
is to signal that they are components that create a virtual DOM tree, and not
regular functions.
Figure 3.8. The TodoList() component can be broken down into a TodoItem() sub-component
and thus, the TodoList() component would look similar to the following:
function TodoList(state) {
return h('ul', {},
children: state.todos.map(
(todo, i) => TodoItem(todo, i, state.editingIdxs)
)
)
}
The TodoItem() component would render a different thing depending on
whether the to-do is in "read" or "edit" mode. Those could be further
decomposed into two different sub-components: TodoInReadMode() and
TodoInEditMode(). It’d be something like the following:
Figure 3.9. The hierarchy of components of the TODOs application, where each component has
below the virtual DOM nodes it generates
In this diagram we see the view of the application as a tree of components
with the virtual DOM nodes they generate below them. The App()
component is the root of the tree, and it has three children: an <h1> element,
and the CreateTodo() and TodoList() components. A component can only
return a single virtual DOM node as its root, so the three children of App()
are grouped together in a fragment.
Then, following the hierarchy down, we see the TodoList() component has a
single child, the <ul> element, which in turn has a list of TodoItem()
components. The ellipsis in the tree indicates that the <ul> element might
have more children than the ones shown in the diagram, a number that
depends on how many to-dos are in the list. Finally, the TodoItem()
component has two children: the TodoInEditMode() and TodoInReadMode()
components. These components would render more virtual DOM nodes, but
we don’t show them in the diagram for simplicity.
Note
As you can see in the diagram 3.9, the component nodes have their title
written in PascalCase and include the parenthesis to indicate they are
functions. Fragments are titled "Fragment."
Now that you understand how to break down the view of your application
into components—pure functions that generate the virtual DOM given the
state of the application—you’re ready to implement the code in the
framework which is in charge of mounting the virtual DOM returned by the
h() functions to the browser’s DOM. You’ll do exactly that in the next
chapter.
<ul>
<li>
<input type="checkbox">
<label>Buy milk</label>
<button>Remove</button>
</li>
<li>
<input type="checkbox">
<label>Buy eggs</label>
<button>Remove</button>
</li>
</ul>
</div>
Here’s how you can use the hFragment() and h() functions to create its
virtual DOM:
hFragment([
h('h1', { class: 'title' }, ['My counter']),
h('div', { class: 'container' }, [
h('button', {}, ['decrement']),
h('span', {}, ['0']),
h('button', {}, ['increment'])
])
])
return hFragment(
Array(n).fill(h('p', {}, [text]))
)
}
Note that I had to split the lines of the text variable to make it fit in the page.
You can keep it in a single line in your code.
You could complement this component with some CSS rules to add colors to
the different levels of messages, and a bit of padding and border to make
them look nicer:
.message {
padding: 1rem;
border: 1px solid black;
}
.message--info {
background-color: #C5E0EF;
}
.message--warning {
background-color: #FFFAD5;
}
.message--error {
background-color: #F9CBCD;
}
3.10 Summary
The virtual DOM is the blueprint for the view of the application: it
allows the developer describe how the view should look like in a
declarative way (similar to what an architect does with the blueprints of
a house) and moves the responsibility of manipulating the DOM to the
framework.
Thanks to using a virtual DOM to declare how the view should look
like, the application developer is freed from having to know how to
manipulate the DOM using the Document API, and they don’t need to
mix business logic with DOM manipulation code.
A component is a pure function—a function with no side effects—that
takes the state of the application as input and returns a virtual DOM tree
representing a chunk of the view of the application. In later chapters, the
definition of a component will be extended to include the ability to have
internal state and a lifecycle that’s independent from that of the
application.
There are three types of virtual nodes: text nodes, element nodes, and
fragment nodes. The most interesting one is the element node, as it
represents the regular HTML elements that can have attributes, children,
and event listeners.
The hString(), h(), and hFragment() functions are used to create text,
element, and fragment virtual nodes, respectively. The virtual DOM can
be directly declared as a tree of JavaScript objects, but calling these
functions makes the process simpler.
Fragment virtual nodes consist of an array of children virtual nodes.
Fragment virtual nodes are useful when a component would return a list
of virtual nodes without a parent node. The DOM—and by extension the
virtual DOM—is a tree data structure, every level in the tree (except the
root) must have a parent node, so a fragment node can be used to group
a list of virtual nodes.
4 Mounting and destroying the
virtual DOM
This chapter covers
Creating HTML nodes from virtual DOM nodes
Inserting the HTML nodes into the browser’s document
Removing HTML nodes from the browser’s document
In the previous chapter, you learnt what the virtual DOM is and how to create
it. You implemented the h(), hString(), and hFragment() functions to
create virtual nodes of type element, text, and fragment, respectively. Now
it’s time to learn how to create the real DOM nodes from the virtual DOM
nodes, and insert them into the browser’s document. This is achieved using
the Document API, as you’ll see in this chapter.
When the view of your application is no longer needed, you want to remove
the HTML nodes from the browser’s document. You’ll learn how to do this
in this chapter as well.
Important
You can find all the listings in this chapter in the listings/ch04 directory of
the book’s repository.
The code you write in this chapter is for the framework’s first version, which
you’ll publish in chapter 6. Therefore, the code in this chapter can be checked
out from the ch6 label:
$ git switch ch6
Figure 4.1. Mounting a virtual DOM to the browser’s document <body> element
When the mountDOM() function creates each of the DOM nodes for the virtual
DOM, it needs to save a reference to the real DOM node in the virtual node,
under the el property (el for element). You can see this in figure 4.2. This
reference is used by the reconciliation algorithm you’ll write in chapters 7
and 8, to know what DOM nodes to update.
Figure 4.2. The virtual node’s el property keeps a reference to the real DOM node
Similarly, if the node included event listeners, the mountDOM() function saves
a reference to the event listener in the virtual node, under the listeners
property.
Figure 4.3. The virtual node’s listeners property keeps a reference to the event listener
Saving these references has a double purpose. First, it allows the framework
to remove the event listeners and detach the element from the DOM when the
virtual node is unmounted. Second, it’s required by the reconciliation
algorithm to know what element in the DOM needs to be updated. This will
become clear in chapter 7; for now, bear with me.
Using the example from earlier, the virtual DOM we defined as:
const vdom = h('form', { class: 'login-form', action: 'login' }, [
h('input', { type: 'text', name: 'user' }),
h('input', { type: 'password', name: 'pass' }),
h('button', { on: { click: login } }, ['Login'])
])
would result in the virtual DOM tree depicted in figure 4.4, where you can
see the el and listeners references in the virtual nodes.
Figure 4.4. The login form virtual DOM example mounted to the browser’s document <body>
element. The virtual nodes keep a reference to the real DOM nodes in the el property, and to the
event listeners in the listeners property (shown as a lightning icon).
This HTML tree would be attached to the <body> element, and the resulting
HTML markup would be:
<body>
<form class="login-form" action="login">
<input type="text" name="user">
<input type="password" name="pass">
<button>Login</button>
</form>
</body>
A virtual node of type text requires a Text node to be created (via the
document.createTextNode() method).
A virtual node of type element requires an Element node to be created
(via the document.createElement() method).
Listing 4.1. The mountDOM() function used to mount the virtual DOM to the browser’s document
(mount-dom.js)
case DOM_TYPES.ELEMENT: {
createElementNode(vdom, parentEl) #2
break
}
case DOM_TYPES.FRAGMENT: {
createFragmentNodes(vdom, parentEl) #3
break
}
default: {
throw new Error(`Can't mount DOM of type: ${vdom.type}`)
}
}
}
The function uses a switch statement that checks the type of the virtual node.
Depending on the node’s type, the appropriate function to create the real
DOM node gets called. If the node type isn’t one of the three supported types,
the function throws an error. (If you made a mistake, like misspelling the type
of a virtual node, this error will help you find it.)
If you recall, the virtual nodes created by the hString() function you
implemented earlier have the following structure:
{
type: DOM_TYPES.TEXT,
value: 'I need more coffee'
}
These virtual nodes have a type property, identifying them as a text node,
and a value property, which is set to the string that the hString() function
receives as an argument. This is the text that you need to pass to the
createTextNode() method. After creating the text DOM node, you need to
do two things:
1. Save a reference to the real DOM node in the virtual node, under the el
property.
2. Attach the text node to the parent element.
parentEl.append(textNode) #3
}
Exercise 4.1
Using the hString() and mountDOM() functions, insert the text "OMG, so
interesting!" below the headline of your local’s newspaper website. (You’ll
need to copy/paste some of your code in the browser’s console to do this.)
Fragment nodes are very simple to mount: you just need to mount the
children of the fragment. What’s important to remember about fragments is
that they aren’t a node that gets attached to the DOM; they’re just an array of
children. It’s for this reason that the el property of a fragment virtual node
should point to the parent element where the fragment’s children are attached
to, as depicted in figure 4.5.
Figure 4.5. Fragment’s el should reference the parent element where its children are attached to
Note that, if you have nested fragment nodes, all the fragment nodes' children
will be appended to the same parent element. This means that all the el
references of those fragment virtual nodes should point to the same parent
element, as you can see depicted in figure 4.6.
Figure 4.6. Nested fragments will all point to the same parent element
Now that you have a good understanding of how fragments work, it’s time to
write some code. Write the createFragmentNode() function inside the
mount-dom.js file as follows:
function createFragmentNodes(vdom, parentEl) {
const { children } = vdom
vdom.el = parentEl #1
If you recall, calling the h() function to create element virtual nodes returns
an object with the type property set to DOM_TYPES.ELEMENT, a tag property
with the tag name, and a props property with the attributes and event
listeners. If the virtual node has children, they appear inside the children
property.
For example, a <button> virtual node with a class of btn and an onclick
event listener would look like this:
{
type: DOM_TYPES.ELEMENT,
tag: 'button',
props: {
class: 'btn',
on: { click: () => console.log('yay!') }
},
children: [
{
type: DOM_TYPES.TEXT,
value: 'Click me!'
}
]
}
To create the corresponding DOM element from the virtual node, you need
to:
As you can see, the props property of the virtual node is the one that contains
the attributes and event listeners, both together. But attributes and event
listeners are handled differently, so you’ll need to separate them.
Then, from the attributes, there are two special cases that need special
handling as well, and are those related to styling: style and class. You’ll
extract those from the props object as well, and handle them separately.
// --snip-- //
Note that you’re using two functions you haven’t implemented yet:
setAttributes() and addEventListeners(), imported from the attributes.js
and events.js files, respectively. You will write them in a minute.
Setting the attributes and adding event listeners is the part where the code
differs from text nodes. With text nodes, you want to attach event listeners
and set attributes to it’s parent element node, not to the text node itself.
Note
The Text type defined in the Document API inherits from the EventTarget
interface, which declares the addEventListener() method. So, in principle,
you can add event listeners to a text node. But if you go ahead and try to do
that in the browser, you’ll see that the event listeners are never called—it just
doesn’t work.
Create a new file under the src/ directory called events.js and add the
following code:
export function addEventListener(eventName, handler, el) {
el.addEventListener(eventName, handler)
return handler
}
}
}
return addedListeners
}
After all, the same event handler functions that we got as input we’re
returning as output, so the refactor seems legit. But, that is now; it won’t be
the case later when you make the components have their own state. You
won’t be adding the same functions as event handlers, but new functions that
will be created by the framework with some extra logic around them. This
might sound confusing now, but stick with me, and you’ll see how this makes
sense later.
Now that you’ve implemented the event listeners, let’s move on and
implement the setAttributes() function. We’re getting closer to having a
working mountDOM() function.
Assuming you have a reference to the <p> element in a variable called p, you
can set the id property of the p element to a different value, like so:
p.id = 'bar'
Caution
There are some attributes that work a bit differently. For instance, the value
attribute of an <input> element is not reflected in the rendered HTML. If you
you have the following HTML:
<input type="text" />
You’ll see the string "yolo" in the input, but the rendered HTML will still be
the same; no value attribute will be rendered. But even more interesting is
the fact that, you can add the attribute in the HTML markup:
<input type="text" value="yolo" />
And the "yolo" string will appear in the input when it first renders, but if you
type in something different, the same value for the value attribute will
remain in the rendered HTML. But you can read whatever was typed in the
input by reading the value property of the input element.
There are, nevertheless, two special attributes that we’ll handle differently:
the style attribute and the class attribute. You’ll see why in a moment.
Let’s start by writing the setAttributes() function: the one you used in
listing 4.2 to set the attributes on the element node. The role of this function
is to extract the attributes that require special handling (the style and class
attributes) from the rest of the attributes, and then call the setStyle() and
setClass() functions to set those attributes. The rest of the attributes are
passed to the setAttribute() function. You’ll write these setStyle(),
setClass(), and setAttribute() functions later.
Start by creating the attributes.js file under the src/ directory and write the
code in listing 4.3.
if (className) {
setClass(el, className) #2
}
if (style) {
Object.entries(style).forEach(([prop, value]) => {
setStyle(el, prop, value) #3
})
}
Now that you have the function that splits the attributes into the ones that
require special handling and the rest, and calls the appropriate functions to set
them, let’s go ahead and look at each of those in turn. In order of appearance,
the first one is the setClass() function, which is in charge of setting the
class attribute. Note that you’ve destructured the attrs property and aliased
the class attribute to the className variable, as class is a reserved word in
JavaScript.
Setting the "class" attribute
When you write HTML, you set the class attribute of an element node like
this:
<div class="foo bar baz"></div>
In this case, the <div> element has three classes: foo, bar, and baz. Easy! But
now comes the tricky part: a DOM element (an instance of the Element class)
doesn’t have a class property, but instead has two properties, namely
className and classList, that are related to the class attribute. Let’s look
at the classList first.
and wanted to add the foo, bar, and baz classes to it, you could do it like this:
div.classList.add('foo', 'bar', 'baz')
Then is the className property, which is a string that contains the value of
the class attribute. Following the example above, if you wanted to add the
same three classes to the <div> element, you could do it like this:
div.className = 'foo bar baz'
And this would yield the same HTML as the previous example.
You actually want to use both ways of setting the class attribute, depending
on the situation. We should allow the developers using your framework to set
the class attribute in two ways: either as a string or as an array of string
items. So, to add multiple classes to an element, the developer could define
the following virtual node:
{
type: DOM_TYPES.ELEMENT,
tag: 'div',
props: {
class: ['foo', 'bar', 'baz']
}
}
Both of these options should work. Thus, the setClass() function needs to
distinguish between the two cases and handle them accordingly. With this in
mind, write the following code in the attributes.js file:
function setClass(el, className) {
el.className = '' #1
if (Array.isArray(className)) {
el.classList.add(...className) #3
}
}
With the setClass() function out of the way, let’s move on to the
setStyle() function, which is in charge of setting the style attribute of an
element.
For example, using the element from the previous snippet, you could remove
the color style by doing this:
element.style.color = null
element.style.cssText // 'font-family: Georgia;'
Now that you know how to work with the style property of an HTMLElement
instance, go ahead and write the setStyle() and removeStyle() functions.
The first function takes an HTMLElement instance, the name of the style to set,
and the value of the style, and sets that style on the element. The second
function takes an HTMLElement instance and the name of the style to remove,
and removes that style from the element.
And with this last function, you’re done implementing the mountDOM()
function, that takes a virtual DOM and mounts it to the real DOM inside the
passed in parent element. Congratulations!
mountDOM(vdom, document.body)
You now want to have a function that, given a mounted virtual DOM,
destroys it and removes it from the document: destroyDOM(). With this
function you’ll be able to clear the document’s body from the previous
example, like so:
destroyDOM(vdom, document.body)
Exercise 4.2
Using the hFragment(), h(), and mountDOM() functions, insert a new section
below the headline of your local’s newspaper website. The section should
have a title, a paragraph, and a link to an article in Wikipedia. Be creative!
Exercise 4.3
Following up on the previous exercise, inspect the virtual DOM tree in the
browser’s console. Check the el property of the fragment virtual node. What
does it point to? What about the el property of the paragraph and link virtual
nodes?
text node—remove the text node from its parent element using the
remove() method.
fragment node—remove each of its children from the parent element,
which if you recall, is referenced in the el property of the fragment
virtual node.
element node—do the two previous things, plus remove the event
listeners from the element.
In all of the cases, you want to remove the el property from the virtual node,
and in the case of an element node, also remove the listeners property. This
is so that you can tell that the virtual node has been destroyed and allow the
garbage collector to free the memory of the HTML element. When a virtual
node doesn’t have an el property, you can safely assume that it’s not
mounted to the real DOM, and therefore can’t be destroyed.
To handle these three cases, you need a switch statement that, depending on
the type property of the virtual node calls a different function.
You are ready to implement the destroyDOM() function. Create a new file
inside the src folder called destroy-dom.js and write the code in Listing 4.4.
switch (type) {
case DOM_TYPES.TEXT: {
removeTextNode(vdom)
break
}
case DOM_TYPES.ELEMENT: {
removeElementNode(vdom)
break
}
case DOM_TYPES.FRAGMENT: {
removeFragmentNodes(vdom)
break
}
default: {
throw new Error(`Can't destroy DOM of type: ${type}`)
}
}
delete vdom.el
}
You’ve written the algorithm for destroying the DOM associated with a
passed in virtual node: vdom. You’ve handled each type of virtual node
separately—you’ll need to write the missing functions in a minute—and
you’ve lastly deleted the el property from the virtual node. This process is
depicted in 4.8.
Let’s move on to the code for destroying an element, which is a bit more
interesting.
el.remove()
children.forEach(destroyDOM)
if (listeners) {
removeEventListeners(listeners, el)
delete vdom.listeners
}
}
Figure 4.9. When destroying a fragment, don’t remove it’s referenced element from the DOM; it
might be the <body> or some other element that you didn’t create, and therefore don’t own
The implementation of the removeFragmentNodes() is therefore very simple:
function removeFragmentNodes(vdom) {
const { children } = vdom
children.forEach(destroyDOM)
}
Exercise 4.4
Using the destroyDOM() function, remove the section that you added to your
local newspaper website in the previous exercise from the DOM. Make sure
that the fragment’s referenced element is not removed from the DOM, as it
was originally created by the newspaper website, not by you.
Then, check the vdom tree you used to create the section, and make sure that
the el property has been removed from all the virtual nodes.
The first version won’t be very sophisticated—it’ll destroy the entire DOM
using destroyDOM() and mount it from scratch using mountDOM() every time
the state changes—but it will be a great starting point. After finishing the
next chapter you’ll have a working framework, so I hope you’re excited
about it!
For this exercise, you first need to open your local—or favorite—newspaper
website and inspect the DOM. Locate where the headline of the article is, and
select it in the inspector, as in figure 4.10. (When you have an element
highlighted in the inspector, you can reference it in the console by using the
$0 variable.)
Figure 4.11. Inserting the "OMG, so interesting!" text inside the headline of an article
If head over to the inspector and inspect the <div> element that contains the
headline, you’ll see that the text you just inserted is there (figure 4.12).
As you can see, the text is inside the <div> element, beside the <h1> element
that contains the headline.
Then, create the vdom for the section you’ll add to the article:
const section = hFragment([
h('h2', {}, ['Very important news']),
h('p', {}, ['such news, many importance, too fresh, wow']),
h(
'a',
{
href: 'https://en.wikipedia.org/wiki/Doge_(meme)',
},
['Doge!'],
),
])
Inspecting the DOM, you can see that the section was added to the article
(figure 4.13).
Then, you can inspect the section vdom again (figure 4.15).
Figure 4.15. Inspecting the section vdom tree after removing it from the DOM
As you can see, the el references of the virtual nodes have all been removed.
If you check the DOM, you’ll see that all the nodes were correctly removed
from the DOM, but the <div> element that was the parent of the fragment
was not removed, as it was created by the newspaper website, not by you.
(Remember that the fragment’s el reference points to the parent element
where the fragment children were mounted.)
4.4 Summary
Mounting a virtual DOM means to create the DOM nodes that represent
each virtual node in the tree, and to insert them into the DOM, inside a
parent element.
Destroying a virtual DOM means to remove the DOM nodes created out
of it, also making sure to remove the event listeners from the DOM
nodes.
5 State management and the
application’s lifecycle
This chapter covers
What state management is
Implementing a state management solution
Mapping JavaScript events to commands that change the state
Updating the state using reducer functions
Re-rendering the view when the state changes
I remember some time ago I went to the coast in the south of Spain, to a
small village in Cadiz. There was this restaurant, so popular that they’d run
out of food quickly; they had a limited number of each of the dishes they
served, and they would update a chalkboard with the number of servings they
had left. When there was no more of a particular dish, they’d cross it out. The
waiter took orders from the customers and updated the chalkboard to reflect
the new state of the restaurant: having one less of that dish. Us, the
customers, could easily know what we could order by looking at the
chalkboard. But from time to time, and due to the work load, the waiter
forgot to update the chalkboard, and customers would find out the dish
they’ve been waiting so long in line to order was sold out. You can picture
the drama. It’s clearly important for the restaurant to have an updated
chalkboard that matched the remaining servings of each dish.
To have a working framework, you’re missing a key piece that does a similar
job as to the waiter in the restaurant, a state manager. In the restaurant
anecdote, the waiter is in charge of keeping the chalkboard in sync with the
state of the restaurant: the number of servings of each dish they had left. In a
frontend application, the state changes as the user interacts with it, and the
view needs to be updated to reflect that changes. The state manager keeps the
application’s state in sync with the view, responding to user input by
modifying the state accordingly, and notifying the renderer when the state
has changed. The renderer is the entity in your framework that takes the
virtual DOM and mounts it into the DOM.
In this chapter, you’ll implement both the renderer (using the mountDOM() and
destroyDOM() functions from the previous chapter) and the state manager
entities, and you’ll learn how they communicate. By the end of the chapter,
you’ll have your first version of the framework, whose architecture (shown in
figure 5.1) is basically the state manager and renderer glued together. (The
state manager is displayed with a question mark because we’ll discover how
it works in this chapter.)
Figure 5.1. Your first framework is just a state manager and a renderer glued together.
The framework you’ll have at the end of the chapter is very rudimentary: to
ensure the view reflects the current state, its renderer completely destroys and
mounts the DOM every time the state changes. As you can see in the diagram
in figure 5.1, the renderer draws the view as a three-step process:
In this chapter, you’ll learn exactly how to do that. So, with the big picture in
mind, let’s start working on the state manager.
Important
You can find all the listings in this chapter in the listings/ch05 directory of
the book’s repository.
The code you write in this chapter is for the framework’s first version, which
you’ll publish in chapter 6. Therefore, the code in this chapter can be checked
out from the ch6 label:
$ git switch ch6
When the user clicked the Add button or pressed the Enter key, that
meant they wanted to add a new TODO item to the list.
"Adding a to-do" is framed in the language of the application, whereas
"clicking a button" is a very generic thing to do. (You click buttons in all
sorts of applications, but that only translates to "adding a to-do" in the
TODOs application.)
If the application developer wants to update the state when a particular event
is dispatched, they first need to determine what that event means in terms of
the application domain. They then map that event to a command that the
framework can understand. A command is a request to do something, as
opposed to an event, which is a notification about something that has
happened. These commands are a request to the framework to update the
state, and are expressed using the domain language of the application.
An event is a notification about something that has happened. "A button was
clicked," "a key was pressed," "a network request was completed" are all
examples of events. Events aren’t requesting the framework or application to
do anything, they’re just notifications with some additional information.
Event names are usually framed in the past tense: 'button-clicked', 'key-
pressed', 'network-request-completed', and so on.
Table 5.1. Mapping between browser events and application commands in the TODOs
application
Double-clicking a on a
Double-click a to-do start-editing-todo to-do item sets the to-do
item
item in edit mode.
And in figure 5.5 you can see the same mapping in the form of a diagram. In
it, you can establish a link between the browser events and the application
commands, which are dispatched by the application developer to the
framework, using the dispatch() function. You’ll learn more about this
function in a minute.
Figure 5.5. The mapping between browser events and application commands
Why is this relevant to answer the previous questions? you might be asking.
Because once the application domain commands are identified, the
application developer can supply functions that update the state as a response
to those commands. The state manager executes those functions to update the
state. Let’s look into what these functions are and how they’re executed.
Exercise 5.1
When each of the buttons is clicked, what commands would you dispatch to
the framework? Can you draw a diagram similar to the one in figure 5.5 that
maps browser events to application commands?
Figure 5.6. A reducer function takes the current state and a payload and returns a new state
Note
This might sound familiar to you if you’ve used Redux before: these
functions are the reducers. (The term reducer comes from the reduce
function in functional programming.) A reducer, in our context, is a function
that takes the current state and a payload (the command’s data) and returns a
new updated state. These functions never mutate the state that’s passed to
them (that’d be considered a side effect, and therefore the function wouldn’t
be pure), but instead create a new state.
Let’s see an example with the TODOs application. To create a new version of
the state when the user removes a to-do item from the list (recall that the state
was just the list of to-dos), the reducer function associated to this 'remove-
todo' command would look like this:
function removeTodo(state, todoIndex) {
return state.toSpliced(todoIndex, 1)
}
And we wanted to remove the to-do item at index 1, the new state would be
computed using the removeTodo() reducer, as follows:
todos = removeTodo(todos, 1)
// todos = ['Walk the dog', 'Sand the chairs']
In this case, the payload associated with the 'remove-todo' command is the
index of the to-do item to remove. Note how the original array is not mutated,
but a new one is created instead (the filter() method of an array returns a
new array).
Exercise 5.2—challenge
How would you design the state of the application? What commands would
you dispatch to the framework in response to the user clicking on a square?
What reducer functions would you implement to update the state?
The state needs to have, at least, three pieces of information: the grid of
squares, the player who’s turn it is, and the winner of the game (if any).
To design what commands are needed, think about what actions the user
can perform in the application.
The reducers can be deduced from the commands, and how the state
needs to be updated in response to them.
Let’s do a quick recap of what we’ve seen so far. We’ve seen that the
application developer translates browser events into application domain
commands, and that the state manager executes the reducer functions
associated to those commands to update the state. But, how does the state
manager know which reducer function to execute when a command is
dispatched? There needs to be something that maps the commands to the
reducer functions. We’ll call this mechanism a dispatcher, and it’s the state
manager’s central piece.
Figure 5.7. A consumer function takes a single parameter and returns no value
A consumer that handles a command can easily wrap a reducer function, as
the following example shows:
function removeTodoHandler(todoIndex) {
// Calls the removeTodo() reducer function to update the state.
state = removeTodo(state, todoIndex)
}
As you can see, the command handler function that removes a to-do from the
list receives the to-do index as its single parameter, and then calls the
removeTodo() reducer function to update the state. The handler simply wraps
the reducer function, and it’s the dispatcher’s responsibility to execute the
handler function in response to the 'remove-todo' command.
But how do you tell the dispatcher which handler function to execute in
response to a command?
Your dispatcher also needs to have a dispatch() method that executes the
handler functions associated to a command. You can see in figure 5.8 a
sequence diagram of how the dispatcher works.
Figure 5.8. The dispatcher’s subscribe() method registers handlers to respond to commands
with a given name, and dispatch() executes the handlers associated to a command
It’s time to implement the dispatcher. Create a new file called dispatcher.js in
the src/ directory. Your runtime/src/ directory should look like this (in bold
font the new files):
src/
├── utils/
│ └── arrays.js
├── attributes.js
├── destroy-dom.js
├── dispatcher.js
├── events.js
├── h.js
├── index.js
└── mount-dom.js
Note
The hash # in front of the variable name is the ES2020 way to make the
variable private inside a class. Starting from ES2020, any variable or method
that starts with a hash is private and can only be accessed from within the
class.
subscribe(commandName, handler) {
if (!this.#subs.has(commandName)) { #1
this.#subs.set(commandName, [])
}
handlers.push(handler) #3
return () => { #4
const idx = handlers.indexOf(handler)
handlers.splice(idx, 1)
}
}
}
The Dispatcher is a class with a private variable called subs (short for
subscriptions): a JavaScript map to store the registered handlers by event
name. Note how more than one handler can be registered for the same
command name.
Warning
In the beginning of this chapter we said that the state manager is in charge of
keeping the state in sync with the views. It does so by notifying the renderer
about state changes, so that the renderer can update the views accordingly.
The question is then: how does the dispatcher notify the renderer?
You know that the state can change only in response to commands. A
command triggers the execution of one or more handler functions, which
execute reducers, which in turn update the state. Therefore, the best moment
to notify the renderer about state changes is after the handlers for a given
command have been executed. You should allow the dispatcher to register
special handler functions, which we’ll call after-command handlers, and are
executed after the handlers for any dispatched command have been executed.
The framework uses these handlers to notify the renderer about potential state
changes, so it can update the view.
Figure 5.9. The dispatcher’s afterEveryCommand() method registers handlers to run after every
command is handled
Write the code in bold font that’s shown inside the Dispatcher class, to add
the afterEveryCommand() method:
// --snip-- //
afterEveryCommand(handler) {
this.#afterHandlers.push(handler) #1
return () => { #2
const idx = this.#afterHandlers.indexOf(handler)
this.#afterHandlers.splice(idx, 1)
}
}
}
This method is very similar to the subscribe() method, except that it doesn’t
take a command name as a parameter: these handlers are called for all
dispatched commands. This time we’re not checking for duplicates; we’re
allowing for the same handler to be registered multiple times. After-
command handlers don’t modify the state, they are a notification mechanism,
and so notifying about the same event multiple times might be a valid use-
case.
Dispatching commands
Listing 5.3. Dispatching a command given its name and payload (dispatcher.js)
dispatch(commandName, payload) {
if (this.#subs.has(commandName)) { #1
this.#subs.get(commandName).forEach((handler) => handler(payload))
} else {
console.warn(`No handlers for command: ${commandName}`)
}
That’s it! Figure 5.10 shows the framework’s first version architecture.
You’ve implemented the dispatcher—the state manager—and now it’s time
to integrate it with the renderer to form a working framework.
Result
subscribe(commandName, handler) {
if (!this.#subs.has(commandName)) {
this.#subs.set(commandName, [])
}
handlers.push(handler)
return () => {
const idx = handlers.indexOf(handler)
handlers.splice(idx, 1)
}
}
afterEveryCommand(handler) {
this.#afterHandlers.push(handler)
return () => {
const idx = this.#afterHandlers.indexOf(handler)
this.#afterHandlers.splice(idx, 1)
}
}
dispatch(commandName, payload) {
if (this.#subs.has(commandName)) {
this.#subs.get(commandName).forEach((handler) => handler(payload))
} else {
console.warn(\`No handlers for command: ${commandName}`)
}
Make sure you wrote the code correctly and your implementation matches the
one in listing 5.4. If so, let’s move on to the next section.
Exercise 5.3
To put your newly implemented Dispatcher class to use, paste the code in
listing 5.4 into the browser’s console (remember to leave the export
statement out). Then, create a new instance of the Dispatcher class and
register a handler for a command called 'greet'. This' command payload
should be a string with the name of the person to greet. When the command
is dispatched, the handler should log a greeting to the console: 'Hello,
<name>!' (where <name> is the name of the person in the payload). You also
want to have an after-command handler that logs a message to the console
saying "Done greeting!".
For example, when the command 'greet' is dispatched with the payload
’John':
dispatcher.dispatch('greet', 'John')
The application instance is the object that manages the lifecycle of the
application: it manages the state, renders the views, and updates the state in
response to user input. There are three things that developers need to pass to
the application instance:
Your framework can take care of the rest: instantiating a renderer and a state
manager, and wiring them together. (Remember, your initial version of the
framework won’t be much more than these two things glued together.) The
application instance can expose a mount() method that takes a DOM element
as parameter, mounts the application in it, and kicks off the application’s
lifecycle. From this point on, the application instance is in charge of keeping
the state in sync with the views, like the waiter in the restaurant from the
beginning of the chapter.
This might sound a bit abstract a the moment, but it’ll become clear later on
when you rewrite the TODO application using your framework. For now,
bear with me and let’s move on to implementing the application instance.
Let’s start with the renderer.
The createApp() function takes an object with two properties: state and
view. The state property is the initial state of the application, and the view
property is the top-level component of the application. You’ll add the
reducers later.
You need two variables in the closure of the createApp() function: parentEl
and vdom. They keep track of the DOM element where the application is
mounted and the virtual DOM tree of the previous view, respectively. They
should both be initialized to null because the application hasn’t been
mounted yet.
Create a new file called app.js and write the code in listing 5.5.
function renderApp() {
if (vdom) {
destroyDOM(vdom) #2
}
vdom = view(state)
mountDOM(vdom, parentEl) #3
}
return {
mount(_parentEl) { #4
parentEl = _parentEl
renderApp()
},
}
}
Okay, you’ve got the renderer, you could already render an application in the
browser, but it wouldn’t respond to user input. For that, you need the state
manager, that tells the renderer to re-render the application when the state
changes. Let’s move on to that.
Write the code in bold in listing 5.6. Note that part of the code you wrote
earlier is elided for clarity.
Listing 5.6. Adding the state manager to the application instance (app.js)
// --snip-- //
}
Let’s unpack what you’ve done here. First, you’ve added a reducers
property into the createApp() function parameter. This property is an object
which maps command names to reducer functions, functions that take the
current state and the command’s payload and return a new state.
Next, you’ve created an instance of the Dispatcher class and saved it in the
dispatcher variable. The line below that is crucial: you’ve subscribed the
renderApp() function to be an after-command handler, so that the application
is re-rendered after every command is handled. Not every command
necessarily changes the state, but you don’t know that in advance, so you
have to re-render the application after every command.
Note
To avoid re-rendering the application when the state didn’t change, you could
compare the state before and after the command was handled. This
comparison can nevertheless become expensive if the state is a heavy and
deeply nested object and the commands are frequent. In chapters 7 and 8
you’ll improve the performance of the renderer, by only patching the DOM
where it’s necessary, so re-rendering the application will be a reasonably fast
operation. Not checking if the state changed is a trade-off we’re making to
keep the code simple.
Can you see why the concept of after-command handlers were a good idea
now? Note that, the afterEveryCommand() function returns a function that
unsubscribes the handler, so you’ve saved it in the subscriptions array, an
array that you’ve initialized to have this function as its first element.
You then iterate the reducers object, wrap each of the reducers inside a
handler function that calls the reducer and updates the state, and subscribe
that handler to the dispatcher. You’ve been careful to save the subscription
functions in the subscriptions array, so that you can unsubscribe them later
when the application is unmounted.
Great—you’ve got the state manager hooked up to the renderer. But there’s
something we haven’t talked about yet: how do the components dispatch
commands? Let’s look at that next.
Figure 5.11. The dispatcher can be thought of as a remote control, where each button dispatches
a command whose handler function can modify the state of the application
The dispatcher in the application instance has the command handlers that the
developer has provided. The component can dispatch those commands using
the dispatch() method of the dispatcher. For example, to remove a to-do
item from the list, the component can dispatch a remove-todo command like
this:
h(
'button',
{
on: {
click: () => dispatcher.dispatch('remove-todo', todoIdx)
}
},
['Done']
)
// --snip-- //
function renderApp() {
if (vdom) {
destroyDOM(vdom)
}
// --snip-- //
}
Bear in mind that, from now onward, components will receive two
arguments: the state and the emit() function. If a component doesn’t need to
dispatch commands, it can ignore the second argument.
You’re almost done! There’s only one thing missing: unmounting the
application.
To clean up the subscriptions and destroy the view, you need to add an
unmount() method to the application instance, as shown in bold in listing 5.8.
// --snip-- //
return {
mount(_parentEl) {
parentEl = _parentEl
renderApp()
},
unmount() {
destroyDOM(vdom)
vdom = null
subscriptions.forEach((unsubscribe) => unsubscribe())
},
}
}
The unmount() method uses the destroyDOM() from last chapter to destroy
the view, sets the vdom property to null, and unsubscribes the reducers and
the renderApp() function from the dispatcher.
That’s it! It’s a good time to review the code you wrote in app.js to make sure
you got it right.
5.2.6 Result
Your app.js should look like the code in listing 5.9.
function renderApp() {
if (vdom) {
destroyDOM(vdom)
}
return {
mount(_parentEl) {
parentEl = _parentEl
renderApp()
},
unmount() {
destroyDOM(vdom)
vdom = null
subscriptions.forEach((unsubscribe) => unsubscribe())
},
}
}
That’s the first version of your framework! Put together in less than 50 lines
of code, it’s a pretty simple framework, but it’s enough to build simple
applications.
In the next chapter, you build and publish your framework to NPM and
refactor the TODOs application to use it.
These commands are mapped to the browser’s click event on the buttons as
illustrated in figure 5.12.
Figure 5.12. The commands are mapped to the browser’s click event on the buttons
5.3.2 Exercise 5.2—Challenge
For the state, we need three things:
Let’s create a function that returns the initial state of the application:
function makeInitialState() {
return {
board: [
[null, null, null],
[null, null, null],
[null, null, null],
],
player: 'X',
winner: null,
}
}
At the start of the game, the board is empty, the current player is X, and
there’s no winner yet.
The only way players can interact with the application is by clicking on the
board, thus we only need one command, which we’ll call 'mark-cell'. This
command’s payload will be the coordinates of the cell that the player has
clicked on (the row and column).
The reducer function for the 'mark-cell' command will update the board,
switch the current player, and set who the winner is, if any. The reducer
function will throw an error if the player clicks on a cell that’s already
marked or if the player clicks on a cell that doesn’t exist—those cases should
be prevented by the view. Then, the reducer creates a new and updated board,
switches the current player, and checks if there’s a winner.
if (state.board[row][col]) { #2
throw new Error('Invalid move')
}
const newBoard = [ #3
...state.board[0],
...state.board[1],
...state.board[2]
]
newBoard[row][col] = state.player #4
return { #7
board: newBoard,
player: newPlayer,
winner,
}
}
First of all, copy and paste the code for the Dispatcher class into the
browser’s console. Then, create an instance of the Dispatcher class and
subscribe a handler for a command named 'greet'. Lastly, add an after-
command handler:
const dispatcher = new Dispatcher()
Now, call the dispatch() method with the command name 'greet' and the
payload 'John':
dispatcher.dispatch('greet', 'John')
5.4 Summary
Your framework’s first version is made of a renderer and a state
manager wired together.
The renderer first destroys the DOM (if it exists) and then creates it from
scratch. This isn’t very efficient and creates problems with the focus of
input fields, among other things.
The state manager is in charge of keeping the state and view of the
application in sync.
The developer of an application maps the user interactions to
commands, framed in the business language, that are dispatched to the
state manager.
The commands are processed by the state manager, updating the state
and notifying the renderer that the DOM needs to be updated.
The state manager uses a reducer function to derive the new state from
the old state and the command’s payload.
6 Publishing and using your
framework’s first version
This chapter covers
Publishing the first version of your framework to NPM
Refactoring the TODOs application to use your framework
In the previous chapter you implemented a state manager and assembled it,
together with a renderer, to build the first version of your frontend
framework. In this chapter, you’ll publish the first version of your
framework, and then refactor the TODOs application you built using vanilla
JavaScript to use it.
Important
You can find all the listings in this chapter in the listings/ch06 directory of
the book’s repository. The code in this chapter can be checked out from the
ch6 label:
$ git switch ch6
You want developers to use the h(), hString(), and hFragment() functions
to create virtual DOM nodes, and the createApp() function to create an
application. You don’t need to export the mountDOM() and destroyDOM()
functions, because they’re only used internally by the framework.
Open the src/index.js file, delete whatever is in there, and add the following
lines:
export { createApp } from './app'
export { h, hFragment, hString } from './h'
Now, run the build script inside the runtime workspace to build the
framework:
$ npm run build
As you’ve seen in appendix A, this script bundles the JavaScript code into a
single ESM file: dist/<fwk-name>.js. (Recall that <fwk-name> is the name of
your framework, as named in the package.json file’s name property.)
Important
Before you proceed, make sure you followed the instructions in appendix A
to set up your NPM account to publish your package.
You want the version field in your package.json file to be 1.0.0; this is the
first version of your framework, but you’ll be publishing more versions later.
"version": "1.0.0",
And that’s it: You’ve published the first version of your frontend framework
to NPM.
You don’t need to write the code in this section (unless you want to try it for
yourself); you can just read it and see how the framework works. You’ll get
your hands dirty in the next section.
I can’t think of anything simpler than a button that counts how many times
you click it. This application’s view is just a button, but it’s interactive: it
renders something different based on the state, which is a number
representing a count. You can implement this simple application with the
following few lines of code:
createApp({
state: 0,
reducers: {
add: (state, amount) => state + amount,
},
This is how little code you need to make a simple application with the
framework. No DOM manipulation drama; that’s handled by the framework
now so you can focus on the important stuff: the application’s logic.
When you click the button, the number it displays increments by one, and the
button renders the new number:
<body>
<button>1</button>
</body>
The framework removes the <button> element from the DOM and creates a
new one every time the state changes—that is, when you click the button. But
this happens in a fraction of a millisecond, so your eye can’t even notice it.
To you, it looks like the button is updating its number, but in reality, you
never click the same button twice. (How philosophical is that?)
Exercise 6.1
Using your framework, implement a counter application that allows the user
to increment and decrement a counter. The counter should start at 0 and the
user should be able to increment it by clicking on a button with a "+" (plus
sign) label. The user should also be able to decrement it by clicking on
another button, this time with the "-" (minus sign) label. The counter should
be displayed between the two buttons. You can create the application inside
the examples/ directory.
Let’s put the framework to use in the TODOs application, shall we?
6.3 Refactoring the TODOs app
Let’s now take the TODOs application you built without a framework and
refactor it to use the framework you just built. You’ll see how much simpler
the code becomes (except for the nuances of writing virtual DOMs instead of
HTML), and be in a good position to assess the benefits of using a frontend
framework versus writing your own DOM-manipulation code.
The first thing you want to do to refactor the TODOs application is to clean
up the <body> tag in the todos.html file. All the HTML markup will be
created by your framework this time, so the <body> tag should be empty.
Your todos.html file should look like listing 6.1.
Listing 6.1. Remove all the markup from the HTML file (todos.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<script type="module" src="todos.js"></script>
<title>My TODOs</title>
</head>
<body></body> #1
</html>
Do the same with the todos.js file; you’ll be writing all the code from scratch.
Then, import the functions exported by the framework from either the dist/
directory or, preferably, from unpkg.com. (I will be using the unpkg.com in
this book, but you can use whichever method you prefer.) Here’s what your
todos.js file should look like:
import { createApp, h, hFragment } from 'https://unpkg.com/<fwk-name>
Note
Please recall (and this will be the last time I mention it to avoid sounding
repetitive) that <fwk-name> is the name of your framework, as named in the
package.json file’s name property.
Next, you want to define the application’s state. This time, it’ll be a bit more
nuanced than a simple array of to-do items.
There are three pieces of information that you’ll need to keep in the state this
time:
edit: an object containing information about the to-do item being edited
by the user:
idx: the index of the to-do item in the todos array that’s being
edited.
original: the original text of the to-do item before the user started
editing it (in case the edition is cancelled and we need to bring the
original value back).
edited: the text of the to-do item as the user is editing it.
With this in mind, write the code in listing 6.2 to define the new state.
const state = {
currentTodo: '',
edit: {
idx: null,
original: null,
edited: null,
},
todos: ['Walk the dog', 'Water the plants'],
}
Let’s now think about the actions that the user can perform on the application
and how they affect the state.
Update the current to-do—The user types a new character in the input
field, so the current to-do needs to be updated.
Add a new to-do—The user clicks the add button to add a new to-do to
the list.
Start editing a to-do—The user double clicks a to-do item to start
editing it.
Edit a to-do—The user types a new character in the input field while
editing a to-do item.
Save an edited to-do—The user finishes editing a to-do and saves the
changes.
Cancel editing a to-do—The user cancels editing a to-do and discards
the changes.
Remove a to-do—The user marks a to-do as completed so it can be
removed from the list.
Write a reducer function for each of these actions below the state definition,
as shown in listing 6.3. Recall that a reducer must be a pure function, so it
can’t have side effects or mutate its arguments; it has to return a new state
object, not a modified version of the current state.
const reducers = {
'update-current-todo': (state, currentTodo) => ({ #1
...state,
currentTodo, #2
}),
return {
...state,
edit: { idx: null, original: null, edited: null }, #12
todos,
}
},
One interesting thing to note is that some events, like 'add-todo' don’t have
a payload associated with them. It isn’t necessary because the new to-do
description is now part of the state, so the reducer can access it directly:
'add-todo': (state) => ({
...state,
currentTodo: '',
todos: [...state.todos, state.currentTodo],
})
Now that you have the state and the reducers, you can define the view.
As you recall from earlier, the components are now functions that take in not
only the state, but also the emit() function to dispatch events to the
application’s dispatcher. Implement the CreateTodo() component next, as
shown in listing 6.5. This component is equivalent to the static HTML
markup you had in the todos.html file: the <label>, <input> and <button>
elements.
It’s the first time you see the emit() function being used inside a component,
so a few words of explanation are in order. The case of the <input> is
particularly interesting, because you’ve set up a two-way binding between the
input field and the state. A two-way binding reflects the changes in the state
in the input field and anything typed in the input field is set in the state.
Whatever side changes (the state or the DOM), the other is updated. You’ve
accomplished this two-way binding by using the value attribute of the
<input> element, and by setting an event listener on the oninput event:
h('input', {
type: 'text',
value: state.currentTodo,
on: {
input: ({ target }) => emit('update-current-todo', target.value)
}
})
This way, when the currentTodo in the state changes its value, this is
reflected in the input field, and when the user types a new character in the
input field, the oninput event is triggered, and the update-current-todo
event is dispatched, so the reducer updates the state. This is a beautiful put-
to-use of your renderer and state manager working together, as you can see in
figure 6.1.
Figure 6.1. A two-way binding between the state and the DOM
Let’s now implement the TodoList() component, which is the list of to-do
items. Write the following code below the CreateTodo() component (listing
6.6).
The TodoList() component is a simple one: it’s just a <ul> element with a
list of TodoItem() components. Implement the missing TodoItem()
component to finish the application (listing 6.7).
return isEditing
? h('li', {}, [ #1
h('input', {
value: edit.edited, #2
on: {
input: ({ target }) => emit('edit-todo', target.value) #3
},
}),
h(
'button',
{
on: {
click: () => emit('save-edited-todo') #4
}
},
['Save']
),
h(
'button',
{
on: {
click: () => emit('cancel-editing-todo') #5
}
},
['Cancel']
),
])
: h('li', {}, [ #6
h(
'span',
{
on: {
dblclick: () => emit('start-editing-todo', i) #7
}
},
[todo] #8
),
h(
'button',
{
on: {
click: () => emit('remove-todo', i) #9
}
},
['Done']
),
])
}
You’re passing the index the to-do item occupies in the list, because some of
the dispatched events need to know the index of the to-do item, like when
you want to edit or remove a to-do. That index is known by the parent
component, TodoList(), so it needs to pass it to the child component as a
prop. It isn’t part of the application’s state—what we typically pass as the
first argument to the component—but it’s relevant to the component.
The only piece that you’re missing is putting everything together. Write the
last line of the todos.js file as follows:
createApp({ state, reducers, view: App }).mount(document.body)
If you now run the serve:examples script, you’ll be able to see the
application running in your browser:
$ npm run serve:examples
You can now add new to-do items, edit them, and remove them. Everything
works the same as before, but this time you wrote no DOM manipulation
code, so you should be proud of yourself for having built your first frontend
framework. Congratulations!
There’s one thing though that you’ve probably noticed when you attempted
to add a new to-do item: every keystroke you type in the input field removes
the focus from the input field, so you have to click it again to type the next
character. Can you guess why this is happening? It’s because every time the
state changes, your framework is destroying the DOM and re-creating it from
scratch. It means that the field where you wrote the last character is no longer
in the DOM; you’re writing in a new input field. (You never write two
characters in the same input field—oh philosophy!) This is far from ideal, but
worry not: you’ll fix this in the next chapter.
Exercise 6.2—Challenge
Write the tic-tac-toe game using your framework. If you need a refresher on
how the game works, you can find the rules in the Wikipedia page:
https://en.wikipedia.org/wiki/Tic-tac-toe.
The game should start with an empty board (you can use a <table>
element to represent the board).
The first player is always the X player. There should be a title that says
whose turn it is: "It’s X’s turn" or "It’s O’s turn".
When a player clicks on a cell, the cell should be filled with the player’s
symbol (X or O) and the turn should be passed to the other player.
When a player wins the game, a message should be displayed saying
who won the game: "Player X wins!" or "Player O wins!". The
remaining cells should be disabled.
When all the cells are filled and there’s no winner, a message should be
displayed saying that the game ended in a draw: "It’s a draw!".
You can use the same state and reducer you used in exercise 5.2, but this time
you’ll need to write the logic to determine if a player has won the game (the
checkWinner() function).
The next two chapters are two of the most challenging in the book: you’ll
write the reconciliation algorithm, thanks to which your framework can
update the DOM when the state changes, without needing to completely
destroy and recreate it. The reconciliation algorithm is complex, but it’s also
lots of fun to write, so I hope you’ll enjoy it. Be sure to grab a cup of coffee
and maybe go out for a relaxing walk before you move on to the next chapter.
If you’re into Yoga, you want to do a couple Sun Salutations. Whatever you
do, come back refreshed and ready to write some code!
6.4 Answers to the exercises
6.4.1 Exercise 6.1
You can find the solution in the examples/ch06/counter/ directory. As you
can see, for a such a simple application I used just an HTML file named
counter.html. Create a similar file for your application and write the
following base HTML code:
<html>
<head>
<title>Counter</title>
</head>
<body>
<h1>Counter</h1>
</body>
</html>
Then, add a <script> tag to the <head> of the HTML file, and import the
framework from the unpkg.com CDN. Define the state and reducers
objects:
<html>
<head>
<script type="module">
import {
createApp,
h,
hString,
hFragment,
} from 'https://unpkg.com/fe-fwk@1'
const state = { count: 0 }
const reducers = {
add: (state) => ({ count: state.count + 1 }),
sub: (state) => ({ count: state.count - 1 }),
}
</script>
<title>Counter</title>
</head>
<body>
<h1>Counter</h1>
</body>
</html>
</script>
<title>Counter</title>
</head>
<body>
<h1>Counter</h1>
</body>
</html>
<title>Counter</title>
</head>
<body>
<h1>Counter</h1>
</body>
</html>
Reload the browser and you should see the counter application working,
similar to figure 6.2.
<body>
<h1>Tic Tac Toe</h1>
</body>
</html>
We’ll need to come back to this file later to add some CSS styles. But let’s
first focus on the game.js file. In this file, you need to define the state object
and the markReducer() reducer function that creates a new state object based
on the current state and the mark that was played. You wrote this function in
exercise 5.2, so you can copy it from there:
export function makeInitialState() {
return {
board: [
[null, null, null],
[null, null, null],
[null, null, null],
],
player: 'X',
draw: false,
winner: null,
}
}
if (state.board[row][col]) {
throw new Error('Invalid move')
}
const newBoard = [
[...state.board[0]],
[...state.board[1]],
[...state.board[2]],
]
newBoard[row][col] = state.player
return {
board: newBoard,
player: newPlayer,
draw,
winner,
}
}
if (checkColumn(board, i, player)) { #2
return player
}
}
if (checkMainDiagonal(board, player)) { #3
return player
}
if (checkSecondaryDiagonal(board, player)) { #4
return player
}
return null #5
}
Next, let’s work on the view of the application. In the tictactoe.js file, you
need to import your framework, and then define the View() component.
We’ll break down the view into two components: the Header() and Board():
import { createApp, h, hFragment } from 'https://unpkg.com/fe-fwk@1'
function Header(state) {
// TODO: implement me
}
if (state.draw) { #2
return h('h3', { class: 'draw-title' }, [`It's a draw!`])
}
Let’s focus on the board now. We’ll use a <table> element for it where every
row is rendered by another component, which we’ll call Row(). When a
player has won or there’s a draw, we want to add a CSS class to the <table>
element to prevent the user from clicking on the cells:
function Board(state, emit) {
const freezeBoard = state.winner || state.draw
function Header(state) {
if (state.winner) {
return h('h3', { class: 'win-title' },
[`Player ${state.winner} wins!`])
}
if (state.draw) {
return h('h3', { class: 'draw-title' }, [`It's a draw!`])
}
createApp({
state: makeInitialState(),
reducers: {
mark: markReducer,
},
view: View,
}).mount(document.body)
Last, let’s add some CSS to make the application look nicer. Inside the
tictactoe.html file, add the following <style> element, with the CSS rules:
<html>
<head>
<script type="module" src="tictactoe.js"></script>
<style>
body {
text-align: center;
}
table {
margin: 4em auto;
border-collapse: collapse;
}
table.frozen {
pointer-events: none;
}
tr:not(:last-child) {
border-bottom: 1px solid black;
}
td {
width: 6em;
height: 6em;
text-align: center;
}
td:not(:last-child) {
border-right: 1px solid black;
}
td.winner {
background-color: lightgreen;
border: 3px solid seagreen;
}
.win-title {
color: seagreen;
}
.draw-title {
color: darkorange;
}
.cell {
width: 100%;
height: 100%;
cursor: pointer;
}
.cell-text {
font-size: 3em;
font-family: monospace;
}
</style>
<title>Tic Tac Toe</title>
</head>
<body>
<h1>Tic Tac Toe</h1>
</body>
</html>
And that’s it! Your tic-tac-toe application is ready to be played, and it should
look similar to figure 6.3.
Figure 6.3. The tic-tac-toe application: in the first image, it’s player O’s turn, in the second
image, player X has won. The third image shows the draw case.
It might not be the first time you implement a tic-tac-toe game, but I’m pretty
confident it’s the first one you implement using a framework you built
yourself. Go ahead and add that to your resume!
6.5 Summary
To publish your framework on NPM, you first bundle it using npm run
build, then you publish it using the npm publish command.
Whatever you export from the src/index.js file is what’s going to be
available to the users of your framework. (You configured it this way in
appendix A.)
In a two-way binding, the state and the DOM are synchronized:
whatever changes in one side is reflected in the other.
Using the first version of your framework, you’ve rewritten the to-do
application you wrote in chapter 2. This time, you didn’t have to worry
about DOM manipulation, but only about the application’s logic.
7 The reconciliation algorithm:
diffing virtual trees
This chapter covers
Comparing two virtual DOM trees
Finding the differences between two objects
Finding the differences between two arrays
Finding a sequence of operations that transform one array into another
Picture this: you’re in the supermarket doing the groceries using the shopping
list your partner gave you. You walk around the aisles, picking up the items
one by one, and putting them in the cart. When you’re done, you head to the
checkout counter, but at that very moment something vibrates in your pocket
—it must be a message from your partner. She realized you already had a
dozen eggs in the fridge, and what’s actually missing is a bottle of wine for
that fancy dinner you’re going to have tonight with some friends. She texts
you the updated shopping list.
You now have two lists: the original one—whose items are already in your
cart—and the updated one, as you can see in figure 7.1.
Figure 7.1. The two shopping lists: the original one and the updated one
You have two options:
1. You can start over, emptying the cart and putting back the items to the
shelves, then picking up the items in the updated list. This way you
make sure you have the right items—the ones in the updated list—in
your cart.
2. You can pause for a minute, compare the two lists and figure out what
items where removed and what items were added. Then you can get rid
of the items that were removed from the original list, and pick the items
that were added to the updated list.
You’re a smart person (after all, you’re reading this book, aren’t you?), so
you choose the second option. It requires some extra brain power, but it’s less
labor-intensive. It only takes you a few seconds to realize that, if you take the
original list, strike out the egg carton, and add the bottle of wine, you’ll have
the updated list (figure 7.2). You figured out the minimum number of
operations you need to have the items in the new list in your cart.
Figure 7.2. The updated shopping list: the original list with the egg carton struck out and the
bottle of wine added
This analogy perfectly illustrates what the reconciliation algorithm is about:
comparing two virtual DOM trees, figuring out what changes, and applying
those changes to the real DOM. Destroying the entire DOM and recreating it
from scratch every time the state changes is like emptying the cart and
picking the new items from scratch every time there’s a new shopping list:
you don’t need to think, but it’s laborious. You want your framework to be
smart to figure out the minimum number—or at least a reasonably small
number—of operations to apply to the real DOM to have it reflect the new
state.
The diagram in figure 7.3 shows how your framework is currently rendering
the view when the state changes. Destroying and recreating the DOM every
time the state changes is an expensive operation. In a frontend application,
the state can change a couple times every second, so imagine if your
shopping list changed that often and you had to empty your cart and start
over every time your partner sent you an updated shopping list.
Figure 7.3. The current framework destroys and recreates the DOM every time the state changes.
This is an expensive operation.
The objective of this chapter is to implement the first half of the
reconciliation algorithm (you’ll implement the rest in the next chapter). This
algorithm does two things:
1. It figures out the differences between two virtual DOM trees (this
chapter), and
2. It applies those differences to the real DOM, so that the framework can
update the DOM in a more efficient way (next chapter).
The same way that when you get a new shopping list, you have to first
compare it with the original one, find the differences, and then make the
changes to the items in your cart. You have to compare the old virtual DOM
with the new virtual DOM, find the differences, and then apply those
differences to the real DOM. You’ll focus on the first part in this chapter.
Figure 7.4. The framework will update the DOM in a more efficient way, using the reconciliation
algorithm written in the patchDOM() function.
So, what are the mistery ingredients of the reconciliation algorithm?
Important
You can find all the listings in this chapter in the listings/ch07 directory of
the book’s repository.
The code you write in this chapter is for the framework’s second version,
which you’ll publish in chapter Chapter 8, The reconciliation algorithm:
patching the DOM. Therefore, the code in this chapter can be checked out
from the ch8 label:
$ git switch ch8
The two first functions are somehow straightforward, but the third is a bit
more complex. In fact, I’d say that it’s where 90% of the complexity of the
reconciliation algorithm lies. In this chapter, you’ll write these functions,
which you’ll use in the next chapter to implement the patchDOM() function.
Let’s start by exploring what we mean by "figuring out what changed and
applying those changes to the real DOM."
To compare two virtual DOM trees, you start comparing the two root nodes,
checking if they’re equal (we’ll see what it takes for two nodes to be equal)
and if their attributes or event listeners have changed. If you find that the
node is not the same, you look no further, destroy the subtree rooted at the
old node, and replace it with the new node and everything under it (we’ll
explore this more in detail later on). You then compare the children of the
two nodes, traversing the trees recursively in a depth-first manner. When you
compare two arrays of child nodes, you need to figure out if a node was
added, removed, or moved to a different position.
Figure 7.5. Comparing two virtual DOM trees to find what changed
Exercise 7.1
Step one. You compare the two root <div> nodes (figure 7.6) and find the id
attribute changed from "abc" to "def", and the style attribute changed from
"color: blue" to "color: red". These changes, labelled as "1. Attribute
modified" and "2. Style modified" in figure 7.5, are the first changes you
need to apply to the real DOM, as we’ll see below.
Step two. You then compare the two children of the <div> node one by one.
The first child is a <p> element (figure 7.7), and it seems to be in the same
position in both trees—it didn’t move. It’s class attribute has changed from
"foo" to "fox". This change is labelled as "3. Class modified" in figure 7.5,
and is the next change to apply to the real DOM.
Figure 7.7. Comparing the first child of the <div> nodes: a <p> element
Figure 7.9. Comparing the second child of the <div> nodes: a <p> element in the old tree, and a
<span> element in the new tree
Step five. Then you look at the <p> node that you know moved one position
(figure 7.10), and see that its class attribute changed from ["foo", "bar"]
to ["baz", "bar"], which means that the class "foo" was removed and the
class "baz" was added. This change is labelled as "6. Class added and class
removed" in figure 7.5.
Figure 7.10. Comparing the <p> element that moved one position to the right
Step six. Last, you check the children of the <p> element (figure 7.11), and
find that the text content changed from "World" to "World!". This change is
labelled as "7. Text changed" in figure 7.5.
Figure 7.11. Comparing the text nodes of the <p> element that moved one position to the right
After comparing the two trees, you found a total of seven changes that need
to be applied to the real DOM. The next step is to apply them.
Note
Figure 7.12. The HTML markup of the old virtual DOM tree
You’ll now apply the changes you identified by comparing the two trees in
the previous section. Applying a change means modifying the real DOM to
match the new virtual DOM tree. Every change we make will be shown in the
figure, so you can see how the DOM changes with each operation.
The first two changes were to the root <div> node’s attributes, as shown in
figure 7.13. To apply these changes, you’d need to update the id and style
attributes of the <div> node:
div.id = 'def'
div.style.color = 'red'
Figure 7.13. Applying the attribute and style changes to the DOM’s <div> node
Modifying the class attribute and text content of the first <p> node
Then go the changes to the first <p> node, as shown in figure 7.14. In this
case, you’d need to update the class attribute and the text content of the <p>
node:
p1.className = 'fox'
p1Text.nodeValue = 'Hi'
The next change is to add the <span> node, as shown in figure 7.15. To add a
new node (and its children) to the DOM, you can pass the virtual node to the
mountDOM() function you wrote earlier, passing the <div> node as the parent
node:
mountDOM(spanVdom, div)
Note that, as is, the mountDOM() function would append the <span> node to
the children array of the <div> node, but that’s not what we want. We want to
specify that the <span> node should be inserted at the second position (index
1) of the children array. We’ll need to modify the mountDOM() function to
accept a third argument, the index where the new node should be inserted:
mountDOM(spanVdom, div, 1)
You will modify the mountDOM() function in the next chapter, but for now,
you can just assume that it takes an index as the third argument and inserts
the new node at that position.
Figure 7.15. Adding the new <span> node and its text node
Modifying the class attribute and text content of the second <p> node
Last, you reach the second <p> node. You don’t need to move it because
when the <span> node was added, it naturally moved the second <p> node
one position to the right. When we say that it was naturally moved, we mean
that the movement happened as the result of another operation, but it’s not a
move operation you need to explicitly do yourself. (Keep this in mind; it’ll be
important later on.) The class attribute changes from ["foo", "bar"] to
["baz", "bar"], so you need to change the classList property of the <p>
node:
p2.classList.remove('foo')
p2.classList.add('baz')
The text content changed from "World" to "World!", so you need to update
the text node, as follows:
p2Text.nodeValue = 'World!'
Figure 7.17. Changes in the rendering mechanism, using the patchDOM() function
You need to modify the code so the renderApp() function doesn’t
completely destroy and recreate the DOM anymore; and this is thanks to the
patchDOM() function. patchDOM() takes the last saved virtual DOM (stored in
the vdom variable inside the application’s instance), the new virtual DOM
resulting from calling the view() function, and the parent element (stored in
the parentEl instance) of the DOM, where the view was mounted, and
figures out the changes that need to be applied to the DOM.
Let’s reflect this important change in the code. Open the app.js file, and make
the changes shown in listing 7.1.
// --snip-- //
function renderApp() {
if (vdom) {
destroyDOM(vdom)
}
const newVdom = view(state, emit) #1
mountDOM(vdom, parentEl)
vdom = patchDOM(vdom, newVdom, parentEl) #2
}
return {
mount(_parentEl) {
parentEl = _parentEl
renderApp()
vdom = view(state, emit)
mountDOM(vdom, parentEl)
#3
},
// --snip-- //
}
}
Warning
You’re importing the patchDOM() from the patch-dom.js file, which you’ll
write in the next chapter (your IDE might complain that such file doesn’t
exist, but that’s okay for now).
Now that you’ve changed the rendering flow, let’s start thinking about the
three functions used to compare two virtual DOM trees: objectsDiff(),
arraysDiff(), and arraysDiffSequence().
Exercise 7.2
Implement a check that doesn’t allow the user to call the mount() method
more than once to prevent the same application from being mounted multiple
times. If you detect that the application was already mounted, throw an error.
To illustrate this let’s look at the example in figure 7.18, the attributes of an
<input> element. In the example, there’s an old object (the one on top) and a
new object (the one on the bottom). By observing the two objects, you can
see that the min key was added in the new object, the max key was removed,
and the disabled key changed from false to true.
Figure 7.18. Finding the difference between two objects: what keys were added, removed, or
changed?
This exercise that you’ve done by mere observation is what the
objectsDiff() function that you’ll implement now does. The code you write
works very similar to your observation, described in the following list:
1. Take a key in the old object. If you don’t see it in the new object, you
know that the key was removed. Repeat with all keys.
2. Take a key in the new object. If you don’t see it in the old object, you
know that the key was added. Repeat with all keys.
3. Take a key in the new object. If you see it in the old object, and the
value associated with the key is different, you know that the value
associated with the key changed.
Create a new file inside the utils folder called objects.js, and write the
objectsDiff() function as shown in listing 7.2.
return {
added: newKeys.filter((key) => !(key in oldObj)), #1
removed: oldKeys.filter((key) => !(key in newObj)), #2
updated: newKeys.filter(
(key) => key in oldObj && oldObj[key] !== newObj[key] #3
),
}
}
Exercise 7.3
Implement a set of test cases to cover all the possible scenarios for the
objectsDiff() function. If you’re not familiar with unit testing, try to think
of the different cases your function should handle, and execute the function in
the Browser’s console with different inputs to see if it works as expected.
Let’s now look at the second function that you’ll implement, one that does a
similar job for arrays.
When you compare two virtual nodes, you need to find the differences
between the classes of the two nodes (the class array) to patch the DOM
accordingly (as you did in the example of figure 7.5, in step 5). In this case, it
doesn’t matter if the items in the array are in a different order, we care about
only the items that were added or removed. For this, you’ll implement the
arraysDiff() function next. But first, let’s look at the example in figure
7.19.
In the example, there’s an old array (the one on top) and a new array (the one
on the bottom). When we compare the two arrays, we see the following:
Figure 7.19. Finding the difference between two arrays: what items were added or removed?
Note that in this comparison, items are either added or removed, but they are
never changed. If an item is changed, we detect it as a removal of the old item
and an addition of the new item.
With this in mind, open the utils/arrays.js file (where you previously
implemented the withoutNulls() function) and write the arraysDiff()
function as shown in listing 7.3.
Warning
With these two out of the way, you’re ready to tackle the beast: the
arrayDiffSequence() function. That’s the function were we might sweat a
bit. But once you’ve written that function, the rest of the book will feel like a
breeze (or something like that). Take a deep breath, and let’s move on.
If you’re given two arrays and I ask you to come up with a list of add,
remove, and move operations that transform the first array into the second
one, how would you go about it? You can see an example in figure 7.20. In
this example, the old array is [A, B, C] and the new array is [C, B, D], and
I was able to find a sequence of three operations that, if applied in order,
transform the old array into the new array.
Figure 7.20. Finding a sequence of operations that transform the original array into the new one
Important
Notice how the move operation’s from index is different from the original
index that C had in the old array? C appeared at i=2, but after removing A, it
moved to i=1. The indices in the operations are always relative to how they
find things in the array at the moment of the operation. But you still want to
keep track of the original index of the item, and I’ll explain why in a moment.
There are many other sequences of operations (infinite, in fact) that would
yield the same result. In other words, the sequence of operations that
transforms the old array into the new array is not unique.
Exercise 7.4
Can you find another sequence of similar operations that would also
transform the old array into the new array?
You’ll use a noop operation when you find an item that’s in both arrays,
which requires no action to stay where it ends up. Notice how I said ends up
and not starts at; this distinction is important.
There are items that move naturally to their final position, because items are
added, removed or moved around them. For example, if we want to go from
[A, B] to [B], we just need to remove A. B falls into place naturally: without
any explicit move operation it goes from i=1 to i=0.
When an item moves naturally, you need to include a noop operation in the
sequence. This helps you keep track of the items that moved naturally, from
where they started to where they ended up. You need this operation because
patching the DOM is a recursive process: each of the items in the children
array that moved around also need to be compared to look for differences.
For this to be possible, you need to know where each item started and where
it ended up, in other words, their initial and final indexes in the array. This
will become much clearer in the next chapter.
The following list is the operations and the data they need for them to be
completely characterized:
As you can see, in all cases, the object that describes the operation has an op
property that indicates the type of operation. The item property is the item
that’s being added, removed, or moved (naturally or forcefully). The index
property is the index where the item ends up, except in the case of a remove
operation, where it’s the index where the item was removed from. For the
move and noop operations, the originalIndex property indicates the index
where the item started. Last, for the move operation, we also keep track of the
from property, which is the index where the item was moved from. Note that
in the case of a move operation, the from and originalIndices need not be
the same.
Exercise 7.5
Apply the operations in the previous sequence to the old array ([A, B, C]),
and check that you end up with the new array ([C, B, D]). You’ll likely be
doing this by hand a couple times this chapter, to debug the code you’ll write.
Let’s now talk about how the algorithm to find the sequence of operations
works.
Going one item at a time and modifying a copy of the old array ensures that
every operation is performed over the updated indices of the old array. When
the new array is completely iterated over, any excess items in the old array
are removed; the new array doesn’t have them, so they’re not needed.
3. If newItem == oldItem:
This algorithm is simpler than it appears at first sight. Figure 7.21 shows a
sequence diagram of the algorithm with the same steps as the list above. I’ll
use it later when you have to implement the algorithm in code, to show you
exactly where you are in the process. It’s a complex algorithm, so I want to
make sure you don’t get lost.
oldArray = [X, A, A, B, C]
newArray = [C, K, A, B]
Step one (i=0). The X in the old array doesn’t appear in the new array, so you
want to remove it (figure 7.21).
Figure 7.22. At i=0 in the old array is X, which doesn’t appear in the new array: it’s a remove
operation.
After removing the item and adding the remove operation to the list of
operations, you keep the index at i=0 because you haven’t fulfilled the
condition of having the same item in both arrays at that index.
Step two (i=0). You find a C in the new array, but there’s an A in the old
array at that same index. Let’s look for the C in the old array, starting at i=0.
Oh! There’s one at i=3 (figure 7.22). Note that, if there were more than one C
in the old array, you’d choose the first one you find.
Figure 7.23. At i=0 there’s a C in the new array. We can move the C in the old array from i=4.
You add the move operation to the sequence of operations, and move the C to
i=0 in the old array. Note that in this case, the original index and the current
index of C are the same. I both cases C appears at i=0.
Step three (i=1). There’s a K in the new array, but an A in the old array. You
look for a K in the old array, starting at i=1. In this case there isn’t one, so
you need an add it (figure 7.23).
Figure 7.24. At i=0 is a K in the new array, but there’s no K in the old array. We need an add
operation.
You add the K to the old array at i=1, and append the operation to the
sequence of operations. Let’s move on.
Step four (i=2). At i=2 both arrays have an A—hooray! (figure 7.24). You
add a noop operation in these cases. For that, you need the original index of A
in the old array. But that A moved naturally due to the previous operations:
It moved one position to the left when the X was removed in step one.
It moved one position to the right when the C was moved in step two,
which cancelled the previous move.
It moved one position to the right when the K was added in step three.
In total, the A moved one position to the right, so you need to subtract one
from the current index of the A in the old array. So, the original index of the A
is i=1.
Figure 7.25. At i=2 both arrays have an A. We add a noop operation, using the original index of
the A in the old array.
You want to keep track of the old array items original indexes, so you have
them available when you add noop and move operations. As you can
imagine, calculating the original index of an item based on the previous
operations isn’t complicated, but it’s much better if you just save it at the
beginning of the process—keep this in mind. Let’s move on to the next step.
Step five (i=3). At i=3 the new array has a B, but the old array has an A. Look
for a B in the old array, starting at i=3. There’s one at i=4, so you can move it
(figure 7.25).
Figure 7.26. At i=3 there’s a B in the new array. We can move the B in the old array from i=4.
You append the move operation to the sequence of operations, and move the
B to i=3 in the old array.
At this point, you’re done iterating the elements in the new array, and all
items from i=0 to i=3 in the old array are aligned with the new array. You’re
only missing to remove what’s left in the old array: the excess items that
don’t appear in the new array.
Step five. At i=4 in the old array is an extra A that you want to remove
(figure 7.26).
Figure 7.27. At i=4 in the old array is an extra A that we need to remove.
In fact, if there were more items in the old array, you’d want to remove them
as well. All of those remove operations would be at index i=4, because as an
item is removed, the next one occupies its place at that index.
I hope the algorithm is clear now after working out an example by hand. It’s
time to implement it in code.
You want a way of keeping track of the old array original indices, so when
you modify a copy of the old array as you apply each operation, you can still
keep the original indices. A good solution is to create a class, let’s call it
ArrayWithOriginalIndices, that wraps a copy of the old array and keeps
track of the original indices. Whatever item is moved inside that array
wrapper, the original index is also moved.
This wrapper class is going to search for a given item in the wrapped array
(like in step two of the previous example, where you looked if there was a C
somewhere in the array) for which you want a custom comparison function.
The items inside the array will be virtual nodes in the next chapter, and you
want a function that tells you if two virtual nodes are equal. (You’ll
implement this function that checks if two virtual nodes are equal in the next
chapter.)
Warning
Recall that, for JavaScript, two objects are equal if they’re the same reference
—that is, the same object in memory. Because our virtual nodes are objects,
you need to define a custom function to compare them. You can’t rely on the
default === comparison, because if a virtual node is the same as another one
but it’s a different object, the comparison will return false, and that’s not
what you want.
Listing 7.4. The ArrayWithOriginalIndices class wraps an array and keeps track of the original
indices (utils/arrays.js)
class ArrayWithOriginalIndices { #1
#array = []
#originalIndices = []
#equalsFn
constructor(array, equalsFn) {
this.#array = [...array] #2
this.#originalIndices = array.map((_, i) => i) #3
this.#equalsFn = equalsFn #4
}
get length() { #5
return this.#array.length
}
}
return sequence #4
}
Giving the equalsFn parameter a default value using the default equality
operator (===) will be handy, as you’ll test this function passing it arrays of
strings in this chapter, mostly. Let’s start with the remove case.
To find if an item was removed, you check if the item in the old array at the
current index doesn’t exist in the new array. This branch in the sequence
diagram is shown in figure 7.28.
Figure 7.28. At i=4 in the old array is an extra A that we need to remove.
Implement this logic inside a method called isRemoval() in the
ArrayWithOriginalIndices class, as shown in bold font in listing 7.6.
class ArrayWithOriginalIndices {
#array = []
#originalIndices = []
#equalsFn
constructor(array, equalsFn) {
this.#array = [...array]
this.#originalIndices = array.map((_, i) => i)
this.#equalsFn = equalsFn
}
get length() {
return this.#array.length
}
isRemoval(index, newArray) {
if (index >= this.length) { #1
return false
}
And now, let’s implement a method to handle the removal of an item and
return the operation. Yoy want to reflect the removal operation both in the
#array and #originalIndices properties of the ArrayWithOriginalIndices
instance. Add the removeItem() method to the ArrayWithOriginalIndices
class, as shown in listing 7.7.
Listing 7.7. Removing an item from the old array and returning the operation (utils/arrays.js)
class ArrayWithOriginalIndices {
// --snip-- //
isRemoval(index, newArray) {
// --snip-- //
}
removeItem(index) {
const operation = { #1
op: ARRAY_DIFF_OP.REMOVE,
index,
item: this.#array[index], #2
}
this.#array.splice(index, 1) #3
this.#originalIndices.splice(index, 1) #4
return operation #5
}
}
And with these two methods, you can implement the remove case in the
arraysDiffSequence() function. Add the code in bold font in listing 7.8 to
the arraysDiffSequence() function.
return sequence
}
The noop case happens when at the current index, both the old and new
arrays have the same item. Thus, detecting this case is straightforward. You
can see the noop case highlighted in the sequence diagram in figure 7.29.
class ArrayWithOriginalIndices {
// --snip-- //
isNoop(index, newArray) {
if (index >= this.length) { #1
return false
}
Once you detect there’s a noop at the current index, you need to return the
noop operation. And here’s where the original indices come in handy.
class ArrayWithOriginalIndices {
// --snip-- //
isNoop(index, newArray) {
// --snip-- //
}
originalIndexAt(index) {
return this.#originalIndices[index] #1
}
noopItem(index) {
return { #2
op: ARRAY_DIFF_OP.NOOP,
originalIndex: this.originalIndexAt(index), #3
index,
item: this.#array[index], #4
}
}
}
As you can see, you don’t need to do anything to the old array to reflect the
noop operation. Things stay the same. With these methods in place you can
go ahead and implement the noop case in the arraysDiffSequence()
function. Write the code in bold font in listing 7.11 inside the
arraysDiffSequence() function.
if (array.isNoop(index, newArray)) { #1
sequence.push(array.noopItem(index)) #2
continue #3
}
return sequence
}
To check if an item was added in the new array, you need to check if that
item doesn’t exist in the old array, starting from the current index. Figure
7.30 shows the addition case in the sequence diagram.
class ArrayWithOriginalIndices {
// --snip-- //
isAddition(item, fromIdx) {
return this.findIndexFrom(item, fromIdx) === -1 #1
}
findIndexFrom(item, fromIndex) {
for (let i = fromIndex; i < this.length; i++) { #2
if (this.#equalsFn(item, this.#array[i])) { #3
return i
}
}
return -1 #4
}
}
Dealing with an addition is as simple as adding the item to the old array and
returning the add operation. You have to add an entry to the
#originalIndices property, but the added item wasn’t present in the original
old array, so you can use a -1 in this case.
Listing 7.13. Adding an item to the old array and returning an add operation (utils/arrays.js)
class ArrayWithOriginalIndices {
// --snip-- //
isAddition(item, fromIdx) {
return this.findIndexFrom(item, fromIdx) === -1
}
findIndexFrom(item, fromIndex) {
for (let i = fromIndex; i < this.length; i++) {
if (this.#equalsFn(item, this.#array[i])) {
return i
}
}
return -1
}
addItem(item, index) {
const operation = { #1
op: ARRAY_DIFF_OP.ADD,
index,
item,
}
this.#array.splice(index, 0, item) #2
this.#originalIndices.splice(index, 0, -1) #3
return operation #4
}
}
These two methods make it easy to implement the addition case in the
arraysDiffSequence() function. Add the code in bold font in listing 7.14 to
the arraysDiffSequence() function.
if (array.isAddition(item, index)) { #2
sequence.push(array.addItem(item, index)) #3
continue #4
}
return sequence
}
The neat thing about the move case is that you don’t need to explicitly test for
it; if the operation isn’t a removal, an addition, or a noop, it must be a move.
This branch is highlighted in the sequence diagram in figure 7.31.
class ArrayWithOriginalIndices {
// --snip-- //
moveItem(item, toIndex) {
const fromIndex = this.findIndexFrom(item, toIndex) #1
const operation = { #2
op: ARRAY_DIFF_OP.MOVE,
originalIndex: this.originalIndexAt(fromIndex), #3
from: fromIndex,
index: toIndex,
item: this.#array[fromIndex],
}
return operation #8
}
}
Listing 7.16. Adding an item to the old array and returning an add operation (utils/arrays.js)
export function arraysDiffSequence(
oldArray,
newArray,
equalsFn = (a, b) => a === b
) {
const sequence = []
const array = new ArrayWithOriginalIndices(oldArray, equalsFn)
if (array.isNoop(index, newArray)) {
sequence.push(array.noopItem(index))
continue
}
if (array.isAddition(item, index)) {
sequence.push(array.addItem(item, index))
continue
}
sequence.push(array.moveItem(item, index))
}
return sequence
}
You’re almost there! You’re only missing to remove the outstanding items
from the old array.
What happens when you reach the end of the new array but there are still
items in the old array? You remove them all, of course. Figure 7.32 shows the
removal case in the sequence diagram.
Figure 7.32. The removal case in the sequence diagram
To remove the outstanding items, you want to remove all items past the
current index (which is the length of the new array) from the old array. You
can use a while loop that keeps removing items while the old array is longer
than the index. Write the code for the removeItemsAfter() method in bold
font in listing 7.17.
class ArrayWithOriginalIndices {
// --snip-- //
removeItemsAfter(index) {
const operations = []
return operations #3
}
}
Listing 7.18. Adding an item to the old array and returning an add operation (utils/arrays.js)
if (array.isNoop(index, newArray)) {
sequence.push(array.noopItem(index))
continue
}
if (array.isAddition(item, index)) {
sequence.push(array.addItem(item, index))
continue
}
sequence.push(array.moveItem(item, index))
}
sequence.push(...array.removeItemsAfter(newArray.length))
return sequence
}
Exercise 7.6
Save the resulting operations in a variable called sequence. Then pass the old
array and the sequence to the applyArraysDiffSequence() function and
check that the resulting array is the same as the new array.
7.7 Answers to the exercises
7.7.1 Exercise 7.1
The HTML for the old virtual tree is:
<div id="abc" style="color: blue;">
<p class="foo">Hello</p>
<p class="foo bar">World</p>
</div>
// -- snip -- //
return {
mount(_parentEl) {
if (isMounted) {
throw new Error('The application is already mounted')
}
parentEl = _parentEl
vdom = view(state, emit)
mountDOM(vdom, parentEl)
isMounted = true
},
unmount() {
destroyDOM(vdom)
vdom = null
subscriptions.forEach((unsubscribe) => unsubscribe())
isMounted = false
},
}
}
expect(added).toEqual([])
expect(removed).toEqual([])
expect(updated).toEqual([])
})
expect(added).toEqual(['foo'])
expect(removed).toEqual([])
expect(updated).toEqual([])
})
expect(added).toEqual([])
expect(removed).toEqual(['foo'])
expect(updated).toEqual([])
})
expect(added).toEqual([])
expect(removed).toEqual([])
expect(updated).toEqual(['foo'])
})
case ARRAY_DIFF_OP.REMOVE:
array.splice(index, 1)
break
case ARRAY_DIFF_OP.MOVE:
array.splice(index, 0, array.splice(from, 1)[0])
break
}
return array
}, oldArray)
}
Now, if you pass the two arrays described in the exercise to the
arraysDiffSequence():
7.8 Summary
The reconciliation algorithm has two main steps: diffing and patching.
Diffing is the process of finding the differences between two virtual
trees.
Patching is the process of applying the differences to the real DOM.
Diffing two virtual trees to find their differences boils down to solving
three problems: finding the differences between to objects, finding the
differences between two arrays, and finding a sequence of operations
that applied to an array will transform it into another array.
8 The reconciliation algorithm:
patching the DOM
This chapter covers
Implementing the patchDOM() function
Using the objectsDiff() function to find the differences in attributes
and styles
Using the arraysDiff() function to find the differences between CSS
classes
Using the arraysDiffSequence() function to find the differences
between virtual DOM children
Using the Document API to patch the changes in the DOM
In the previous chapter, you saw how the reconciliation algorithm works in
two phases: finding the differences between two virtual DOM trees, and
patching those differences in the real DOM.
You implemented the three key functions to find differences between two
objects or two arrays: objectsDiff(), arraysDiff() and
arraysDiffSequence(). In this chapter, you’ll use these functions to
implement the reconciliation algorithm inside the patchDOM() function.
patchDOM() finds the differences between two virtual DOM trees (the one
that’s currently rendered and the one after the state has changed) and patches
the real DOM accordingly.
With the patchDOM() function, your framework will be able to update the
DOM using a small set of operations, instead of replacing the whole DOM
tree every time the state changes. Thanks to this function, the new version of
your framework—which you’ll publish at the end of this chapter—will be
much more efficient than the previous version. The best part is that, the
TODOs application rewrite you did in chapter 6 will still work with the new
version of your framework, as its public API doesn’t change. You’ll just need
to update the import statement to import the new version of the framework
instead of the old one. This time though, you won’t lose the focus from the
<input> fields every time you type a character, because the fields will be
updated without being replaced.
We’ll end the chapter looking at a few ways you can use browser developer
tools to inspect the DOM and observe how the reconciliation algorithm
patches small portions of the DOM.
Important
You can find all the listings in this chapter in the listings/ch08 directory of
the book’s repository. The code in this chapter can be checked out from the
ch8 label:
$ git switch ch8
Then, the user writes some invalid text in the <input> field (whatever
"invalid" means here), clicks the validate <button> and the error message
appears:
<form>
<input type="text"/>
<p class="error">Invalid text! Try something else</p>
<button>Validate</button>
</form>
As you can see, the error message is inserted between the <input> field and
the <button> element (at position i=1) inside the <form> element’s children.
This is illustrated in figure 8.1. Your current implementation of the
mountDOM() function doesn’t allow you to insert a node at a given index
inside a parent node.
Let’s write an insert() function inside the mount-dom.js file to insert a node
at a given index inside a parent node. You can then use this function inside
the mountDOM() function. You want the function to also append nodes at the
end without having to figure out what the index would be, because that’s still
a common use case. Here’s what you can do: if the passed in index is null or
undefined, append the node to the parent node.
Then, in the cases where the index is defined (neither null nor undefined),
you need to account for the following cases:
To insert a node at a given index, you can use the insertBefore() method of
the Node interface. This method requires two arguments: the new element to
insert and the reference element, which is the element before which the new
element will be inserted. Figure 8.2 illustrates how the insertBefore()
method would work for the example in figure 8.1.
In a blank HTML file, create a <div> element with two <p> children, as
follows:
<div>
<p>One</p>
<p>Three</p>
</div>
Then, use the insertBefore() method to insert a new <p> element between
the two existing ones. The new <p> element should contain the text Two.
Open the mount-dom.js file and write the insert() function inside it, below
the mountDOM() function (listing 8.1).
Listing 8.1. The insert() function (mount-dom.js)
if (index < 0) {
throw new Error(`Index must be a positive integer, got ${index}`)
}
Note
Recall that, depending on how you configured your linter, you might get a
complaint about using == instead of === (this comes from the "eqeqeq" rule).
You can safely ignore this warning; the == operator is fine here, because you
want to check for both null and undefined values.
With the insert() function in place, you can now modify the mountDOM()
function to accept an index as a third parameter (listing 8.2), so that you can
insert the node at that index. The index should be passed down to the text and
element node creation functions, which will then use the insert() function
to insert the node at the given index.
case DOM_TYPES.ELEMENT: {
createElementNode(vdom, parentEl, index)
break
}
case DOM_TYPES.FRAGMENT: {
createFragmentNodes(vdom, parentEl, index)
break
}
default: {
throw new Error(`Can't mount DOM of type: ${vdom.type}`)
}
}
}
parentEl.append(textNode)
insert(textNode, parentEl, index)
}
}
With these changes in place, let’s now turn our attention to the patchDOM()
function.
Figure 8.3. Comparing two virtual DOM trees to find what changed
We did the exercise of comparing the two virtual DOM trees by hand,
starting by the top-level <div> element, then moving on to its children in a
depth-first manner. Lets turn these steps from the exercise into an algorithm
that you can implement, the reconciliation algorithm.
The reconciliation algorithm compares two virtual DOM trees, finds the
sequence of operations that transform one into the other, and patches the real
DOM by applying those operations to it. The algorithm is recursive, and it
starts at the top-level nodes of both virtual DOM trees. After comparing these
nodes, it moves on to their children, and so on until it reaches the leaves of
the trees.
Thanks to the exercise we worked out by hand in the previous chapter, you
have a fair understanding of what the algorithm does. Let’s try to put that
knowledge into words. Here’s the algorithm, step by step:
A. adding a node—mount the new node (and its subtree) at the given
index.
B. removing a node—destroy the node (and its subtree) at the given
index.
C. moving a node—move the node to the new index. Start from step 1
using these as the new top-level nodes.
D. noop—start from step 1 using these as the new top-level nodes.
Note
The idea of destroying a DOM node and its subtree when it’s found to have
changed comes from React. You can read more about React’s reconciliation
algorithm at reactjs.org/docs/reconciliation.html. I’ll explain in a minute why
this is a good idea in practice.
The algorithm doesn’t look very complicated, but you’ll see the
implementation has a lot of details. The good news is that you already
implemented the most complex part of it in the previous chapter: the
arraysDiffSequence() function. You’ll use this function in a minute. Figure
8.4 shows the algorithm as a flowchart. Have this handy as you read the next
section; it’ll help you during the implementation.
The cases where a child node was added or removed are simple enough: you
simply call the mountDOM() or destroyDOM() functions, respectively. If you
recall, these functions already take care of mounting or destroying the whole
subtree. This is why the algorithm isn’t recursive in these cases. As you can
see, in figure 8.4, the add and remove branches after the patchChildren()
call don’t go back to the top of the algorithm, but rather end the algorithm.
To patch the children, you need to first extract the children arrays from the
two nodes. When you find a fragment node in the children array, instead of
keeping it, you want to extract its children. This is illustrated in figure 8.5,
where you can see that the fragment nodes at every level are replaced by its
children.
Figure 8.5. The child nodes of a fragment are extracted and added to the parent’s children array
You want to extract the children of a fragment node so that the trees that the
reconciliation algorithm works with are as similar as possible to the real
DOM, and if you recall, fragments aren’t part of the DOM; they’re just a way
to group nodes together. In the example illustrated in figure 8.5, the DOM
would look like this:
<p>
<span>Once</span>
<span>upon</span>
<span>a</span>
<span>time</span>
</p>
As you can see, there is no trace of the fragments in the real DOM. So, in
short, fragments never make it to the reconciliation algorithm, because there
aren’t any fragments in the real DOM.
The last thing that I want you to notice is step 2, where if you find two top-
level nodes that are different, you destroy the whole subtree to mount the new
one. There are two things that require some explanation here. First of all,
what do we mean when we say that two nodes are different? Second, why do
you have to destroy the whole subtree? Doesn’t that sound a bit wasteful?
You want to know when two nodes are equal so that you can reuse the
existing DOM node; if not, it needs to be destroyed and replaced with a new
one. Reusing an existing DOM node and just patching its properties is much
more efficient than destroying it and mounting a new one, so you want to
reuse nodes as much as possible. For two virtual nodes to be equal, first they
need to be of the same type. A text node and an element node can’t never be
equal, for example, because you wouldn’t be able to reuse the existing DOM
node. When you know the two nodes you’re comparing are of the same type,
the rules are the following:
text nodes—two text nodes are always equal (even if their nodeValue is
different).
fragment nodes—two fragment nodes are always equal (even if they
contain different children).
element nodes—two element nodes are equal if their tagName properties
are equal.
Note
You could definitely change the tag of a <div> to a <span> in the HTML
source using the browser’s developer tools, and that would work perfectly
fine. But what’s happening under the hood is that the browser is destroying
the old HTMLDivElement and creating a new HTMLSpanElement. That’s exactly
what you need to do in your code using the Document API if you compare
two element nodes and find the tag changed: destroy the node with the old
tag and mount a new one with the new tag.
With this in mind, let’s implement a function to check if two nodes are equal:
nodesEqual(). If you look at the algorithm’s flowchart 8.7, the equality
check happens at the top, when the old and new nodes enter the algorithm
(highlighted in the figure).
Figure 8.7. Checking if the old and new nodes are equal in the algorithm’s flowchart
To create a function to check if two nodes are equal, create a new file called
nodes-equal.js and write the code in listing 8.6. The areNodesEqual()
function applies the aforementioned rules to check if two nodes are equal.
return true
}
Exercise 8.2
Paste the areNodesEqual() function into the browser’s console (don’t forget
to also include the DOM_TYPES constant) and test it with the following cases:
Now that you have a function to check if two nodes are equal, you can use it
in the algorithm to decide whether to destroy the old node and mount the new
one. This was our second question: why do you have to destroy the whole
subtree? Let’s look into that.
8.2.3 Subtree change
Destroying the whole subtree and recreating it when you detect the node
changed sounds like going back to when your framework removed the whole
DOM tree and mounted a new one. Isn’t that what we’re trying to avoid with
the reconciliation algorithm? Couldn’t you just compare their children to
check if at least the subtrees are the same so that you need to recreate only the
nodes that are different? This is illustrated in figure 8.8.
Figure 8.8. The top node’s tag changed, but the subtrees are the same
In figure 8.8, the top nodes—the ones you’re comparing—are different, but
their subtrees are the same. You could patch just the top node and leave the
subtrees alone. If the children are different, you can patch only them with the
help of the arraysDiffSequence() function.
What happens in general is that, when you detect that a node has changed, it
usually means that a new part of the view enters the scene while the old one
leaves. This new part of the view typically has a different subtree below it,
completely unrelated to the old subtree. If you decided to patch the top node
and then start comparing the two subtrees, you’d find a lot of differences,
because the new subtree is completely different from the old one. You’d end
up doing a lot of comparisons and DOM operations that are equivalent to
destroying the whole subtree and recreating it from scratch, only using many
more operations.
So, a more realistic situation than the one in figure 8.8 is the one in figure
8.9, where you can see that when a parent node changes, their subtrees are
likely to be completely different. When you go from having a <form> to
having a <div>, you can probably anticipate that there will be something
different below it. And if it’s not the case, if the parent node changes but the
subtree is the same, destroying and recreating the whole subtree is a price
you’re willing to pay for the simplicity of the algorithm.
Figure 8.9. A more realistic case where the top nodes are different and so are their subtrees
Having illustrated why it’s better to simply destroy the whole subtree and re-
create it when the top virtual nodes being compared are different, let’s
implement the algorithm. You’ll start writing the patchDOM() function, which
is the main entry point of the algorithm. You’ll add more cases to the
function as we go, but for now, you’ll just implement the case where the top
nodes are different—the easiest one to implement.
You first find the index of the old node’s element (referenced by the el
property of the virtual node) in its DOM’s parent node (referenced by the
parentEl property), destroy it, and then mount the new node in the same
place. To find this index, you can use the indexOf() method of the parent
element’s list of child nodes (parentEl.childNodes), passing it the old
node’s DOM element. One case to bear in mind, though, is when the index
returned by indexOf() is -1, which means that the old node’s DOM element
is not among the parent children. This case when the old node is a fragment,
where the el property references the parent element; you can’t find an
element inside itself. For this case, we want to just use a null index, which
means the new vdom will be appended at the end of the parent node’s list of
child nodes.
Figure 8.10. Replacing the old DOM with the new DOM
Create a new file called patch-dom.js and write the code in listing 8.7.
Listing 8.7. Patching the DOM when the top nodes are different (patch-dom.js)
return newVdom
}
}
As you can imagine, the first thing you need to do in the patchDOM() function
—just after the areNodesEqual() comparison from the previous section—is
check whether the nodes are text nodes. If so, you call a function to patch the
text node, patchText(), which you need to implement. You can use a switch
statement to check the type of the nodes and act accordingly.
Figure 8.11 illustrates where the patching of a text node is in the algorithm’s
flowchart.
Figure 8.12. Copying the DOM element reference from the old virtual node to the new one
Modify the patchDOM() function, adding the code in bold font in listing 8.8.
return newVdom
}
newVdom.el = oldVdom.el #1
switch (newVdom.type) {
case DOM_TYPES.TEXT: {
patchText(oldVdom, newVdom) #2
return newVdom #3
}
}
return newVdom
}
Let’s now write the patchText() function. This function compares the texts
in the nodeValue property of the old and new virtual nodes. If they are
different (read: the text has changed), set the nodeValue property of the DOM
element to the new text.
Note how, in the first line of the function, you’re extracting the DOM
element from the oldVDom virtual node’s el property, but you could have also
used the newVDom virtual node’s reference, because before calling this
function, patchDOM() has already saved it there. (At this point oldVdom.el
=== newVdom.el must be true.)
Let’s now move on to the case where the top nodes are element nodes. This is
the most interesting—as well as complex—case.
return newVdom
}
newVdom.el = oldVdom.el
switch (newVdom.type) {
case DOM_TYPES.TEXT: {
patchText(oldVdom, newVdom)
return newVdom
}
case DOM_TYPES.ELEMENT: {
patchElement(oldVdom, newVdom)
break
}
return newVdom
}
Element nodes are more complex than text nodes, because they have
attributes, styles, event handlers, and can have children. You’ll take care of
patching the children of a node in the next subsection, which incidentally
covers the case of fragment nodes (these nodes are no more than an array of
children nodes). But you’ll focus on the attributes, styles (including CSS
classes), and event handlers in this section.
Write the code for the patchElement() function in listing 8.11, inside the
patch-dom.js file. (You don’t need to include the // TODO comments in the
code listing; they’re just there to remind you of what you of the functions you
need to write next.)
Now that you’ve broken down the work of patching element nodes into
smaller tasks, let’s start with the first one: patching attributes.
Patching attributes
The attributes of a virtual node are all of the key-value pairs that come inside
its props object—except for the class, style, and on properties, which have
a special meaning. Now, given the two objects containing the attributes of the
old and new virtual nodes (oldAttrs and newAttrs, respectively), you need
to find out which attributes have been added, removed, or changed. You
wrote a function in the last chapter that does exactly that: objectsDiff().
The objectsDiff() function tells you which attributes have been added,
removed, or changed. You can get rid of the attributes that have been
removed using the removeAttribute() function that you wrote earlier in the
book (inside the attributes.js file). The attributes that have been added or
changed can be set using the setAttribute() function, that you also wrote
earlier.
Write the code for the patchAttrs() function in listing 8.12 at the bottom of
the patch-dom.js file. (Don’t forget to include the new import statements at
the top of the file.)
Listing 8.12. Patching the attributes (patch-dom.js)
import {
removeAttribute,
setAttribute,
} from './attributes'
// --snip-- //
The tricky part—but’s not that tricky, really—about patching the CSS classes
is that they can come as a string (for example: 'foo bar'), or as an array of
strings (for example: ['foo', 'bar']). Here’s what you’ll do:
If the CSS classes come in an array, filter the blank or empty strings out
of it, and keep them as an array.
If the CSS classes come as a string, split it on whitespace, and filter the
blank or empty strings out of it.
This way, you work with two arrays of strings representing the CSS classes
of the old and new virtual DOMs. You can then use the arraysDiff()
function you implemented in the previous chapter to find out which CSS
classes have been added and removed. The DOM element has a classList
property (an instance of the DOMTokenList interface) that you can use to add
and remove CSS classes from the element. Add the new classes using the
classList.add() method, and remove the old ones using the
classList.remove() method. The classes that were neither added nor
removed don’t need to be touched—they can stay as they are.
Write the code for the patchClasses() function in listing 8.13, at the bottom
of the patch-dom.js file. Again, don’t forget to include the new import
statements at the top of the file, and this time, pay attention to a function that
you need to import, isNotBlankOrEmptyString(), that you’ll write in a
second.
import {
removeAttribute,
setAttribute,
} from './attributes'
import { destroyDOM } from './destroy-dom'
import { DOM_TYPES } from './h'
import { mountDOM } from './mount-dom'
import { areNodesEqual } from './nodes-equal'
import {
arraysDiff,
} from './utils/arrays'
// --snip-- //
if (removed.length > 0) {
el.classList.remove(...removed) #4
}
if (added.length > 0) {
el.classList.add(...added) #5
}
}
Create a new file under the utils directory, called strings.js, and write the
code for the isNotBlankOrEmptyString() function in listing 8.14.
Patching the style is similar to patching the attributes: you compare the old
and new style objects (using the objectsDiff() function), and then you add
the new or modified styles, and remove the old ones. To set or remove styles,
you can use the setStyle() and removeStyle() functions that you wrote
earlier in the book.
Inside the patch-dom.js file, write the code for the patchStyle() function in
listing 8.15. (Don’t forget to import the removeStyle() and setStyle()
functions at the top of the file.)
import {
removeAttribute,
setAttribute,
removeStyle,
setStyle,
} from './attributes'
import { destroyDOM } from './destroy-dom'
import { DOM_TYPES } from './h'
import { mountDOM } from './mount-dom'
import { areNodesEqual } from './nodes-equal'
import {
arraysDiff,
} from './utils/arrays'
import { objectsDiff } from './utils/objects'
import { isNotBlankOrEmptyString } from './utils/strings'
// --snip-- //
handler(event)
}
el.addEventListener(eventName, boundHandler)
return asyncHandler
}
You will understand why you want to do this soon, but for now, just know
that the functions defined in the virtual DOM to handle events are not the
same as the functions that are attached to the DOM. This is the reason why
the patchEvents() function needs the oldListeners object—the function’s
actually attached to the DOM. As you know, to remove an event listener from
a DOM element, you call its removeEventListener() method, passing it the
name of the event and the function that you want to remove, so we need the
functions that were used to attach the event listeners to the DOM.
For the rest, it’s just a matter of using the objectsDiff() function to find out
which event listeners have been added, modified, or removed, and then
calling the addEventListener() function and el.removeEventListener()
method correspondingly. In this case though, when an event listener has been
modified (that is, the event name is the same but the handler function is
different), you need to remove the old event listener and then add the new
one. Keep this detail in mind.
There are two very important things you should pay attention to in the code.
The first one is that, you use the function in oldListeners[eventName] to
remove the event listener, and not the function in oldEvents[eventName]. I
already explained why this is, but’s important that you write this line of code
correctly for your framework to work properly:
el.removeEventListener(eventName, oldListeners[eventName])
The second thing is that you use your own implementation of the
addEventListener() function to add the event listeners to the DOM, and not
the el.addEventListener() method of the DOM element. To remove the
event listeners, you use the el.removeEventListener() method.
Write the code for the patchEvents() function in listing 8.16. (Don’t forget
importing the addEventListener function at the top of the file.)
import {
removeAttribute,
setAttribute,
removeStyle,
setStyle,
} from './attributes'
import { destroyDOM } from './destroy-dom'
import { addEventListener } from './events'
import { DOM_TYPES } from './h'
import { mountDOM } from './mount-dom'
import { areNodesEqual } from './nodes-equal'
import {
arraysDiff,
} from './utils/arrays'
import { objectsDiff } from './utils/objects'
import { isNotBlankOrEmptyString } from './utils/strings'
// --snip-- //
function patchEvents(
el,
oldListeners = {},
oldEvents = {},
newEvents = {}
) {
const { removed, added, updated } = objectsDiff(oldEvents, newEvents)
const addedListeners = {} #3
return addedListeners #6
}
Both element and fragment nodes can have children, so the patchDOM()
function needs to patch the children arrays of both types of nodes. Patching
the children means figuring out which children have been added, which ones
have been removed, and which ones have been shuffled around. For this, you
implemented the arraysDiffSequence() in the previous chapter.
You can see in figure 8.14 where the patching of children is in the
algorithm’s flowchart. Remember that, in the case of move and noop
operations between children, the algorithm recursively calls the patchDOM()
function, passing it the old and new children nodes. The add and remove
operation cases terminate the algorithm.
Figure 8.14. Patching the children of a node
Start by modifying the patchDOM() function to call the patchChildren()
function after the switch statement. This will execute the patchChildren()
function for all types of elements except the text nodes, which return early
from the patchDOM() function.
Modify the patchDOM() function by adding the code in bold font in listing
8.17. This includes importing arraysDiffSequence and ARRAY_DIFF_OP from
the utils/arrays.js file, and calling the patchChildren() function.
import {
removeAttribute,
setAttribute,
removeStyle,
setStyle,
} from './attributes'
import { destroyDOM } from './destroy-dom'
import { addEventListener } from './events'
import { DOM_TYPES } from './h'
import { mountDOM } from './mount-dom'
import { areNodesEqual } from './nodes-equal'
import {
arraysDiff,
arraysDiffSequence,
ARRAY_DIFF_OP,
} from './utils/arrays'
import { objectsDiff } from './utils/objects'
import { isNotBlankOrEmptyString } from './utils/strings'
return newVdom
}
newVdom.el = oldVdom.el
switch (newVdom.type) {
case DOM_TYPES.TEXT: {
patchText(oldVdom, newVdom)
return newVdom
}
case DOM_TYPES.ELEMENT: {
patchElement(oldVdom, newVdom)
break
}
}
patchChildren(oldVdom, newVdom)
return newVdom
}
The patchChildren() function extracts the children arrays from the old and
new nodes (or use an empty array in their absence), and then calls the
arraysDiffSequence() function to find the operations that transform the old
array into the new one. (Let’s not forget that this function requires the
areNodesEqual() function to compare the nodes in the arrays.) Then, for
each operation, it performs the appropriate patching, as we’ll see in a minute.
You first need a function, which you can call extractChildren(), that
extracts the children array from a node, such that, if it encounters a fragment
node, it extracts the children of the fragment node and adds them to the array.
This function needs to be recursive, so that if a fragment node contains
another fragment node, it also extracts the children of the inner fragment
node.
Inside the h.js file, where the virtual node creation functions are defined,
write the code for the extractChildren() function in listing 8.18.
const children = []
for (const child of vdom.children) { #2
if (child.type === DOM_TYPES.FRAGMENT) {
children.push(...extractChildren(child, children)) #3
} else {
children.push(child) #4
}
}
return children
}
With this function ready, you can now write the code for the
patchChildren() function in listing 8.19. (Don’t forget importing the
extractChildren() function at the top of the file.)
import {
removeAttribute,
setAttribute,
removeStyle,
setStyle,
} from './attributes'
import { destroyDOM } from './destroy-dom'
import { addEventListener } from './events'
import { DOM_TYPES } from './h'
import { mountDOM, extractChildren } from './mount-dom'
import { areNodesEqual } from './nodes-equal'
import {
arraysDiff,
arraysDiffSequence,
ARRAY_DIFF_OP,
} from './utils/arrays'
import { objectsDiff } from './utils/objects'
import { isNotBlankOrEmptyString } from './utils/strings
// --snip-- //
switch (operation.op) { #5
case ARRAY_DIFF_OP.ADD: {
// TODO: implement
}
case ARRAY_DIFF_OP.REMOVE: {
// TODO: implement
}
case ARRAY_DIFF_OP.MOVE: {
// TODO: implement
}
case ARRAY_DIFF_OP.NOOP: {
// TODO: implement
}
}
}
}
All that’s left to do is fill in the switch statement with the code for each
operation (where the // TODO comments are). This will be simpler than you
think. Let’s start with the ARRAY_DIFF_OP.ADD operation.
Add operation
When a new node is added to the children array, it’s as if you were mounting
a subtree of the DOM at a specific place. Thus, you can simply use the
mountDOM() function to do this, passing it the index at which the new node
should be inserted.
You can see in figure 8.15 how when a node addition is detected (an
operation of type ARRAY_DIFF_OP.ADD), it’s inserted into the DOM.
switch (operation.op) {
case ARRAY_DIFF_OP.ADD: {
mountDOM(item, parentEl, index)
break
case ARRAY_DIFF_OP.REMOVE: {
// TODO: implement
}
case ARRAY_DIFF_OP.MOVE: {
// TODO: implement
}
case ARRAY_DIFF_OP.NOOP: {
// TODO: implement
}
}
}
}
Remove operation
When a node is removed from the children array, you want to unmount it
from the DOM. Thanks to the destroyDOM() function you wrote earlier in the
book, this is easy.
Figure 8.16 illustrates the case where a node is removed from the children
array (an operation of type ARRAY_DIFF_OP.REMOVE), and how it’s removed
from the DOM.
switch (operation.op) {
case ARRAY_DIFF_OP.ADD: {
mountDOM(item, parentEl, index)
break
}
case ARRAY_DIFF_OP.REMOVE: {
destroyDOM(item)
break
case ARRAY_DIFF_OP.MOVE: {
// TODO: implement
}
case ARRAY_DIFF_OP.NOOP: {
// TODO: implement
}
}
}
}
Move operation
When you detect that a node has moved its position in the children array, you
have to move it in the DOM as well. To do this, you need to grab the
reference to the DOM node, and use its insertBefore() method to move it
to the new position. The insertBefore() method requires a reference to a
DOM node that will be the next sibling of the node you’re moving; you need
to find the node that’s currently at the desired index.
Note
The browser automatically removes the node from its original position when
you move it; you won’t end up with the same node in two different places.
After moving the node, you want to pass it to the patchDOM() function to
patch it. These nodes stay in the DOM from one render to the next, so they
might have not only moved around, but also changed in other ways (for
example, a CSS class was added to them).
You can see the case of a node moving its position illustrated in figure 8.17.
Here, when a node moves inside its parent node’s children array (an
operation of type ARRAY_DIFF_OP.MOVE), the same movement is replicated in
the DOM.
switch (operation.op) {
case ARRAY_DIFF_OP.ADD: {
mountDOM(item, parentEl, index)
break
}
case ARRAY_DIFF_OP.REMOVE: {
destroyDOM(item)
break
}
case ARRAY_DIFF_OP.MOVE: {
const oldChild = oldChildren[originalIndex] #1
const newChild = newChildren[index] #2
const el = oldChild.el #3
const elAtTargetIndex = parentEl.childNodes[index] #4
parentEl.insertBefore(el, elAtTargetIndex) #5
patchDOM(oldChild, newChild, parentEl) #6
break
}
case ARRAY_DIFF_OP.NOOP: {
// TODO: implement
}
}
}
}
Noop operation
If you recall, some of the child nodes might have not moved, or moved due to
other nodes being added or removed around them (what I call natural
movements). You don’t need to explicitly move them, because they fall into
their new positions naturally. But, what you need to do is patch them,
because they might have changed in other ways, as noted before.
switch (operation.op) {
case ARRAY_DIFF_OP.ADD: {
mountDOM(item, parentEl, index)
break
}
case ARRAY_DIFF_OP.REMOVE: {
destroyDOM(item)
break
}
case ARRAY_DIFF_OP.MOVE: {
const el = oldChildren[from].el
const elAtTargetIndex = parentEl.childNodes[index]
parentEl.insertBefore(el, elAtTargetIndex)
patchDOM(oldChildren[from], newChildren[index], parentEl)
break
}
case ARRAY_DIFF_OP.NOOP: {
patchDOM(oldChildren[originalIndex], newChildren[index], parentEl)
break
}
}
}
}
That’s it: you’ve implemented the reconciliation algorithm. And most
remarkably, you’ve done it from scratch.
Let’s now publish the new and improved version of your framework to see it
in action in the TODOs application.
First, bump the version of the runtime package by incrementing the version
field in the package.json file to 2.0.0:
{
"version": "1.0.0",
"version": "2.0.0",
}
Then, run the npm publish command to publish the new version of the
package. That’s it! Your new and improved version of the framework is now
available on NPM and unpkg.com.
Now, the only line that you need to change is the first line in the index.js file,
where you import your framework. Change it to import the new version of
the framework from unpkg.com:
import { createApp, h, hFragment } from 'https://unpkg.com/<fwk-name>
import { createApp, h, hFragment } from 'https://unpkg.com/<fwk-name>
And with that simple change, your TODOs application is now using the new
version of your framework. You can serve the application by running the
following command at the root of the project:
$ npm run serve:examples
How can you make sure your algorithm isn’t doing extra work and it’s only
patching what changed?
For example, if you open the TODOs application from chapter 6 Publishing
and using your framework's first version—when the DOM was recreated
every time—and type a character in the input field, you’ll see that the entire
DOM tree is highlighted, as you can see in figure 8.18. (If you followed the
folder naming conventions explained in appendix A, your application from
chapter 6 Publishing and using your framework's first version should be
running at localhost:8080/examples/ch06/todos/todos.html.)
Figure 8.18. With your previous version of the framework, the entire DOM tree is highlighted
when you type a character in the input field.
If you open the TODOs application from this chapter, and type a character in
the input field… nothing is highlighted. That’s because the DOM tree didn’t
change at all (just the value property of the input field). But once you’ve
written three characters, the disabled attribute is removed from the Add
<button>, and you see this node flashing in the DOM tree (see figure 8.19).
Figure 8.19. With the new version of your framework, nothing is highlighted when you type a
character in the input field, until you’ve written three characters and the disabled attribute is
removed from the Add <button>.
Chrome has a neat feature that allows you to see the areas in the page that are
repainted. You can find it in the developer tools, under the Rendering tab,
and it’s called "Paint flashing". If you select it, you can see highlighted in
green rectangles the parts of the page that the browser repaints.
Figure 8.20. With your previous version of the framework, the entire page is repainted every time
you type a character in the input field.
But if you look at the TODOs application from this chapter, you can see that
only the input field’s text and the field’s label are repainted (figure 8.21).
Figure 8.21. With the new version of your framework, only the input field’s text and the field’s
label are repainted when you type a character in the input field.
The fact that the "New TODO" label is repainted is a bit surprising, but it
doesn’t mean that the framework patched something it shouldn’t have. As
you saw in the DOM tree, nothing flashes there as you write in the input
field. These "paint flashes" are a sign of the rendering engine doing its job of
figuring out the layout of the page and repainting the pixels that might have
moved or changed. Nothing you should worry about.
Experiment a bit with the developer tools to understand how the DOM is
patched by your framework. The exercises that follow will give you some
ideas of the experiments you can do with them.
Exercise 8.3
Use the "Elements" tab in Chrome (or the "Inspector" tab in Firefox) to
inspect the DOM modifications (shown as flashes in the nodes) that happen
when you add, modify or mark a to-do item as completed. Compare what
happens in both the TODOs application from chapter 6 and that from this
chapter.
Exercise 8.4
Use the "Sources" tab in Chrome (or the "Debugger" tab in Firefox) to debug
the patchDOM() function (you can set a breakpoint in the first line of the
function) as you:
For this exercise, first create an html file with the following content:
<html>
<head>
<title>Exercise 1</title>
</head>
<body>
<div>
<p>One</p>
<p>Three</p>
</div>
</body>
</html>
Then, in the browser’s console, you can use the following JavaScript code to
insert a new <p> element between the two existing ones:
const div = document.querySelector('div')
const three = div.querySelectorAll('p').item(1)
const two = document.createElement('p')
two.textContent = 'Two'
div.insertBefore(two, three)
Copy and paste the areNodesEqual() and the DOM_TYPES code into the
console. Don’t forget that you should leave out the export keyword when
you paste the code into the console. Then test the cases described in the
exercise.
Let’s start with the TODOs application from chapter 6. If you open the
Elements tab in Chrome (or the Inspector tab in Firefox) and inspect the
DOM modifications that happen when you add a new TODO item, you’ll see
that the entire page is repainted (figure 8.22).
Figure 8.22. With the previous version of your framework, the entire page is repainted when a
new TODO is added.
A similar thing happens when you mark a TODO item as completed (figure
8.23).
Figure 8.23. With the previous version of your framework, the entire page is repainted when a
TODO is marked as completed.
Now open the same application from this chapter and inspect the DOM
modifications that happen when you add a new TODO item. You’ll see that
only the added item is repainted (figure 8.24). Hooray! Your reconciliation
algorithm is working!
Figure 8.24. With the new version of your framework, only the added item is repainted when a
new TODO is added.
Try marking a TODO item as completed. In this case, only the <ul> item that
contained the completed item is repainted (figure 8.25).
Figure 8.25. With the new version of your framework, only the parent of the completed item is
repainted when a TODO is marked as completed.
8.5.4 Exercise 8.4
For this exercise, you first want to locate the framework file in the browser’s
debugger. If you’re using chrome, then you want to open the Sources tab and
locate the framework file there (figure 8.26).
Figure 8.29. The patchDOM() function is called when you type a letter in the input field
The first time the breakpoint is hit, the old and new virtual DOM trees are the
top-level fragment nodes: <h1>, <div> and <ul>. If you recall, the
patchDOM() function starts from the very top and moves down the tree,
comparing the old and new nodes. You have to click the "Resume execution"
button a couple of times until the two compared virtual nodes are the <input>
where you wrote a letter (figure 8.30).
Remove or disable the break point so that you can write a new TODO
without interruptions. Before hitting the Add button, set the breakpoint again.
Click the add button and then click the "Resume execution" button a couple
of times until the two compared virtual nodes are the <ul> node and the <li>
node you just added (figure 8.31).
Figure 8.31. Comparing the old and new <ul> and <li> nodes
In this case, the old children of the <ul> node are the three <li> nodes that
were already there, and the new children are the four <li> nodes, including
the one you just added. I’ll leave it as a experiment for you to step over the
lines of code and see how the patchChildren() function does its job. Take
some time to examine how your code is modifying the DOM; this will help
you understand how the reconciliation algorithm works.
8.6 Summary
The reconciliation algorithm compares two virtual DOM trees, finds the
sequence of operations that transform one into the other, and patches the
real DOM by applying those operations to it. The algorithm is recursive,
and it starts at the top-level nodes of both virtual DOM trees. After
comparing these nodes, it moves on to their children, and so on, until it
reaches the leaves of the trees.
To know if a DOM node can be reused, you need to compare the
corresponding virtual DOM nodes. If the virtual nodes are of a different
type, the DOM node can’t be reused. If the virtual nodes are either text
or fragment nodes, they can be reused. Element nodes can be reused if
they have the same tag name.
When the nodes being compared aren’t equal—that is, they can’t be
reused—the DOM is completely destroyed and recreated from scratch.
This makes sense in most of the cases, as if the parent node of a subtree
changes, it’s likely that the children are different as well.
Text nodes are patched by setting the DOM node’s nodeValue property
to the new node’s text.
Element nodes are patched by separately patching their attributes, CSS
classes and styles, and event listeners. You use the objectsDiff() and
arraysDiff() functions to find the differences between the old and new
values of these properties, and apply the changes to the DOM.
Both fragment and element nodes need their children patched.
To patch the children of a node, you use the arraysDiffSequence() to
find the sequence of operations that transform one array of children into
the other, and apply those operations to the DOM.
Appendix. Setting up the project
Before you write any code, you need to have an NPM project set up. In this
appendix, I’ll help you create and configure a project to write the code for
your framework.
I understand you might not typically configure an NPM project from scratch
with a bundler, a linter, and a test library yourself, as most frameworks come
with a CLI tool (like, for example create-react-app or angular-cli) which
does the scaffolding and generating the boilerplate code structure for you. So
I’ll give you two options to create your project:
1. Use the CLI tool I created for this book. With a single command, you
can create a project where you can start writing your code right away.
2. Configure the project yourself, from scratch. It’s more laborious, but
you get to learn how to configure the project.
If you want to get started quickly with the book and can’t wait to write your
awesome frontend framework, I recommend you use the CLI tool. In this
case, you need to read section A.6. But if you are the kind of developer who
enjoys configuring everything from scratch, you can follow the steps to
configure the project yourself in section A.7.
Before you start configuring your project, let’s first cover some basics about
where you can find the code for the book and the technologies you’ll use.
These sections are optional, but I recommend you to read them before you
start configuring your project. If you can’t wait to start writing code, you can
skip them and move on to sections A.6 and A.7. You can always come back
and read the rest of the appendix later.
https://github.com/angelsolaorbaiceta/fe-fwk-book
I recommend you to clone the repository or download the ZIP archive of the
repository, to follow along with the book. It contains all the listings that
appear the book (in the listings folder), sorted by chapter. I’m assuming
you’re familiar with git and know how to clone a repository and checkout
tags or branches. If previous sentence sounded like nonsense to you, don’t
worry, you can still follow along with the book. Download the project as a
ZIP archive by clicking the <> Code button in the repository and then
clicking on the Download ZIP button.
Warning
The code you’ll find in the repository is the final version of the framework,
the one you’ll have written by the end of the book. If you want to check the
code for each chapter, you need to checkout the corresponding Git tag as
explained below. You can also refer to the listings/ directory in the
repository, which contains all the listings from the book, sorted by chapter.
Let’s take a look at how you can find the code for each chapter.
By checking out the code for each chapter, you can see how the framework
evolves as we add new features. You can also compare your code by the end
of each chapter with the code in the repository. Note that not all chapters
have a tag in the repository, only those that have a working version of the
framework. I also wrote unit tests for the framework that I won’t cover in the
book, but you can look at them to find ideas on how to test your code.
Note
If you’re not familiar with the concept of git tags, you can learn about them
in https://git-scm.com/book/en/v2/Git-Basics-Tagging. Basic git knowledge
is assumed for this book.
I recommend you to avoid copy/pasting the code from the book and instead
type it yourself. If you get stuck because your code doesn’t seem to work,
look at the code in the repository, try to figure out the differences, and then
fix it yourself. If you write a unit test to reproduce the problem, much better.
I know this is more cumbersome, but it’s the best way to learn, and it’s
actually how you’d probably want to tackle a real-world bug in your code.
You can also refer to the listings/ directory in the repository, which contains
all the listings from the book, sorted by chapter.
You can find instructions on how to run and work with the code in the
README file of the repository. This file contains up-to-date documentation
on everything you need to know about the code. Make sure to quickly read it
before you start working with the code. (When you get started with the code
of an open-source repository, reading the README file is the first thing you
should do.)
So, just bear in mind that the code you’ll find in the repository and the book
has the purpose of teaching efficiently, not being production-ready.
As many tests as I wrote and as many times as I reviewed the code, I’m sure
there are still bugs in it. Frontend frameworks are complex beasts, and it’s
hard to get everything right. If you find a bug in the code, please report it in
the repository by opening a new issue.
https://github.com/angelsolaorbaiceta/fe-fwk-book/issues
Then, click on the New issue button. In the row that reads Bug report, click
on the Get started button. Fill in the form with the details of the bug you
found, and click on the Submit new issue button.
You’ll need to give enough information for me to easily reproduce and fix the
bug. I know it takes time to file a bug report with these many details, but it’s
considered good etiquette in the open source community. It shows respect
from your side towards the maintainers of a project, who use their free time
to create and maintain it for everyone to use free of charge. It’s good that we
get used to being respectful citizens of the open source community.
If you don’t know what GitHub pull requests are, I recommend you to read
about them at https://docs.github.com/en/pull-requests. Pull requests are the
way to contribute to open source projects on GitHub (and also how many
software companies add changes to their codebase), so it’s good to know how
they work.
Moving on. Before you create your project, let’s take a look at the
technologies you’ll use and how the project will be structured.
I once worked for a start-up that grew quickly but wasn’t doing great. We had
very few customers using our app—if any at all—and every time we added
something in the code, we’d introduce new bugs. (Code quality wasn’t a
priority; speed was, and automated testing was nowhere to be found.)
Surprisingly, we blamed it on the tools we were using. We were convinced
that, when we migrated the code to the newest version of the framework we
were using, we would get more customers and things would work great from
then onwards. Yes, I know how ridiculous this sounds now. But we made it
to "modernize" the tooling, and guess what? the code was still the same: a
hard-to-maintain mess that would break if you stared at it for too long. Turns
out that using more modern tools doesn’t make your code better if the code is
poorly written in the first place.
What do I want to say with this? That I believe your time is better used
writing quality code than arguing about what tools will make your
application successful. Well architected and modularized code with a
great suite of automated tests that, apart from making sure the code works
and can be refactored safely, also serves as documentation, beats any
tooling. That is not to say the tools don’t matter, because they obviously do.
Ask a carpenter if they can be productive using a rusty hammer or a blunt
saw.
For this book, I’ve tried to choose tools that are mature and that most
frontend developers might be familiar with. Choosing the newest, shiniest or
trendiest tool wasn’t my goal.
I want to teach you how frontend frameworks work, and the truth is, that
most tools work perfectly fine for this purpose. The knowledge that I’ll teach
you in this book transcends the tools we choose to use. So, having said this, if
you have a preference for a tool and some experience with it, feel free to use
it instead of the ones I’ll recommend here.
We’ll use NPM workspaces, which were introduced in version 7, so you want
to make sure you have at least version 7 installed:
$ npm --version
8.19.2
Both yarn and pnpm also support workspaces, so you can use them as well.
(In fact, they introduced workspaces before NPM did.)
Towards the end of the book, when you use your framework to build
applications, you’ll also use Webpack. In fact, you’ll write a Webpack loader
that’ll transform the templates for your components into render functions.
A.2.3 Linter—ESLint
To lint the code, we’ll use ESLint. It’s a very popular linter, and it’s very easy
to configure. I’m a firm believer that static analysis tools are a must for any
serious project where the quality of the code is important (as it always is the
case).
ESLint will prevent us from having unused variables declared, imports that
are not used, and many other things that can go wrong in our code. ESLint is
super configurable, but most developers are happy with the default
configuration: it’s a good starting point. We’ll use the default configuration
for this book as well, but you can always change it to your liking. If there’s a
linting rule you deem important, you can add it to your configuration.
So why am I using JavaScript for this book? Because the code tends to be
shorter without types, and what I want to do here is teach you the principles
of how frontend frameworks work, principles that apply equally well to both
JavaScript and TypeScript. I prefer to use JavaScript because the code listings
will be shorter and I can get to the point faster, and thus teach you more
efficiently.
As with the previous tools, if you feel strongly about using TypeScript, you
can use it instead of JavaScript. You’ll need to figure out the types yourself,
but that’s part of the fun, right? Also, don’t forget to setup the compilation
step in your build process.
One of the most important lessons I’ve learned over the years is that taking
the time to read the documentation is a great investment of your time.
Reading the docs before you start using a new tool or library saves you the
time you’d spend trying to figure out how to do something that, somewhere
in the documentation, someone has taken the time and care to explain in
detail. The time that you end up spending trying to figure things out on your
own, and searching StackOverflow for answers, is—in my personal
experience—usually greater than the time you’d have spent should you read
the documentation upfront.
Note
If you’re not familiar with the concept of NPM workspaces, you can read
more about at https://docs.npmjs.com/cli/v9/using-npm/workspaces. It’s
important that you understand how they work, so the structure of the project
makes sense to you and you can follow along the instructions that follow.
Each package has its own package.json file, with its dependencies, and it’s
bundled separately. It’s effectively three separate projects, but they are all
part of the same repository. This project structure is very common these days,
and it’s called a monorepo.
build—builds the package, bundling all the JavaScript files into a single
file, which is published to NPM.
test—runs the automated tests in watch mode.
test:run—runs the automated tests once.
lint—runs the linter to detect issues in your code.
lint:fix—runs the linter and automatically fixes the issues it can.
prepack—a special life cycle script that runs before the package is
published to NPM. It’s used to make sure the package is built before
being published.
Caution
If the URL displays the 404—not found page, that means nobody is using that
name for a Node JS package yet, so you’re good to go; you can use that
name.
Important
Let’s now create the project. Remember that you have two options. If you
want to configure things yourself, you want to skip the next section and jump
to section A.7. If you want to use the CLI tool to get started quickly, read the
next section.
A.6 Option A—Using the CLI tool
Using the CLI tool I created for the book is the fastest way to get started. It’ll
save you the time it takes to create and configure the project from scratch, so
you can start writing code right away. You just need to open your terminal,
move to the directory where you want the project to be created, and run the
following command:
$ npx fe-fwk-cli init <fwk-name>
With npx you don’t even need to install the CLI tool locally, it will be
downloaded and executed automatically.
That’s it! You can now jump to the A.8 section to learn how to publish your
framework to NPM.
Note
If you created your project using the CLI tool, you can skip this section.
To create the project yourself, you first want to create a directory for it. Using
the command line, you can do that by running the following command:
$ mkdir <fwk-name>
This command creates a package.json file in the directory. There are a few
edits you need to make to the file. You want to edit the description field to
something like:
"description": "A project to learn how to write a frontend framework"
Then, you want to make this package private, so you don’t accidentally
publish it to NPM. It’s the workspace packages you’ll create in a minute that
you’ll publish to NPM, not the parent project itself:
"private": true
Finally, you want to add a workspaces field to the file, with an array of the
directories where you’ll be creating the three packages your project will
consist of:
"workspaces": [
"packages/runtime",
"packages/compiler",
"packages/loader"
]
You might have other fields, such as author, license, etc., but the ones in
the previous snippet are the ones you need to make sure are there. Let’s now
create a directory in your project where you can add example applications to
test your framework.
Tip
While the examples folder remains empty, that is, before you write any
example application, you might want to add a .gitkeep file to it, so that git
picks up the directory and includes it in the repository. (Git doesn’t track
empty directories.) As soon as you put a file in the directory, you can remove
the .gitkeep file, but keeping it doesn’t hurt anyway.
To keep the examples directory tidy, you want to create a subdirectory for
each chapter: examples/ch02, examples/ch03, etc. Each subdirectory will
contain the example applications using the framework resulting from the
chapter, which allows you to see the progress in how the framework becomes
more and more powerful, and easier to use. You don’t need to create the
subdirectories now, you’ll create them as you need them in the book.
Now you need a script to serve the example applications. Your applications
will consist of an entry HTML file, which loads other files, such as
JavaScript files, CSS files, and images. For the browser to be able to load
these files, you need to serve them from a web server. The http-server
package is a very simple web server that you can use to serve the example
applications.
Important
This is the only script that you need to add to the project root package.json
file. All the other scripts you’ll add to the project will be added to the
package.json files of the packages you’ll create in the next sections. Bear this
in mind. You won’t add any other script to the root package.json file.
Great—Let’s now create the three packages that will make up your
framework code.
Before you create the packages, you need to create a directory where you’ll
put them, the packages directory you specified in the workspaces field of the
package.json file.
Make sure your terminal is inside the project’s root directory by running the
pwd command:
$ pwd
If your terminal is not in the project’s root directory, use the cd command to
navigate to it. Then, create a packages directory:
$ mkdir packages
Then create the folder for the runtime package and cd into it:
$ mkdir runtime
$ cd runtime
JavaScript resolves the path to the file specified in the main field. You’ve told
NPM that the file is the dist/<fwk-name>.js file, which is the bundled file
containing all the code for the runtime package. This file will be generated by
Rollup, by running the build script you’ll add to the package.json file in the
next section.
The files field specifies the files that will be included in the package when
you publish it to the NPM repository. The only file that you want to include
is the bundled file, so you’ve specified that in the files field. Files like the
README, the LICENSE, and the package.json file are automatically
included in the package, so you don’t need to include them here.
Your project should have the following structure (in bold font is what you’ve
just created):
<fwk-name>/
├── package.json
├── examples/
│ └── .gitkeep
└── packages/
└── runtime/
└── package.json
Rollup is the bundler that you’ll use to bundle your framework code. It’s very
simple to configure and use.
Let’s install Rollup. Make sure your terminal is inside the runtime package
directory by running the pwd command:
$ pwd
Now, you need to configure Rollup. Create a rollup.config.mjs file (note the
.mjs extension, to tell Node JS this file should be treated as an ES module
and thus can use the import syntax) in the runtime folder and add the
following code:
import cleanup from 'rollup-plugin-cleanup'
import filesize from 'rollup-plugin-filesize'
export default {
input: 'src/index.js', #1
plugins: [cleanup()], #2
output: [
{
file: 'dist/<fwk-name>.js', #3
format: 'esm', #4
plugins: [filesize()], #5
},
],
}
The entry point of the framework code is the src/index.js file: starting
from this file, Rollup will bundle all the code that is imported from it.
The comments in the source code should be removed from the generated
bundle (they occupy space and are not needed for the code to execute).
This is what the plugin-rollup-cleanup plugin does.
The generated bundle should be an ES module (using import/export
syntax, supported by all major browsers) that is saved in the dist folder,
as <fwk-name>.js.
We want the size of the generated bundle to be displayed in the terminal
to keep an eye on it and make sure it doesn’t grow too much. This is
what the plugin-rollup-filesize plugin does.
Now add a script to the package.json file (the one inside the runtime
package) to run Rollup and bundle all the code, and another one to run it
automatically before publishing the package:
"scripts": {
"prepack": "npm run build",
"build": "rollup -c"
}
The prepack script is a special script that is run automatically by NPM before
publishing the package, which you do by running the npm publish command
(more on this later). This makes sure that, whenever you publish a new
version of the package, the bundle is generated. The prepack script simply
runs the build script, which in turn runs Rollup.
To run Rollup, you simply call the rollup command and pass the -c flag to
tell it to use the rollup.config.mjs file as configuration. (If you don’t pass a
specific file to the -c flag, Rollup will look for a rollup.config.js or
rollup.config.mjs file.)
Before you test the build command, you need to create the src folder and the
src/index.js file. You can do that with the following commands:
$ mkdir src
$ touch src/index.js
(In windows shell touch will not work use call > filename instead.)
You can now run the build command to bundle the code:
$ npm run build
This command processes all the files imported from the src/index.js file
(none at the moment) and bundles them into the dist folder. The output in
your terminal should look similar to the following:
src/index.js → dist/<fwk-name>.js...
┌─────────────────────────────────────┐
│ │
│ Destination: dist/<fwk-name>.js │
│ Bundle Size: 56 B │
│ Minified Size: 55 B │
│ Gzipped Size: 73 B │
│ │
└─────────────────────────────────────┘
created dist/<fwk-name>.js in 62ms
Recall that, instead of <fwk-name>, you should use the name of your
framework. That rectangle in the middle of the output is the rollup-plugin-
filesize plugin in action. It’s reporting the size of the generated bundle (56
bytes in this case), and also the size it’d have if the file was minified and
gzipped. We won’t be minifying the code for this book, as the framework is
going to be small anyway, but for a production-ready framework, you should
definitely do it. A minified JavaScript can be loaded faster by the browser,
and thus improve the TTI (Time to Interactive) of the web application using
it.
A new file, dist/<fwk-name>.js, has been created in the dist folder. If you
open it, you’ll see it just contains the console.log() statement you added to
the src/index.js file.
ESLint is a linter that will help you write better code. It analyzes the code you
write and reports any errors or potential problems.
And lastly, add the following two scripts to the package.json file:
"scripts": {
"prepack": "npm run build",
"build": "rollup -c",
"lint": "eslint src",
"lint:fix": "eslint src --fix"
You can run the lint script to check the code for errors, and the lint:fix
script to automatically fix some of them, those that ESLint knows the fix for.
I won’t be showing the output of the lint script in the book, but you can run
it yourself to see what it reports as you write your code.
As I’ve mentioned, I used a lot of tests while developing the code for this
book, but I won’t be showing them in the book. You can take a look at them
in the source code of the framework and try to write your own as you follow
along. Having tests in your code will help you make sure things work as
expected as you move forward with the development of your framework.
We’ll use Vitest to run the tests. To install Vitest, run the following
command:
$ npm install --save-dev vitest
Tests can run in different environments, such as Node JS, JSDOM, or a real
browser. Because we will use the Document API to create DOM elements,
we’ll use JSDOM as the environment. (If you want to know more about
JSDOM, please refer to their repository: https://github.com/jsdom/jsdom.)
With Vitest and JSDOM installed, you can now create the vitest.config.js file
with the following content:
import { defineConfig } from 'vitest/config'
This configuration tells Vitest to use the JSDOM environment and to use the
verbose reporter, which outputs a very descriptive report of the tests.
You should place your test files inside the src/__tests__ folder, and name
them *.test.js for Vitest to find them. Go ahead and create the src/__tests__
folder:
$ mkdir src/__tests__
Inside, you can create a sample.test.js file with the following content:
import { expect, test } from 'vitest'
The test script runs the tests in watch mode, and the test:run script runs
the tests once and exits. Tests in watch mode are handy when you’re working
with TDD (Test Driven Development), as they run again every time you
make changes in the test file or the code being tested.
Give a try to the tests by running the following command:
$ npm run test:run
You can run individual tests passing the name or path of the test file as an
argument to the test:run and test scripts. All the tests matching the name
or path will be run. For example, to run the sample.test.js test, you can run
the following command:
$ npm run test:run sample
This will run only the sample.test.js test and produce the following output:
✓ src/__tests__/sample.test.js (1)
✓ sample test
Your project structure should look like the following at this point (in bold
font are the files and folders that you’ve created):
<fwk-name>/
├── package.json
├── examples/
│ └── .gitkeep
└── packages/
└── runtime/
├── package.json
├── rollup.config.mjs
├── .eslintrc.js
├── vitest.config.js
└── src/
├── index.js
└── __tests__/
└── sample.test.js
Before creating the compiler package, you want to cd back to the packages
folder:
$ cd ..
$ cd ..
$ cp -r runtime compiler
$ pwd
The compiler package will use the exact same structure and configuration as
the runtime package. So, you can copy the runtime folder and rename it to
compiler using the following command:
$ cp -r runtime compiler
This copies everything, including the node_modules folder. The only thing
that you need to change is the package.json's name field, like so:
"name": "<fwk-name>-compiler",
That is, the name of your framework followed by the -compiler suffix. That’s
it.
Your project’s structure should look like the following (the compiler package
is in bold font, and the contents of the packages are elided for brevity):
<fwk-name>/
├── package.json
├── examples/
│ └── .gitkeep
└── packages/
├── runtime/
└── compiler/
You can also copy the runtime folder and rename it to loader using the
following command (recall that your current working directory should be the
packages folder):
$ cp -r runtime loader
And make sure that you change the package.json's name field to:
"name": "<fwk-name>-loader",
For reference, your project’s structure should look like the following (once
again, the contents of the packages are elided for brevity):
<fwk-name>/
├── package.json
├── examples/
│ └── .gitkeep
└── packages/
├── runtime/
├── compiler/
└── loader/
One last thing you want to do for the package.lock file to be correctly
generated, including the dependencies of the compiler and loader packages,
is to cd into the root folder of the project (where the root package.json file
is):
$ cd ..
But before you can do that, you need to create an account on NPM and log in
to it from the command line. Let’s do that now. If you don’t plan to publish
your framework, you can skip this section.
and follow the prompts. To make sure you’re logged in, run the following
command:
$ npm whoami
Warning
Make sure you’re not in the root folder of the project. The root folder’s
package.json file has the private field set to true, which means that it’s not
meant to be published to NPM. It’s the runtime package that you want to
publish to NPM.
Remember that the runtime package is the code for the framework (what you
want to publish), and the root of the project is just the monorepo containing
the runtime, compiler, and loader packages (not to be published to NPM).
With your terminal in the runtime directory, run the following command:
$ npm publish
You might be wondering what gets published to NPM. There are some files
in your project that always get published, and these are the package.json file,
the README.md file, and the LICENSE file. Apart from these, you can
specify which files to publish in the files field of the package.json file. If you
recall from a previous section, the files field in the package.json file of the
runtime package looks like this:
"files": [
"dist/<fwk-name>.js"
],
So, you’re only publishing the dist/<fwk-name>.js file, the bundled version
of your framework. The source files inside the src/ folder are not published.
The package is published with the version specified in the package.json file’s
version field. Bear in mind that you can’t publish a package with the same
version twice. Throughout the book, we’ll increment the version number
every time we publish a new version of the framework.
This is great if you plan to set up an NPM project with a bundler (such as
Rollup or Webpack) configured to bundle your application and the
framework together. This is what you typically do when you’re building a
production application with a frontend framework. But for small projects or
quick experiments that use only a handful of files—as those you’ll work on in
this book—it’s simpler to import the framework directly from the dist/
directory in your project, or from a Content Delivery Network (CDN).
One free CDN that you can use is unpkg.com. Everything that’s published to
NPM is also available on unpkg.com, so once you’ve published your
framework to NPM, you can import it from there, like so:
import { createApp, h } from 'https://unpkg.com/<fwk-name>'
Or, if you want to use the last minor version of the framework, you can just
request a major version, like so:
import { createApp, h } from 'https://unpkg.com/<fwk-name>@1'
I recommend you to use unpkg using the versioned URL, so that you don’t
have to worry about breaking changes in the framework that might break
your examples as you publish new versions of it. If you import the
framework directly from the dist/ folder in your project, every time you
bundle a new version that’s not backwards compatible, your example
applications will break. You could overcome this by instructing Rollup to
include the framework version in the bundled file name, but I’ll be using
unpkg in this book.