concept datum in category rxjs

appears as: data, data, The data
RxJS in Action

This is an excerpt from Manning's book RxJS in Action.

Right now, somewhere in the world, someone just created a tweet, a stock price just dropped, and, most certainly, a mouse just moved. These tiny pinpricks of data light up the internet and pass ubiquitously through semiconductors scattered across the planet. A deluge of data propagates from any connected device. What does this have to do with you? As you push your code to production, this fire hose of events is pointed squarely at your JavaScript application, which needs to be prepared to handle it effectively. This creates two important challenges: scalability and latency.

Figure 2.3. Function doAsyncWork() is an example of a remote call that fetches data from the server. Suppose this call has a latency around one second, depending on network conditions. Immediately after, the next function runs doMoreWork(), expecting that a piece of shared data has already been initialized. Because of this latency, the shared data has not been initialized, and the execution of doMoreWork() is compromised.

In this scenario, doAsyncWork() fetches some data from the server, which never completes in a constant amount of time. So doMoreWork() fails to run properly because it reads data that hasn’t yet been initialized. Callbacks and Promises help you solve this problem, so that you don’t have to hardcode your own timeouts in order to anticipate latency. Dealing directly with time is a recipe for disaster because your code will be extremely brittle and hard to maintain and will cause you to come in to work during a weekend when your application is experiencing slightly more traffic than usual. Working with data immutably, using FP, and the help of an asynchronous library like RxJS can make these timing issues disappear—immutable variables are protected against time. In chapters 4 and 6, we’ll cover timing and synchronization with observables, which offer a much superior solution to this problem.

You can clearly see how Rx was inspired by FP. All we had to do was wrap the array into a stream and then subscribe to it to listen for the computed values that derive from the sequence of steps declared in the stream’s pipeline. This is the same as saying that streams are containers that you can use to lift data (events) into their context, so that you can apply sequences of operations on this data until reaching your desired outcome. Fortunately, you’re already familiar with this concept from working with arrays for many years. You can lift a value into an array and map any functions to it. Suppose you declare some simple functions on strings like toUpper, slice, and repeat:

['rxjs'].map(toUpper).map(slice(0, 2)).map(repeat(2)); //-> 'RXRX'

The ancient Greek philosopher Heraclitus once said, “You can never step into the same river twice.” He formulated this statement as part of his doctrine on change and motion being central components of the universe—everything is constantly in motion. This epic realization is what RxJS streams are all about: as data continuously flows and moves through the stream, orchestrated through this is the data type you’re learning about called Stream. Despite being dynamic, Streams are immutable data types. Once a Stream is declared to wrap an array, listen for mouse clicks, or respond to an HTTP call, you can’t mutate it or add a new value to it afterward—you must do it at the time of declaration. Hence, you’re specifying the dynamic behavior of an object or value declaratively and immutably. We’ll revisit this topic a bit more in the next chapter.

Data, as in the data that you care about and that which gives rise to search engines, websites, and video games, is the flower component of software design. Creating software should therefore be about how you manipulate data rather than how you create approximations of real-world objects (as you might in OO programming). Bringing data to the forefront and separating it from the behavior of the system is at the heart of data-driven/data-centric design. Similarly, loosely coupling functions from the objects that contain data is a design principle of FP and, by extension, RP.

To be driven by data is to be compelled to act by the presence of it and to let it fuel your logic. Without data to act on, behavior should do nothing. The idea of data giving life to behavior ties back to our earlier definition of what it means to be reactive—reacting to data instead of waiting for it. Streams are nothing more than a passive process that sits idle when nothing is pushed through them and no consumer is attached, as shown in figure 2.8.

Figure 2.8. Initially, streams are lazy programs that wait for a subscriber to become available. Any events received at this point are discarded. Subscribing to the stream puts the wheels in motion, and event data flows through the pipeline and out for consumers to use.

This design pattern seems intuitive to most people because we think of data as requiring some sort of behavior in order to be meaningful. In a physics simulation, the mass of a ball is just a decimal number without context until the behavior of gravity is applied to it. Thus, if we are to imagine that both are intertwined by nature, it seems only natural that they should cohabitate logically within an object. In theory, this would seem to be a fairly obvious approach, and indeed the prevalence and popularity of OO programming stands testament to its power as a programming paradigm.

2.3.1. Identifying different sources of data

We mentioned earlier that the advantage of separating data and behavior is that you can reason about a holistic model to account for any type of data. Hence, the first step to break the data free is to understand that all of these data sources are the same when viewed through a data-driven (or stream-driven?) lens. First, let’s re-categorize the types of data we’ll encounter. Rather than dealing with them as strict JavaScript types, let’s look at some broader categories of data.