aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/helpers/rxjs.ts
blob: 625bca0f7d6231daf75e312670af3c5de14655cd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { uniq } from 'lodash-es'
import { Observable } from 'rxjs'
import { bufferTime, distinctUntilChanged, filter, map, share, switchMap } from 'rxjs/operators'

function buildBulkObservable <T extends number | string, R> (options: {
  notifierObservable: Observable<T>
  time: number
  bulkGet: (params: T[]) => Observable<R>
}) {
  const { notifierObservable, time, bulkGet } = options

  return notifierObservable.pipe(
    distinctUntilChanged(),
    bufferTime(time),
    filter(params => params.length !== 0),
    map(params => uniq(params)),
    switchMap(params => bulkGet(params)),
    share()
  )
}

export {
  buildBulkObservable
}