DEV Community

Erwan Raulo
Erwan Raulo

Posted on Edited on

Angular Resource for reads. Here's the missing piece for writes

Angular's reactivity story has never felt this good.

With Angular 22, resource() and httpResource() are stable. Fetching data is simple: describe what you depend on, read the resulting signals, and let Angular handle the reactive plumbing.

Then you need to save something.

Angular deliberately does not use resource() for writes. So what should a Signal-native write API look like?

That's the problem @ngsignal/mutation tries to solve. Its API mirrors Angular's resource family:

Reads Writes Input
resource() mutation() Promise
rxResource() rxMutation() Observable
httpResource() httpMutation() HTTP request description

Make writes feel like Angular resources, without building another data layer.

Not a lot of code: a behavior contract for writes (state, errors, lifecycle, SSR, concurrency).

Cancelling a read discards work. Cancelling a write doesn't undo it.

The resource API is intended for reads, and the httpResource guide recommends using HttpClient directly for POST and PUT. The Resource RFC sums it up: a resource declares a dependency on data; it is not an imperative primitive for mutations.

The reason: cancellation means something different for a write. Abort a POST /orders and the browser stops waiting, but the server may already have created the order.

For reads, cancellation discards stale work. For writes, it can make you lose the result of work that already happened.

Cancelling a GET discards a stale read, but cancelling a POST /orders leaves order #1042 on the server while the client shows no order

So Angular leaves the primitive to HttpClient and lets application libraries decide how to model write state.

The problem is that every application can end up rebuilding the same state machine.

"Just use HttpClient" gets complicated fast

A saving signal, an error signal and a call to post(): easy. Then real usage starts:

  • Race conditions: an older response overwrites a newer one.
  • Double submits: three clicks, three writes.
  • Ordering: two updates land in the wrong order.
  • Lifecycle: the component dies mid-request.
  • SSR: rendering must wait for pending work.

The HTTP call is easy. The state machine is the hard part.

Why not use a bigger data library?

TanStack Query offers mutations inside a full data-synchronization system (cache, queries, devtools, invalidation), and NgRx Toolkit offers mutation primitives with RxJS concurrency strategies. Both are great when you need that.

@ngsignal/mutation takes the opposite approach:

No store. No cache. No provider. No RxJS in the core. Just a small Signal-native primitive for writes.

Meet mutation()

npm install @ngsignal/mutation
Enter fullscreen mode Exit fullscreen mode

A mutation is just a function that turns an input into a Promise:

readonly createTodo = mutation<string, Todo>({
  mutationFn: (title, abortSignal) =>
    this.api.createTodo(title, abortSignal),

  onSuccess: (todo) => {
    this.todos.update((list) => [...list, todo]);
  },
});
Enter fullscreen mode Exit fullscreen mode

That's the imperative side.

The other side is reactive: mutation() exposes read-only signals for its status, value, error, input and derived states such as isPending() and isSuccess(). The template reads them directly:

<button (click)="createTodo.mutate(title())">
  {{ createTodo.isPending() ? 'Saving…' : 'Add todo' }}
</button>

@if (createTodo.isError()) {
  <p class="error">Could not save the todo.</p>
}
Enter fullscreen mode Exit fullscreen mode

mutate() also returns a Promise: error() holds the latest call's error for the UI, so await the Promise only when the caller needs that call's result. Ignoring it never causes an unhandled rejection.

So the model becomes:

Imperative trigger. Reactive state.

The important part is that common write problems now have explicit semantics:

Problem Behavior
Race conditions Only the latest call writes status(), value() and error(); isPending() stays true while any call is in flight; every executed call still runs its callbacks
Double submits concurrency: 'drop' rejects new calls while one is pending
Ordering concurrency: 'queue' runs calls sequentially
Lifecycle Destroyed contexts cannot commit stale results
SSR In-flight work participates in PendingTasks

That is most of the API.

What it replaces

For comparison, here is the same write done by hand with the same guarantees (stale-response guard, abort on destroy, SSR pending task) but without any queue or drop logic yet:

