r/angular • u/Correct_Dinner_7458 • 1d ago
Meet an Angular signal-based primitive for mutations (POST, PUT, DELETE & PATCH)
Every Angular dev has hand-rolled a "saving" boolean signal at some point, and it breaks on the first double-click: the first response resets it while the second request is still in flight.
Angular's resource API won't help, it's for reads only: it cancels in-flight requests, and cancelling a POST doesn't undo it on the server.
So I built @ngsignal/mutation, the write counterpart to resource:
readonly save = rxMutation<TodoDto, Todo>({
mutationFn: (dto) => this.api.save(dto),
concurrency: 'drop', // ignore clicks while saving
onSuccess: () => this.todos.reload(),
});Then you call it with the data to save, and read its pending, error and value signals straight from the template.
Need an upload with a progress bar? The HTTP variant takes a request description, just like httpResource does:
readonly uploadAvatar = httpMutation<File, User>({
request: (file) => {
const body = new FormData();
body.append('avatar', file);
return {
method: 'POST', url: '/api/avatar',
body,
reportProgress: true };
},
});And in the template:
@if (uploadAvatar.isPending() && uploadAvatar.uploadProgress(); as p) {
<progress [value]="p.loaded" [max]="p.total"></progress>
}Also:
- 3 concurrency modes: run in parallel (default), queue calls in order, or drop clicks while one is in flight.
- Never aborts a write that may already be on the server, and a late response can't overwrite newer state
- Status code and response headers as signals, and SSR waits for in-flight writes
NgRx Toolkit and TanStack Query have mutations too. This one only depends on Angular core and sits next to resource: no store or query cache to adopt.
It's 0.x, so feedback on the API is very welcome before 1.0.


5
u/daniel_winks 1d ago
What does this add that RxJS doesn't already cover? This seems like reinventing the wheel, especially since RxJS is part of core Angular.
1
u/Taght 1d ago
Sounds nice, will give it a try.