-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathswitchmap.js
More file actions
97 lines (90 loc) · 2.24 KB
/
Copy pathswitchmap.js
File metadata and controls
97 lines (90 loc) · 2.24 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const Observable = require('rxjs/Observable').Observable;
require('rxjs/add/observable/from');
require('rxjs/add/observable/of');
require('rxjs/add/operator/delay');
require('rxjs/add/operator/map');
require('rxjs/add/operator/switchMap');
require('rxjs/add/operator/take');
const tap = require('rxjs/operators').tap;
const interval = require('rxjs').interval;
const timer = require('rxjs').timer;
const switchMap = require('rxjs/operators').switchMap;
function obs() {
// return Observable.from([1, 2, 3]);
return Observable.create(s => {
s.next('a');
s.next('b');
setTimeout(() => {
s.next('c');
}, 3000);
setTimeout(() => {
s.next('d');
s.complete();
}, 9000);
return () => {
console.log(`outer completed`);
}
});
}
function inner(x){
console.log(`calling inner ${x}`);
return Observable.create(s => {
const i$ = interval(1000)
.take(1)
.pipe(
tap(i => console.log(`inside interval ${x}`)) // always the latest interval
)
.subscribe(_ => {
console.log(`fire ${x}`);
s.next(_);
}, null, () => {
console.log(`interval completed ${x}`);
});
return () => {
console.log(`inner completed ${x}`);
i$.unsubscribe();
}
});
}
const subs$ = obs().pipe(
tap(_ => console.log(`tap ${_}`)),
switchMap(_ => {
console.log(`inside switchmap ${_}`);
return inner(_);
})
)
.subscribe(_ => {
console.log(`======== next ${_} ========`);
}, null, () => {
console.log('======== All completed ========');
});
`
tap a
inside switchmap a
calling inner a
tap b
inside switchmap b
calling inner b
inner completed a # cancelled so that one next is not fired.
inside interval b
fire b
======== next 0 ========
interval completed b
tap c
inside switchmap c
calling inner c
inner completed b
inside interval c
fire c
======== next 0 ========
interval completed c
tap d
inside switchmap d
calling inner d
inner completed c
outer completed
inside interval d
fire d
======== next 0 ========
interval completed d
`;