generated from amosproj/amos202Xss0Y-projname
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwithLoadingBar.tsx
42 lines (40 loc) · 1.13 KB
/
withLoadingBar.tsx
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
import { Observable, Subscription } from 'rxjs';
import CancellationError from './CancellationError';
import LoadingStore from '../stores/LoadingStore';
type Props = {
loadingStore: LoadingStore;
};
/**
* Pipe for observables that activates a loading backdrop as long as the
* observable is active
*/
export default function withLoadingBar<T>(config: Props) {
return (source: Observable<T>): Observable<T> =>
new Observable<T>((subscriber) => {
const sub = new Subscription();
sub.add(
source.subscribe({
next(x: T) {
subscriber.next(x);
},
error(err: unknown) {
config.loadingStore.removeLoader(sub);
if (err instanceof CancellationError) {
subscriber.complete();
return;
}
subscriber.error(err);
},
complete() {
config.loadingStore.removeLoader(sub);
subscriber.complete();
},
})
);
config.loadingStore.addLoader(sub);
return () => {
config.loadingStore.removeLoader(sub);
sub.unsubscribe();
};
});
}