- Mastering Reactive JavaScript
- Erich de Souza Oliveira
- 534字
- 2021-07-09 20:33:11
Creating an observable from iterable objects
We can create an observable from iterable objects using the from() method. An iterable in JavaScript can be an array (or an array-like object) or other iterates added in ES6 (such as Set() and map()). The from() method has the following signature:
Rx.Observable.from(iterable,[mapFunction],[context],[scheduler]);
Usually, you will pass only the first argument. Others arguments are optional; you can see them here:
- iterable: This is the iterable object to be converted into an observable (can be an array, set, map, and so on)
- mapFunction: This is a function to be called for every element in the array to map it to a different value
- context: This object is to be used when mapFunction is provided
- scheduler: This is used to iterate the input sequence
Now let's see some examples on how we can create observables from iterables.
To create an observable from an array, you can use the following code:
Rx.Observable
.from([0,1,2])
.subscribe((a)=>console.log(a));
This code prints the following output:
0
1
2
Now let's introduce a minor change in our code, to add the mapFunction argument to it, instead of creating an observable to propagate the elements of this array. Let's use mapFunction to propagate the double of each element of the following array:
Rx.Observable
.from([0,1,2], (a) => a*2)
.subscribe((a)=>console.log(a));
This prints the following output:
0
2
4
We can also use this method to create an observable from an arguments object. To do this, we need to run from() in a function. This way, we can access the arguments object of the function. We can implement it with the following code:
var observableFromArgumentsFactory = function(){
return Rx.Observable.from(arguments);
};
observableFromArgumentsFactory(0,1,2)
.subscribe((a)=>console.log(a));
If we run this code, we will see the following output:
0
1
2
One last usage of this method is to create an observable from either Set() or Map(). These data structures were added to ES6. We can implement it for a set as follows:
var set = new Set([0,1,2]);
Rx.Observable
.from(set)
.subscribe((a)=>console.log(a));
This code prints the following output:
0
1
2
We can also use a map as an argument for the from() method, as follows:
var map = new Map([['key0',0],['key1',1],['key2',2]]);
Rx.Observable
.from(map)
.subscribe((a)=>console.log(a));
This prints all the key-value tuples on this map:
[ 'key0', 0 ]
[ 'key1', 1 ]
[ 'key2', 2 ]
All observables created from this method are cold observables. As discussed before, this means it fires the same sequence for all the observers. To test this behavior, create an observable and add an Observer to it; add another observer to it after a second:
var observable = Rx.Observable.from([0,1,2]);
observable.subscribe((a)=>console.log('first subscriber receives => '+a));
setTimeout(()=>{
observable.subscribe((a)=>console.log('second subscriber receives => '+a));
},1000);
If you run this code, you will see the following output in your console, showing both the subscribers receiving the same data as expected:
first subscriber receives => 0
first subscriber receives => 1
first subscriber receives => 2
second subscriber receives => 0
second subscriber receives => 1
second subscriber receives => 2
- Linux運(yùn)維實(shí)戰(zhàn):CentOS7.6操作系統(tǒng)從入門到精通
- Mastering KVM Virtualization
- 混沌工程:復(fù)雜系統(tǒng)韌性實(shí)現(xiàn)之道
- Windows Server 2012網(wǎng)絡(luò)操作系統(tǒng)企業(yè)應(yīng)用案例詳解
- Linux自動(dòng)化運(yùn)維:Shell與Ansible(微課版)
- 從實(shí)踐中學(xué)習(xí)Kali Linux無線網(wǎng)絡(luò)滲透測試
- Social Data Visualization with HTML5 and JavaScript
- OpenSolaris設(shè)備驅(qū)動(dòng)原理與開發(fā)
- 跟老男孩學(xué)Linux運(yùn)維:Shell編程實(shí)戰(zhàn)
- Windows 7實(shí)戰(zhàn)從入門到精通(超值版)
- Linux內(nèi)核分析及應(yīng)用
- 微軟360度
- Mastering AWS CloudFormation
- Apache ShardingSphere權(quán)威指南
- Instant Responsive Web Design