rxjs-web icon indicating copy to clipboard operation
rxjs-web copied to clipboard

wraping fromNetwork in existing service function

Open sysmat opened this issue 5 years ago • 6 comments
trafficstars

I'm using angular http and can't get it to work in my component.

  • service existing code:
search(criteria: string): Observable<OrgSearch[]> {
    return this.http
              .get<OrgSearch[]>(`api/info/search?q=${criteria}`, this.auth.getHttpAuthOpt())
              .pipe(catchError((error: Response) => observableThrowError(error)));
  }
  • component existing code:
this.infoService
        .search(this.orgSearchCondition)
        .pipe(
          takeUntil(this.onDestroyComponent$),
          stopSpiner<OrgSearch[]>(() => this.isLoading = false),
          showError<OrgSearch[]>(() => {
            this.uiService.showErrorFlashMessage('prišlo je do napake pri iskanju članic!');
            return EMPTY;
          })
        )
        .subscribe(result => this.onSearchOk(result, this.orgSearchCondition));
  • but when I wrap it, it's never triggered
search(criteria: string): Observable<OrgSearch[]> {
    return fromNetwork()
      .pipe(
        first(),
        switchMap(net => {
          console.log(`net=${net}`);
          return this.http
            .get<OrgSearch[]>(`api/info/search?q=${criteria}`, this.auth.getHttpAuthOpt())
            .pipe(catchError((error: Response) => observableThrowError(error)));
        })
      );
  }
  • I'm doing something wrong?
  • The idea of lib is great

Regards

sysmat avatar Nov 01 '20 10:11 sysmat

Hi thanks for your kind words. So I checked the code again, I could imagine, that your browser is maybe not supporting the network API, but then it should emit an error. Can you reproduce the behavior in a stackblitz and share the code with me, then I can have a more in-depth look into this. Sorry for the hassle

niklas-wortmann avatar Nov 01 '20 16:11 niklas-wortmann

Even if try directly subscribe, nothing happened, not even an error

<button (click)="rxjsWeb()">
    rxjsWeb
</button>
public rxjsWeb() {
    console.log('rxjsWeb');
    fromNetwork()
      .pipe(
        first(),
        map(net => { console.log(`net=${net}`); return true; })
      )
      .subscribe(
        result => console.log(`net=${result}`),
        err => console.error(`err=${err}`)
      );
  }
  • In Edge(LTS) & Chrome(LTS) in web development console nothing
  • In Firefox(LTS) in web development console I got err=Error: Network API is not fully supported
  • When I'll have time, I'll provide a case in stackblitz Regards

sysmat avatar Nov 02 '20 07:11 sysmat

If I try by native API it works in any browser

const conn = (navigator as any).connection;
    if (conn) {
      const connectionlist = ['slow-2g', '2g', '3g', '4g'];
      const effectiveType = conn.effectiveType;
      console.log(effectiveType);
    }

sysmat avatar Nov 10 '20 10:11 sysmat

This is weird. Thanks a lot for the information, but I feat that I am not able to investigate in that without a reproduction. I would really appreciate if you could provide some kind of reproduction.

niklas-wortmann avatar Nov 10 '20 14:11 niklas-wortmann

Here is demo

  • https://stackblitz.com/edit/angular-rxjs-web?file=src/app/hello.component.ts
  • https://angular-rxjs-web.stackblitz.io

sysmat avatar Nov 11 '20 11:11 sysmat

thanks a lot, I will have a look and come back to you!

niklas-wortmann avatar Nov 11 '20 12:11 niklas-wortmann