readonly status = signal<'idle' | 'pending' | 'success' | 'error'>('idle');
readonly error = signal<unknown>(undefined);
private generation = 0;
private destroyed = false;
private readonly controllers = new Set<AbortController>();
private readonly pendingTasks = inject(PendingTasks);

constructor() {
  inject(DestroyRef).onDestroy(() => {
    this.destroyed = true;
    this.controllers.forEach((controller) => controller.abort());
  });
}

async createTodo(title: string) {
  const generation = ++this.generation;
  const controller = new AbortController();
  this.controllers.add(controller);
  this.status.set('pending');
  this.error.set(undefined);
  const removeTask = this.pendingTasks.add();
  try {
    const todo = await this.api.createTodo(title, controller.signal);
    if (this.destroyed) return;
    this.todos.update((list) => [...list, todo]);
    if (generation !== this.generation) return;
    this.status.set('success');
  } catch (error) {
    if (this.destroyed || generation !== this.generation) return;
    this.status.set('error');
    this.error.set(error);
  } finally {
    removeTask();
    this.controllers.delete(controller);
  }
}
Enter fullscreen mode Exit fullscreen mode

Thirty-odd lines, repeated for every write in the application. The live demo has both versions of the same page side by side (/ and /manual).

rxMutation() and httpMutation()

Your services already return Observables? Keep them:

import { rxMutation } from '@ngsignal/mutation/rxjs-interop';

readonly createUser = rxMutation<CreateUserDto, User>({
  mutationFn: (dto) => this.userService.create(dto),
});
Enter fullscreen mode Exit fullscreen mode

Like rxResource(), the call resolves with the last value, once the Observable completes. HttpClient emits once and completes, so most services need nothing more; an Observable that never completes stays pending until reset() or destroy. RxJS stays out of the core bundle.

rxMutation marble diagram: the mutation resolves with the last value when the Observable completes; an Observable that completes without a value rejects

httpMutation() goes further: describe the request, the same way as with httpResource(), and get HTTP signals back. Say, a file upload with a progress bar:

import { httpMutation } from '@ngsignal/mutation/http';

readonly upload = httpMutation<File, Attachment>({
  request: (file) => ({
    method: 'POST',
    url: '/api/attachments',
    body: file,
    reportProgress: true,
  }),
});
Enter fullscreen mode Exit fullscreen mode
@if (upload.uploadProgress(); as progress) {
  <progress [value]="progress.loaded" [max]="progress.total"></progress>
}
Enter fullscreen mode Exit fullscreen mode

Validating the response takes one option:

readonly createUser = httpMutation<CreateUserDto, User>({
  request: (dto) => ({ method: 'POST', url: '/api/users', body: dto }),
  parse: (body) => userSchema.parse(body), // Zod, Valibot, or plain code
});
Enter fullscreen mode Exit fullscreen mode

A subtle case: when parsing throws, the user was already created. The status says error, the status code says 201; retry blindly and you create it twice. So mutate() rejects with an HttpMutationParseError, which a retry policy can tell apart from a failed request.

httpMutation: the server creates the user and returns 201, then parse throws, so status() is 'error' while statusCode() is 201

Response headers, download progress and a .blob() variant (for a PDF generated by a POST) are there too.

The interesting part comes next.

The key design choice: a new call never cancels the previous one

A new mutate() call does not abort the previous one; only reset() or destroying the injection context does. Each call gets a generation number, and only the latest one writes the signals.

Two mutate() calls: A finishes after B and is not aborted; only B, the latest call, commits to the signals, but both run onSuccess, B with isLatest true and A with isLatest false

"This result is stale" does not mean "this write must be cancelled."

That is exactly what switchMap semantics get wrong for writes.

The callbacks, though, belong to every call: a superseded write still happened on the server. Create two todos quickly and both are appended; a superseded call that fails still reports its error. For side effects that should only follow the current call, callbacks receive { isLatest }:

onSuccess: (todo, title, { isLatest }) => {
  this.todos.update((list) => [...list, todo]); // every call
  if (isLatest) this.form.reset();              // current call only
},
Enter fullscreen mode Exit fullscreen mode

