S
Size: a a a
S
hm
S
S
DP
hm
S
hm
L
R
R
R
С
L
export class LoadingService {
isLoading$: Observable<boolean>;
private loadingCount$ = new BehaviorSubject(0);
constructor() {
this.isLoading$ = this.loadingCount$
.pipe(
map(count => count > 0),
distinctUntilChanged()
);
}
inc() {
this.updateCount(c => c + 1);
}
dec() {
this.updateCount(c => (c > 0
? c - 1
: 0));
}
public track<T>(destroyed$: Observable<void>) {
return (target: Observable<T>) =>
Observable.create((observer: Observer<T>) => {
this.inc();
const sub = target
.pipe(takeUntil(destroyed$))
.subscribe(observer);
return () => {
this.dec();
sub.unsubscribe();
};
});
}
private updateCount(fn: (count: number) => number) {
this.loadingCount$.next(fn(this.loadingCount$.value));
}
}
L
S
hm
S
R