Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular listen on subscription start of rxjs observable?

Is there a way to detect the start of the subcription of a rxjs observable within the pipe?

I want to trigger a loading indicator when a http observable (destroyed when respone has been finalized) get subscribed.

Or do I have to create a wrapper observable for this action?

like image 907
Patrick Avatar asked Sep 05 '25 03:09

Patrick


1 Answers

It depends on what RxJS version you're using. With RxJS < 7.3 you can use defer():

defer(() => {
  loadingFlag = true;
  return this.http.doyourrequest().pipe(
    finalize(() => loadingFlag = false),
  );
});

Since RxJS >= 7.3 you can use new event handlers for tap():

this.http.doyourrequest().pipe(
  tap({
    subscribe: () => loadingFlag = true,
    finalize: () => loadingFlag = false,
  }),
);
like image 138
martin Avatar answered Sep 07 '25 23:09

martin