Concurrent calls

A second mutate() does not abort the first: a write may already have reached the server, so both run to completion. Every call runs its callbacks, with { isLatest }; only the latest call writes status, value and error. The concurrency option changes what a second call does: see the modes below. Source

Two calls at once

  • Ada Lovelace
  • Grace Hopper

Delete Ada, then quickly Grace (always fails); then submit two names quickly. Each call takes 2 s (an artificial delay).

Latest call only

createUser.status
idle
createUser.input
—
deleteUser.status
idle
deleteUser.error

Callbacks

  1. Nothing yet: delete two users quickly.

Server

  1. Nothing yet.
protected readonly deleteUser = mutation<User, void, Error>({
  mutationFn: (user, abortSignal) => this.server.deleteUser(user, abortSignal),
  onSuccess: (_result, user, { isLatest }) => {
    this.users.update((current) => current.filter((u) => u.id !== user.id));
    this.log.add(`deleteUser("${user.name}") succeeded, isLatest: ${isLatest}`);
  },
  onError: (error, user, { isLatest }) =>
    this.log.add(`deleteUser("${user.name}") failed: ${error.message}, isLatest: ${isLatest}`),
});
@let deleting = deleteUser.pendingInputs().includes(user);
<li>
  {{ user.name }}
  <button type="button" class="danger" (click)="deleteUser.mutate(user)" [disabled]="deleting">
    {{ deleting ? 'Deleting…' : 'Delete' }}
  </button>
</li>

The concurrency option

What a second mutate() does while a call is in flight:

'parallel'
Runs at once (the default, above).
'queue'
Waits for the previous call: move an item to the top.
'queue-latest'
Waits, replacing the call already waiting: autosave.
'drop'
Rejected at once, never sent: pay.
mutationQueue()
Waits for the calls of every mutation sharing the queue: rename, then delete.
{ queue, key }
Waits for the calls with the same key: save per document.