This is part 1 of Learning UniRx, a Unity tutorial series designed to teach you how to use Reactive Extensions for Unity.
If you’re new to UniRx, check out Getting Started with UniRx.
Learning UniRx By Understanding Reactive Programming
If you want to learn how to use UniRx, you must first understand reactive programming. Let’s take a look at a couple of definitions.
The first is courtesy of Wikipedia. “In computing, reactive programming is an asynchronous programming paradigm concerned with data streams and the propagation of change.”
Or if you’ve read one of the most popular tutorials on the subject, you may be familiar with the following definition. “Reactive programming is programming with asynchronous data streams.”
But what does that really mean?
What is a Data Stream?
Think of a standard collection. We’ll use an array of Fibonacci numbers as an example.
This collection is immutable, which means it can’t be changed.
If we wanted to extract the positive values from this array, we could use LINQ’s Where operator to do it. The result would be a completely new, immutable list.
We could then create transform values using LINQ’s Select operator, which would again return a completely new, immutable list. For instance, we could multiply each value by 2.
This is all pretty standard stuff, and should look familiar if you’ve ever used LINQ or dealt with collections. Well, this same concept applies to data streams.
Data streams are nothing more than collections of values, except for one important difference; Time.
Time Keeps On Slippin’, Slippin’, Slippin’
The values of a data stream are emitted asynchronously over time. You don’t know what or even when in time the values of your data streams will be when your program starts.
Nonetheless, they’re collections. They’re first-class citizens that can be filtered, transformed, and merged however we like.
So reactive programming is programming with objects that represent collections of asynchronous events.
This is an insanely powerful paradigm that leads to a more declarative codebase.