Maxime Dauvergne
Maxime Dauvergne

Reputation: 13

Finalize operator not called when wrapped

I am using a angular service to wrap some of my http calls in order to manage the loading state of my application.

This service stores the status of the calls for further process. Unfortunately the finalize function is never called.

Here is the wrapper:

searchWithLoader(obs: Observable<any>) {
   this.show();
   obs.pipe(finalize(() => {
      console.log("FINALIZE NOT CALLED");
      this.hide();
   }));

   return obs;
}

And the way I typically call it :

   let performanceLoader = this.loaderService.searchWithLoader(
      this.performanceDataService.getSomething({
        x: x,
        y: y,
      }));

    performanceLoader.subscribe(() => {});

Is there a way around this in order for the finalize to be called in a service instead of the component?

Upvotes: 1

Views: 460

Answers (2)

William Stevens
William Stevens

Reputation: 552

When you call the pipe function of the obserable it doesn't mutate the observable. Instead, it returns a new observable. So you should set obs to obs.pipe() like below

searchWithLoader(obs: Observable<any>) {
   this.show();
   return obs.pipe(finalize(() => {
      console.log("FINALIZE NOT CALLED");
      this.hide();
   }));      
}

Upvotes: 4

Patricio Vargas
Patricio Vargas

Reputation: 5522

Your code

 this.performanceDataService.getSomething({
        x: x,
        y: y,
      }))

It's not an observable, it need to be an observable when you pass it to the searchWithLoader function. UNLESS you can show the code fo the getSomething we can help you more

Upvotes: 1

Related Questions