aboutsummaryrefslogblamecommitdiffhomepage
path: root/client/src/app/helpers/rxjs.ts
blob: 2eaf0a055436be938ae2cffee67276401f1dfdd8 (plain) (tree)
1
2
3
4
5
6
7
8
9
                                

                                                                                                
 

                                                                       
              
                                         
    
                                                       


                                 
                     

                                          



                                                      






                     
import { uniq } from 'lodash-es'
import { Observable } from 'rxjs'
import { bufferTime, distinctUntilChanged, filter, map, share, switchMap } from 'rxjs/operators'

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

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

export {
  buildBulkObservable
}