diff options
Diffstat (limited to 'client/src/app/helpers/rxjs.ts')
-rw-r--r-- | client/src/app/helpers/rxjs.ts | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/client/src/app/helpers/rxjs.ts b/client/src/app/helpers/rxjs.ts new file mode 100644 index 000000000..eb051f868 --- /dev/null +++ b/client/src/app/helpers/rxjs.ts | |||
@@ -0,0 +1,29 @@ | |||
1 | import { uniq } from 'lodash-es' | ||
2 | import { asyncScheduler, Observable } from 'rxjs' | ||
3 | import { bufferTime, distinctUntilChanged, filter, map, observeOn, share, switchMap } from 'rxjs/operators' | ||
4 | import { NgZone } from '@angular/core' | ||
5 | import { enterZone, leaveZone } from './zone' | ||
6 | |||
7 | function buildBulkObservable <T extends number | string, R> (options: { | ||
8 | ngZone: NgZone | ||
9 | notifierObservable: Observable<T> | ||
10 | time: number | ||
11 | bulkGet: (params: T[]) => Observable<R> | ||
12 | }) { | ||
13 | const { ngZone, notifierObservable, time, bulkGet } = options | ||
14 | |||
15 | return notifierObservable.pipe( | ||
16 | distinctUntilChanged(), | ||
17 | // We leave Angular zone so Protractor does not get stuck | ||
18 | bufferTime(time, leaveZone(ngZone, asyncScheduler)), | ||
19 | filter(params => params.length !== 0), | ||
20 | map(params => uniq(params)), | ||
21 | observeOn(enterZone(ngZone, asyncScheduler)), | ||
22 | switchMap(params => bulkGet(params)), | ||
23 | share() | ||
24 | ) | ||
25 | } | ||
26 | |||
27 | export { | ||
28 | buildBulkObservable | ||
29 | } | ||