-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
50 lines (40 loc) · 1.45 KB
/
test.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 } from "./observable";
import { Observer } from "./observer";
const observableAsync = new Observable<string>(subscriber => {
const interval = setInterval(() => subscriber.next('Hi'), 2000);
return () => clearInterval(interval);
});
const subAsync = observableAsync.subscribe(new Observer<string>(x => console.log(x)));
setTimeout(() => subAsync(), 7000);
const observableSync = new Observable<number>(subscriber => {
subscriber.next(1);
subscriber.next(2);
subscriber.next(3);
subscriber.complete();
// wont send
subscriber.next(4);
// also wont trigger error on complete
subscriber.error(new Error('Wont throw'));
return () => undefined;
});
observableSync.subscribe(new Observer(x => console.log(x)));
const mappedObservable = new Observable<number>(subscriber => {
const arr = [10, 20, 30, 40, 50];
arr.forEach(value => subscriber.next(value));
return () => undefined;
}).map(value => value / 10)
.map(value => value + value);
mappedObservable.subscribe(new Observer(console.log));
const isDegreeOf2 = (value: number) => {
if (value === 1) return true;
let divisor = 2;
while(value > 1) {
value /= divisor;
divisor *= 2;
}
return value === 1;
}
const filteredObservable = mappedObservable.filter(isDegreeOf2);
filteredObservable.subscribe(new Observer(console.log));
const tappedObservable = mappedObservable.tap(x => console.log(`x = ${x}`));
tappedObservable.subscribe(new Observer(console.log));