Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
How can I end interval without unsubscribing in RxJS?

I have database queries that get executed and return data from the server. When the data returns my subscription works. If I call refreshData the queries execute again and return data, however the subscription in AnotherComponent doesn't run the second time.

I believe that this is caused by take(1) which is taking one item and ending the subscription. Is there a way that I can end the interval, but keep the subscription active in AnotherComponent?

Note: I decided to use an interval because some items subscribe too late and never receive the initial data.

I don't think that a ReplaySubject is the way to go because I don't care what the previous data was.

This service makes the applications initial queries needed to load a page.

export class StartupGraphqlService {

  private initCalled = false;
  private queryResults?: StartupCompletion;

  private readonly startupQueries = new Subject<StartupCompletion>();

  get $startupQueries() {
    return interval(100)
      .pipe(
        filter(() => !!this.queryResults),
        map(() => this.queryResults as StartupCompletion),
        take(1)
      );
  }

  refreshData() {
    this.initCalled = false;
    this.initQueries();
  }

  initQueries() {
    if (this.initCalled) return;
    this.initCalled = true;
    // Execute queries
    // ...
    this.startupQueries.next(data)
  }
}

This component executes the initial query then 3 seconds later (for testing) executes it again.

export class AppComponent {
  ngOnInit() {
    this.authService.loggedIn().subscribe(logged => {
      if(logged) {
        this.startupQueries.initQueries();
        timer(3000).subscribe(() => {
          console.log('Refresh Queries');
          this.startupQueries.refreshData();
        });
      }
    }
  }
}

This should display the result every time it is updated.

export class AnotherComponent {
  ngOnInit() {
    this.startup.$startupQueries.subscribe(result => {
      console.log(result);
    })
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I found that using a BehaviorSubject works, it seems to be doing what I intended.

  private queryResults?: StartupCompletion;
  private readonly startupQueries = new BehaviorSubject<StartupCompletion | undefined>(this.queryResults);
  $startupQueries = this.startupQueries.asObservable().pipe(
    filter(() => !!this.queryResults),
    map(() => this.queryResults as StartupCompletion),
  );
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda