0

I have a variable to which I assign a subscription to a request, I want to get the last value issued by that subscription. My code is similar to the following

// Service 1
public request
getData() {
    return new Observable((observer) => {
      if (this.request) this.request.unsubscribe();
      this.request = this.http.get(this.requestExploreUrl()).subscribe((data) => {
        observer.next(data);
      });
    });
  }

I realize that the code doesn't make much sense, but there are several components that consume this service, is it possible to subscribe to the value of the request variable?

I have tried service1.request.subscribe (x => x) doesnt work

Also you cannot try another service1.getData().subscribe (x => x) because having an if (this.request) this.request.unsubscribe (); the last subscription will be canceled.

juanjinario
  • 596
  • 1
  • 9
  • 24
  • are you trying to cache the last request result? – Fan Cheung Apr 15 '20 at 02:29
  • Yes, I need to know the last result.emited, I have a this.request = httpClient.subscribe(); How I can get the las data to this.request ? – juanjinario Apr 15 '20 at 08:35
  • You can take a look here: https://stackoverflow.com/questions/61118662/angular-http-interceptor-make-requests-one-after-another/61123449#61123449 – Fan Cheung Apr 15 '20 at 08:54
  • I don't want a caching solution, I want to know if there is a command to review a subscription that is in "closed" state, I made a console.log ('this.request') and it has many properties I wanted to know if there is also an event that allow to obtain the data that I require @FanCheung – juanjinario Apr 15 '20 at 10:39
  • https://stackoverflow.com/questions/52561728/what-is-the-difference-between-the-isstopped-and-closed-property-of-the-subject that gives u answer about those property, but we rarely need to use such property in rx programming. – Fan Cheung Apr 15 '20 at 10:55
  • I have found those, but I don't know if there is one that gives me the last value issued, or I would like to know if there is another option to transform the variable back into an observable – juanjinario Apr 15 '20 at 11:08
  • The first link I posted already has the answer, that’s the rx way of doing things – Fan Cheung Apr 15 '20 at 11:09
  • Dont understand this http.get(url).subscribe(this._cache), and I dont know how use the unsubscribe() in my request – juanjinario Apr 15 '20 at 11:17
  • You don’t have to unsubscribe, http is an one off subscription, if it times out it will throw error and stop ur subscription – Fan Cheung Apr 15 '20 at 11:18

1 Answers1

0

I think you want to share the result of your request between all the components that need it.

Take a look at the stackblitz for the full version and learn-rxjs for the shareReplay

app.component.ts

import { Component } from '@angular/core';
import { DataService } from './data.service';
import { Observable } from 'rxjs';
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular';
  constructor(public dataService: DataService) {
  }

  request() {
    this.dataService.getData().subscribe()
  }
}

app.component.ts

import { Injectable } from '@angular/core';
import { BehaviorSubject, Subject, of } from 'rxjs';
import { shareReplay, tap } from 'rxjs/operators';

@Injectable()
export class DataService {
  _data = new Subject();
  data$ = this._data.asObservable().pipe(shareReplay(1));
  constructor() { }

  getData() {
    return of(new Date().getTime())
    .pipe(
      tap((value) => this._data.next(value))
    )
  }
}

app.component.html

<button (click)=request()>Request</button>
{{ dataService.data$ | async }}
<hr>
<app-a></app-a>
<hr>
<app-b></app-b>
Lievno
  • 1,013
  • 9
  • 14
  • No, i made an http.get request and stored it in a variable, i want to see what value the last query returned. Your example is also not very clear. – juanjinario Apr 22 '20 at 22:36