-
Notifications
You must be signed in to change notification settings - Fork 0
/
02-observable_multi.ts
50 lines (49 loc) · 1.49 KB
/
02-observable_multi.ts
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
import { Observable, Subscription } from 'rxjs';
import { subscribeToArray } from 'rxjs/internal/util/subscribeToArray';
import { unsubscibe } from './0-unSub';
export class ObservableMulti {
create() {
return new Observable<string>((subscriber) => {
console.log('Observable executed');
subscriber.next('Alice');
setTimeout(() => subscriber.next('Ben'), 2000);
setTimeout(() => subscriber.next('Charlie'), 4000);
setTimeout(() => subscriber.complete(), 4500);
return () => {
// Teardonw
console.log('Self Teardonw');
subscriber.unsubscribe();
};
});
}
run() {
// Each subscription is independent
const observable$ = this.create();
// subscriber 1
console.log('Subscription1 starts');
const subscription01 = observable$.subscribe(
// also recommended ways of subscibe like
{
next: (value) => console.log('Subscription01', value),
error: (err) => {},
complete: () => {
console.log('Subscription01 Complete');
unsubscibe([subscription01]);
},
}
);
// subscriber 2
setTimeout(() => {
console.log('Subscription2 starts');
const subscription02 = observable$.subscribe(
//FIXME: // <= this is deprecated
(value) => console.log('Subscription02', value),
(err) => {},
() => {
console.log('Subscription02 Complete!');
unsubscibe([subscription02]);
}
);
}, 1000);
}
}