The Real Fullstack

The Endless Subscription

The analytics dashboard reports five “price seen” events per tick. There is only one price on screen. Where do the other four come from?

Explorador7 minSimulador de linha do tempoDesafio de código
angularrxjstypescriptreactvue

Este desafio ainda não foi traduzido para Português (Brasil). Você está lendo a versão em inglês.

Missão:Reproduce the growth by navigating away and back, then choose a teardown strategy that keeps exactly one live subscription — without freezing the price.Contexto · Objetivos de aprendizagem ▾

Quillfin, a fictional trading app built with Angular, shows a live price ticker on the instrument page. Users who browse between instruments for a while report the tab slowing down, and the analytics team sees event counts that grow with session length.

Missão: Reproduce the growth by navigating away and back, then choose a teardown strategy that keeps exactly one live subscription — without freezing the price.

Objetivos de aprendizagem

  • Explain why a subscription to a never-completing stream outlives its component.
  • Choose between takeUntilDestroyed, the async pipe / toSignal and manual unsubscribe.
  • Recognise why take(1) is not a lifecycle fix for live data.
SimulaçãoSimulado

Execute a simulação para ver a saída.

Executar simulação
@Component({ selector: 'app-ticker', template: `{{ price() }}` })export class TickerComponent implements OnInit {  private prices = inject(PriceStream);   // websocket-backed, never completes  readonly price = signal(0);  ngOnInit() {    this.prices.updates$.subscribe((p) => {   // nobody ever unsubscribes      this.price.set(p);      this.analytics.track('price-seen');      // runs once per leaked instance    });  }}

Simplificação

Each visit creates exactly one component instance and the stream ticks once per step. Memory figures are illustrative, not measured. Modelo educacional — os números ilustram comportamento, não são benchmarks.

Conceitos

Desafios relacionados

Referências