Ordered writes in eight lines

Parallel writes can be processed out of order, and that breaks when order matters.

Think autosave: draft A → draft B → draft C sent together can be processed out of order. With concurrency: 'queue', a request is only sent once the previous one settles:

readonly saveDraft = httpMutation<Draft, Draft>({
  request: (draft) => ({
    method: 'PUT',
    url: `/api/drafts/${draft.id}`,
    body: draft,
  }),
  concurrency: 'queue',
});
Enter fullscreen mode Exit fullscreen mode

The queue itself is tiny:

let queueTail: Promise<void> = Promise.resolve();

function enqueue<T>(task: () => Promise<T>): Promise<T> {
  const result = queueTail.then(task, task);

  queueTail = result.then(
    () => undefined,
    () => undefined,
  );

  return result;
}
Enter fullscreen mode Exit fullscreen mode

The eight-line Promise queue: A rejects, the tail swallows the failure, B and C still run, and only C commits to the signals

No array, no scheduler, no RxJS. queueTail.then(task, task) starts the next task once the previous one settles, success or failure. The tail swallows that outcome so one failure cannot poison the queue, while each caller still gets its own result.

Because requests are built when they start, an auth token or an ETag is read at send time, not at click time. And reset() aborts calls still waiting, so they are never sent.

Writes to the same data often come from several mutations (a rename on blur, then a delete): mutationQueue() gives them one shared queue, optionally per entity id. See Ordering writes across mutations.

The queue orders requests as the client sends them. If the server needs stronger guarantees, versioning or ETag-based concurrency control still apply.

Three concurrency modes

Mode Good fit RxJS analogue
'parallel' (default) Independent actions mergeMap-like execution, latest call owns the state
'queue' Autosave, ordered updates concatMap
'drop' Submit, payment exhaustMap
— — No switchMap: a new write never aborts the previous one

The same three clicks with the default, 'queue' and 'drop' concurrency modes

There is no switchMap because switching means aborting the previous write. A dropped call never touches the signals or the callbacks: only its Promise rejects, with an AbortError, which matters only if you await it.

Was the write sent?

Back to the opening problem: once a write is cancelled, the question is whether it reached the server. Every cancellation rejects with an AbortError whose message answers it:

Cancelled by Message Write sent?
'drop', a call already in flight 'Dropped' No
mutate() after destroy 'Destroyed' No
reset()/destroy while queued 'NotStarted' No
reset()/destroy while in flight 'Aborted' Maybe

wasNeverSent(error) is true for the first three, so an optimistic update can roll back only the writes that certainly never left, without guessing from an HTTP status.

Signal Forms

Angular 22's Signal Forms submit() expects an async action, and mutate() returns a Promise. The form owns the submission (validity, double submits, field errors); the mutation owns the write (abort on destroy, SSR, state for the rest of the page):

save(): void {
  void submit(this.signUpForm, async (f) => {
    try {
      await this.signUp.mutate(f().value());
      return undefined;
    } catch {
      return { kind: 'server', message: 'Sign-up failed, try again' };
    }
  });
}
Enter fullscreen mode Exit fullscreen mode

The full recipe maps a server error to its field.

Where the project stands

@ngsignal/mutation is at 0.9.2, supports Angular 21 and 22, runs CI, CodeQL and OpenSSF Scorecard, and tests its RxJS interop against RxJS 6 and 7. It is still experimental: pin an exact version. This article was updated for 0.9 (November 2026). The roadmap to 1.0 lists what is left.

Imperative work in. Reactive state out.

Try it

⭐ GitHub

📦 npm

⚡ Live demo, or open it in StackBlitz: every section of this article has its own page.

📖 API reference and recipes

How do you handle double submits today: a saving flag, exhaustMap, or something else? Tell me in the comments; feedback on the API itself (concurrency semantics, cancellation, HTTP progress) is the most useful. Issues and PRs are welcome.

And if Angular eventually ships an official mutation primitive, this project will still have been a worthwhile experiment. 🙂

Top comments (0)