Angular listen on subscription start of rxjs observable?

Patrick

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?

martin

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,
  }),
);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related