-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmergeMap.js
46 lines (45 loc) · 1.19 KB
/
mergeMap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const Rx = require('./index');
const GroupSubscription = require('./GroupSubscription');
/**
* Rx.Observable.prototype.mergeMap = require('toy-rx/mergeMap');
*/
module.exports = function mergeMap(transformFn) {
const inObservable = this;
const outObservable = Rx.Observable.create(function subscribe(outObserver) {
const subscription = new GroupSubscription();
let active = 0;
const inObserver = {
next: (x) => {
try {
var inner = transformFn(x);
} catch (e) {
outObserver.error(e);
return;
}
const innerObserver = {
next: (y) => outObserver.next(y),
error: (e) => outObserver.error(e),
complete: () => {
if (--active === 0) {
outObserver.complete();
}
}
}
active++;
subscription.add(inner.subscribe(innerObserver));
},
error: (e) => {
outObserver.error(e);
},
complete: () => {
if (--active === 0) {
outObserver.complete();
}
}
};
active++;
subscription.add(inObservable.subscribe(inObserver));
return subscription;
});
return outObservable;
}