Execution Context: Stream vs Function
Sometimes when you try to implement a simple ajax method, you might find yourself in a situation. You don't know how to register the ajax call correctly or whether to register it locally or globally:
In this chapter, We will show you about the technique and how to use it efficently.
The different between Stream and Function
In RxJS, we use stream so frequently that we always try to convert a function into a stream. But the main different between a Stream and a Function we may not really know. To illustrate the differences, we have a small example:
So as we see, there are two main differences between a stream and a function:
The sources and the targets of a stream are usually explicit. In constrast, the inputs and the outputs of a function are arbitrary.
A function is lazy, it only execute correctly if all its input informations have been filled in. Meanwhile, stream is reactive, only one of its sources need to be changed to trigger the stream change.
It means that when a stream has already been declared, it's unlikely to change its input and output sources. But a function is different, it will be called once at a time on different inputs and different outputs.
Another explanation is: The technique of RxJS is to execute a set of functions repeatly on different inputs and outputs. To do this, we need to lock the input and the output sources of a stream.
In summary, function is lazy and stream is reactive. Depend on what we need that we decide to use stream or function.
The transition between Function and Stream
A stream is not totaly reactive. I has to be lazy at some point. For example a subject only emit a value when someone call next
. To illustrate here is the diagram:
Convert a Function to RxJS style
There're two thing we have to notice about a function. One is its declaration. The second is how to call it.
It's the same method works with stream. For a stream, We will also need a stream declaration and a way to trigger that stream runs.
Nested Function Call / Nested Stream Triggering / Nest Stream Function Call
One of the most powerful feature is higher-order-function. With higher-order-function, we can perform much more higher level of logic.
In RxJS, we can do the same thing with Subject:
We can also have mix the benefit of both function and stream together:
Examples
The first example, we will recap the way we make an ajax call. Using function only, here is what we got:
Translate it to RxJS, then we got:
The second example is about process queue. Imagine here is how our process queue works:
The third example is quite tricky. The third example is about how to lock some background tasks when some modal is being shown:
Here is how we define it in RxJS:
Last updated
Was this helpful?