0

What would be the optimal structure to use in Angular 2+ component so as to utilize the power of rxjs switchMap() in a back-end call via a service?

Notes:

  • We have a nested component firing a custom "fetch" event (so fromEvent(myBtn2.nativeElement, 'fetch').pipe(...); is not firing, see comment (1), so we workaround it with Subject. If there's a way harness fetch with fromEvent(), we could spare the use of Subject )

Sample implementation in StackBlitz here

app.component.html

<app-inner #myBtn1 (fetch)="onFetch1($event)" [data]="data"></app-inner>
<app-inner #myBtn2 (fetch)="onFetch2($event)" [data]="data"></app-inner>

app.component.ts

@Component({...})
export class AppComponent implements {

  @ViewChild('myBtn2', { read: ElementRef }) myBtn2: ElementRef;

  public data = 0;

  private BTN = { GET: new Subject() };
  private subs = new Subscription();

  constructor (private _ds: DataService) {}

  public ngOnInit() {
    this.setBtnLogic();
  }

  public onFetch1(ev) {
    this.subs.add(
      this._ds.getData().subscribe(res => {
        this.data = res;
        console.log('<<<', res);        // <-- logs 10 times
      })
    );
  }

  public onFetch2(ev) {
    this.BTN.GET.next(ev);
  }

  public setBtnLogic(ev?) {

    // fromEvent(this.myBtn2.nativeElement, 'fetch')   // <-- (1) this is not working
    this.BTN.GET
    .pipe(
      switchMap(p => this._ds.getData()),
      tap(data => this.data = data),
      tap(res => console.log('<<< observable ', res)), // <-- logs only the last response 
    ).subscribe();
  }

   // unsubscribes in ngOnDestroy() ...
}
zaggi
  • 870
  • 1
  • 7
  • 24
  • So you want `fromEvent(myBtn2.nativeElement, 'fetch')` to work?! Are you asking about anything else? What do you exaclty mean by `What would be the optimal structure to use in Angular 2+ component so as to utilize the power of rxjs switchMap() in a back-end call via a service?` – frido Jan 17 '19 at 15:17
  • If you don't want the canceling affect of `switchMap` you should use `mergeMap` to log every response. – frido Jan 17 '19 at 15:28
  • @frido, just the opposite - I need exactly the cancelling effect and I wonder what would be the most elegant way (i.e. shorter and more elegant if available) to the provided sample implementation – zaggi Jan 18 '19 at 15:36
  • @frido, (1) how to hook the custom event with fromEvent() as it would make the provided sample shorter (the Subject won't be necessary in this case) and (2) optimal = shorter/more elegant/or just another opinion on the implementation. Thanks – zaggi Jan 18 '19 at 15:37
  • In your example you're binding the value `$event` you get from `app-inner` to `data` of `app-inner`. That doesn't really make sense. Why would you want to get a value from a component just to give it straight back to the same component? – frido Jan 18 '19 at 16:02
  • @fridoo, the focus is on how to do harness the custom event and approach to use A2+ swtichMap optimally, the code practical use is immaterial – zaggi Jan 18 '19 at 16:25

0 Answers0