0

I try to make 2 http-requests but they are not really made even though the debug stepping indicates that httpclient.delete is called. the execution never stops in breakpoint inside subscribe.

app.component.ts

@HostListener('window:unload', ['$event'])
  unloadHandler() {

    this.tabService.closeMainWindow();
  }

tab-manager.service.ts

  closeMainWindow(): void {
    if (this.windowId === this.sessionId) {
      this.closeWindows();
      this.logout().subscribe(
        () => this.initCache.emptyCache(),
        err => console.error('Deleting plan session resulted in error', err));
    }
  }


private logout() {
    const ses = this.ngRedux.getState().planning.planSession;
    if (ses !== null) {
      return this.httpClient.delete<IPlanSession>(`${this.url}/${ses.id}`).pipe(
        concatMap(() => this.httpClient.post('api/v1/auth/logout', null).pipe(
          tap(() => this.tokenService.logout()))));
    }
    return this.httoClient.post('api/v1/auth/logout', null).pipe(
      tap(() => this.tokenService.logout()));
  }

EDIT: Here is a question with same problem. However it's old for angular5 and there's no real solution.

char m
  • 7,840
  • 14
  • 68
  • 117
  • did you mean api inside the `logout` function is not being called ? – Yash Rami Mar 19 '20 at 12:19
  • actually not quite sure what is called. Network-tab in browser does not show anything when I debug step by step and next time the plan session is not deleted and logout not made in backend. – char m Mar 19 '20 at 12:46
  • the stepping indicates that both delete and post are called but not really the tokenService.logOut breakpoint is never stopped at. – char m Mar 19 '20 at 12:48
  • but the explanation could be that the unload handler exits and the asynchronic http requests are just not made. – char m Mar 19 '20 at 12:49
  • so the `tokenservice.logout()` is not being called is it correct? – Yash Rami Mar 19 '20 at 12:49
  • yes, that is correct and it does not stop at breakpoint inside subscribe. – char m Mar 19 '20 at 12:52

1 Answers1

0

you can try like this

private logout() {
return new Promise(resolve => {
 const ses = this.ngRedux.getState().planning.planSession;
    if (ses !== null) {
      return this.httpClient.delete<IPlanSession>(`${this.url}/${ses.id}`).pipe(
        concatMap(() => this.httpClient.post('api/v1/auth/logout', null).pipe().subscribe(response => {
this.tokenService.logout()
});
    }
    return this.httoClient.post('api/v1/auth/logout', null).pipe().subscribe(response => {
this.tokenService.logout();
resolve(true);
});
});
  }

Yash Rami
  • 2,276
  • 1
  • 10
  • 16
  • thanks! i can try that but why this would be any different? this is basically same thing. – char m Mar 19 '20 at 13:01
  • the `tap` operator and it is used for side effects inside a stream. So this operator can be used to do something inside a stream and returning the same observable as it was used on. It runs a method to emit a plain isolated side effect. – Yash Rami Mar 19 '20 at 13:03
  • true, this didn't have tap! i try this and let u know. i just checked fast that u just moved subscribe from closeMainWindow to logout. – char m Mar 19 '20 at 13:06
  • `closeMainWindow` remain as it is. means that function is also contain the `subscribe` – Yash Rami Mar 19 '20 at 13:10
  • now i had time to chek this out. this does not make a lot of sense. now it returns subscription and not observable – char m Mar 24 '20 at 08:27
  • @charm now API inside the logout is called or not? – Yash Rami Mar 24 '20 at 08:31
  • this does not build, method returns Subsription. that cannot be subscribed. – char m Mar 24 '20 at 08:34
  • 1
    Got your point!, I just edit my answer can you check and let me know if it is working or not – Yash Rami Mar 24 '20 at 08:37
  • see my edited question. the problem async stuff is not guaranteed to be finished. so your answer ufortunately makes no difference. – char m Mar 24 '20 at 09:27