typescript 48 lines · 8 steps

Autosaving a form with RxJS in Angular

An Angular component turns form changes into a debounced, self-cleaning autosave pipeline with a live status signal.

Explained by highlit
1@Component({
2 selector: 'app-article-editor',
3 templateUrl: './article-editor.component.html',
4})
5export class ArticleEditorComponent implements OnInit, OnDestroy {
6 @Input() articleId!: string;
7 
8 readonly form = this.fb.nonNullable.group({
9 title: ['', Validators.required],
10 summary: [''],
11 body: [''],
12 });
13 
14 readonly saveState = signal<'idle' | 'saving' | 'saved' | 'error'>('idle');
15 
16 private readonly destroy$ = new Subject<void>();
17 
18 constructor(
19 private readonly fb: FormBuilder,
20 private readonly drafts: DraftService,
21 ) {}
22 
23 ngOnInit(): void {
24 this.form.valueChanges
25 .pipe(
26 filter(() => this.form.valid),
27 map(() => this.form.getRawValue()),
28 debounceTime(800),
29 distinctUntilChanged((a, b) => JSON.stringify(a) === JSON.stringify(b)),
30 tap(() => this.saveState.set('saving')),
31 switchMap((draft) =>
32 this.drafts.save(this.articleId, draft).pipe(
33 catchError(() => {
34 this.saveState.set('error');
35 return EMPTY;
36 }),
37 ),
38 ),
39 takeUntil(this.destroy$),
40 )
41 .subscribe(() => this.saveState.set('saved'));
42 }
43 
44 ngOnDestroy(): void {
45 this.destroy$.next();
46 this.destroy$.complete();
47 }
48}
01 / 01
STEP 01

Walkthrough

Space play ←→ step click any line
Three takeaways
  1. 1Chaining RxJS operators lets you express debounce, dedupe, and cancellation as one declarative pipeline.
  2. 2switchMap plus catchError keeps a save stream alive by cancelling stale requests and swallowing errors without breaking the observable.
  3. 3A destroy Subject with takeUntil is a reliable pattern for tearing down subscriptions when a component is destroyed.

Related explainers

typescript
import { registerLocaleData } from '@angular/common';
import localeFr from '@angular/common/locales/fr';
import localeFrExtra from '@angular/common/locales/extra/fr';
import localeDe from '@angular/common/locales/de';

Locale-aware bootstrapping in Angular

i18n localization dependency-injection
Intermediate 8 steps
typescript
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import * as Joi from 'joi';
 

Validating env config at boot in NestJS

configuration schema-validation environment-variables
Intermediate 8 steps
typescript
import { Inject, Injectable, Logger } from '@nestjs/common';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Cache } from 'cache-manager';
import { InjectRepository } from '@nestjs/typeorm';

A cache-aside country lookup in NestJS

cache-aside dependency-injection batch-lookup
Intermediate 8 steps
typescript
import { Injectable, effect, signal, computed } from '@angular/core';
 
interface Preferences {
  theme: 'light' | 'dark';

A signal-based preferences store in Angular

signals state-management persistence
Intermediate 7 steps
typescript
import { useEffect, useState } from "react";
 
interface Section {
  id: string;

Building a scroll-spy hook in React

custom-hooks intersectionobserver dom-observation
Intermediate 8 steps
typescript
import { Injectable, Scope, Inject, NotFoundException } from '@nestjs/common';
import { REQUEST } from '@nestjs/core';
import { Request } from 'express';
import { DataSource } from 'typeorm';

Per-tenant database connections in NestJS

multi-tenancy connection-pooling dependency-injection
Advanced 8 steps

Share this explainer

Here's the card — post it anywhere.

Autosaving a form with RxJS in Angular — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code