Andrew Zhang
2021.8.12
Publisher
Observer
notify
subscribe
import { Observable } from 'rxjs';
const source$ = new Observable(observer => {
observer.next(1);
observer.next(2);
observer.next(3);
});
source$.subscribe(item => {
console.log(item);
});
Normal
Error
Complete
next
complete
error
1
2
1
2
1
2
1
2
3
import { Observable } from 'rxjs';
new Observable(observer => {
let number = 1;
setInterval(() => {
observer.next(number++);
}, 1000);
});
new Observable(observer => {
let number = 1;
setInterval(() => {
if (number > 2) {
observer.complete();
}
observer.next(number++);
}, 1000);
});
new Observable(observer => {
let number = 1;
setInterval(() => {
if (number > 2) {
observer.error();
}
observer.next(number++);
}, 1000);
});
Observer
Observer
Observer
Observable
Observable
Observable
1
2
3
1
2
3
Observer A
Observer B
Observer
Observer
Observer
Observable
1
2
3
2
3
Observer A
Observer B
import { Observable } from "rxjs";
const cold = new Observable(observer => {
observer.next(Math.random());
});
cold.subscribe(a => console.log(a));
cold.subscribe(b => console.log(b));
const producer = Math.random();
const hot = new Observable(observer => {
observer.next(producer);
})
hot.subscribe(a => console.log(a))
hot.subscribe(b => console.log(b))
Lazy
Lazier
import { Subject, interval } from 'rxjs';
const tick$ = interval(1000)
const subject = new Subject();
tick$.subscribe(subject);
const subscription1 = subject.subscribe(
item => console.log(item),
);
setTimeout(() => {
const subscription2 = subject.subscribe(
item => console.log(item),
)
}, 1500);
import { interval } from 'rxjs';
const tick$ = interval(1000);
const subscription1 = tick$.subscribe(
item => console.log(item),
);
setTimeout(() => {
const subscription2 = tick$.subscribe(
item => console.log(item),
);
}, 1500);
0
1
2
0
1
2
0
1
2
1
2
Subject
Cold
Observer
Observer
Observer
Cold
hot
import { BehaviorSubject } from 'rxjs';
const subject$ = new BehaviorSubject(0);
const subscription1 = subject$.subscribe(
item => console.log(item),
);
subject$.next(1);
subject$.next(2);
const subscription2 = subject.subscribe(
item => console.log(item),
);
subject$.next(3);
0, 1, 2, 3
2, 3
* 4
* 2
cache
import { interval } from 'rxjs';
import {
map,
filter,
take,
scan,
} from 'rxjs/operators';
interval(1000).pipe(
map(n => Math.pow(n, 2)),
scan((acc, val) => acc + val),
filter(v => v > 10),
take(3),
)
import { interval } from 'rxjs';
import {
map,
filter,
take,
scan,
} from 'rxjs/operators';
interval(1000).pipe(
take(3),
map(n => Math.pow(n, 2)),
scan((acc, val) => acc + val),
filter(v => v > 10),
);
map
scan
fitler
take
import { merge, combineLatest, forkJoin } from 'rxjs';
import { concat, zip, withLatestFrom } from 'rxjs/operators';
const source$ = interval(800);
source$.pipe(
take(3),
map(i =>
interval(500).pipe(take(3))
),
)
High-order Observable
const source$ = interval(800);
source$.pipe(
take(3),
concatMap(i => project(i)),
)
interval(200).pipe(
throttleTime(1000),
debounceTime(1000),
)
interval(200).pipe(
throttleTime(1000),
)
function throwOnUnluckyNumber(num) {
if (num === 4) {
throw new Error('unlucky number 4');
}
return num;
};
const source$ = interval(500).pipe(
map(i => throwOnUnluckyNumber(i))
);
const subscription1 = source$.pipe(
retry(1),
catchError(err => of(8)),
finalize(() => console.log('error or complete'))
)
// make it hot
const subject = new Subject();
source$.subscribe(subject);
const subscription2 = subject.pipe(
retry(1),
catchError(err => of(8)),
finalize(() => console.log('error or complete'))
)
Timeline
No data stack
const source$ = new Subject();
let s = source$.subscribe(v => console.log(v));
source$.next("1");
s = null;
s = source$.subscribe(v => console.log(v));
source$.next("2");
s.unsubscribe();
Memory Leak
ngOnInit() {
this.dataSub = this.getData().subscribe();
this.cancelSub = fromEvent(
cancelBtn,
'click',
).subscribe();
this.rangeSub = fromEvent(
rangeSelector,
'change',
).subscribe();
}
ngOnDestroy() {
this.dataSub.unsubscribe();
this.cancelSub.unsubscribe();
this.rangeSub.unsubscribe();
}
Unsubscribe manually in time
Complete
$destroyer = new Subject();
ngOnInit() {
this.dataSub = this.getData().pipe(
takeUntil(this.destroyer$),
).subscribe();
this.cancelSub = fromEvent(
cancelBtn,
'click',
).pipe(
takeUntil(this.destroyer$),
).subscribe();
this.rangeSub = fromEvent(
rangeSelector,
'change',
).pipe(
takeUntil(this.destroyer$),
).subscribe();
}
ngOnDestroy() {
this.$destroyer.next();
this.$destroyer.complete();
this.$destroyer.unsubscribe();
}
import { Observable } from "rxjs";
import { switchMap, takeUntil } from "rxjs/operators";
declare const a: Observable<number>;
declare const b: Observable<number>;
declare const notifier: Observable<any>;
const c = a
.pipe(
takeUntil(notifier),
switchMap((_) => b)
)
.subscribe((value) => console.log(value));
TakeUntil Leak