Search code examples
typescriptrxjsobservableangular2-servicesside-effects

Using combineAll to replace switchMap and map?


I tried to use combineAll to, well, combine all, I tried different combinations but could not get it work. But I managed to get some working code by doing this ugly nesting with all these switchMap/map. How can I achive the same result using combineAll ?

@Effect()
  SelectItem$: Observable<Action> = this.actions$
    .ofType(supernode.ActionTypes.SET_SELECTED_ITEM)
    .debounceTime(100)
    .switchMap(() => {
      return this.itemService.isEditable().switchMap(
        edit => this.itemService.isSharable().switchMap(
          share => this.itemService.isListable().map(
            list => new item.SetShareStatus({
              listable: list, editable: edit, sharable: share
            })
          )
        )
      ).take(1);
    })
    .catch(this.handleError);

Solution

  • I'm not sure what all the functions you call should do but it seems to me you don't need to use neither combineAll nor switchMap. I think you would do better with zip() or forkJoin():

    .switchMap(() => Observable.forkJoin(
        this.itemService.isEditable()
        this.itemService.isSharable()
        this.itemService.isListable()
      ))
      .map(results => new SetShareStatus({
        listable: results[2], editable: results[1], sharable: results[1]
       }))
    })
    

    I think combineLatest() isn't a good choice because it emits when any of its source Observables emit which is I think what you don't want.