How to build a reactive engine in JavaScript. Part 1

How to build a reactive engine in JavaScript. Part 1

With the growing need for robust and interactive web interfaces, many developers have started embracing the reactive programming paradigm. Learn how to build a reactive engine with JavaScript.

With the growing need for robust and interactive web interfaces, many developers have started embracing the reactive programming paradigm.

Before we begin implementing our own reactive engine, let’s quickly explain what reactive programming actually is. Wikipedia gives us a classic example of a reactive interface implementation – namely a spreadsheet. Defining a formula such as =A1+B1 would update the cell whenever either A1 or B1 change. Such a formula can be considered a computed value.

You will learn how to implement computed values in the second part of this reactive series. Before that, we first need a base for our reactivity engine.

The engine

Currently there are many different approaches to solving the problem of observing and reacting to the changing application state.

  • Angular 1.x has its dirty checking.
  • React, because of the way it works – doesn’t actually track changes in the data model. It uses the virtual DOM to diff and patch the DOM.
  • Cycle.js and Angular 2 prefer the reactive streams implementations like XStream and Rx.js.
  • Libraries like Vue.js, MobX or Ractive.js all use a variation of getters/setters to create observable data models.

In this tutorial, we will go the getters/setters way of observing and reacting to changes.

Note: To keep the tutorial as simple as possible, the code lacks the support for non-primitive data types or nested properties and many of the required sanity checks, thus by no means should this code be considered production ready. The code below is written using the ES2015 standard and is loosely inspired by the Vue.js reactive engine implementation.

The observable object

Let’s start with a data object, whose properties we want to observe.

let data = {
  firstName: 'Jon',
  lastName: 'Snow',
  age: 25
}

Let’s start by creating two functions that will transform our object’s properties into observable properties using the getter/setter functionality.

function makeReactive (obj, key) {
  let val = obj[key]

  Object.defineProperty(obj, key, {
    get () {
      return val // Simply return the cached value
    },
    set (newVal) {
      val = newVal // Save the newVal
      notify(key) // Ignore for now
    }
  })
}

// Iterate through our object keys
function observeData (obj) {
  for (let key in obj) {
    if (obj.hasOwnProperty(key)) {
      makeReactive(obj, key)
    }
  }
}

observeData(data)

By running observeData(data) we transform our object into an object capable of being observed; now we have a way to create notifications whenever the value changes.

javascript react development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How native is React Native? | React Native vs Native App Development

Article covers: How native is react native?, React Native vs (Ionic, Cordova), Similarities and difference between React Native and Native App Development.

Hire Dedicated React Native Developer

Have you ever thought of having your own app that runs smoothly over multiple platforms? React Native is an open-source cross-platform mobile application framework which is a great option to create mobile apps for both Android and iOS. **[Hire...

Hire Dedicated React Native Developer in India | React Native Development

Hire dedicated React Native developers for your next project. As the top react native development company we offer you the best service as per your business needs.

Hire React Js Developer from Expert React JS Development Company

Are you looking to [hire React JS developer](https://www.mobiwebtech.com/react-js-development-company/ "hire React JS developer") from a prestigious and reliable React JS development company? Visit at Mobiweb Technologies here we have a big team...

A Short Guide to React Native App Development

React Native is undoubtedly one of the most widely used cross-platform frameworks for creating native-like apps. This framework can be easily used for developing brand-new apps from scratch and even in existing iOS or Android projects.easily used for developing brand-new apps from scratch and even in existing iOS or Android projects.