-
Notifications
You must be signed in to change notification settings - Fork 24.8k
/
observables.ts
72 lines (57 loc) 路 1.53 KB
/
observables.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// #docplaster
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
export function docRegionObservable(console: Console) {
// #docregion observable
// declare a publishing operation
const observable = new Observable<number>(observer => {
// Subscriber fn...
// #enddocregion observable
// The below code is used for unit testing only
observer.next(2);
// #docregion observable
});
// initiate execution
observable.subscribe(value => {
// observer handles notifications
// #enddocregion observable
// The below code is used for unit testing only
console.log(value);
// #docregion observable
});
// #enddocregion observable
return observable;
}
export function docRegionUnsubscribe() {
const observable = new Observable<number>(() => {
// Subscriber fn...
});
// #docregion unsubscribe
const subscription = observable.subscribe(() => {
// observer handles notifications
});
subscription.unsubscribe();
// #enddocregion unsubscribe
return subscription;
}
export function docRegionError() {
const observable = new Observable<number>(() => {
// Subscriber fn...
});
// #docregion error
observable.subscribe(() => {
throw new Error('my error');
});
// #enddocregion error
}
export function docRegionChain() {
let observable = new Observable<number>(observer => {
// Subscriber fn...
observer.next(2);
});
observable =
// #docregion chain
observable.pipe(map(v => 2 * v));
// #enddocregion chain
return observable;
}