Remote actions and mutations
Remote actions and mutations give Vue screens a safe way to invoke server-side Frappe operations. The data layer separates reusable calls from writes that may run at the same time, then orders successful document changes so late network responses cannot restore older state. See Data access for the wider data model and Frappe request transport for the request boundary.
Remote operation model
The data layer has two public ways to invoke a Frappe operation. useCall keeps a reactive request state for a URL, while call sends one direct request and returns its result. Higher-level document and list composables use isolated write calls so each submit has its own request and result.
flowchart LR
Screen[Vue screen] --> Call[useCall or call]
Screen --> Action[Document or list action]
Action --> Isolated[Isolated submit]
Call --> Transport[Frappe request transport]
Isolated --> Transport
Transport --> Server[Frappe method or document API]
Transport --> Stores[Shared document and list state]
Realtime update notifications
Realtime notifications provide a separate path for server-side changes that do not come from the current action. A doctype subscription reports the changed document name to the owning resource, which can then keep the related view or store current. See src/…/realtime.ts• L7-18.
Subscriptions belong to a socket. The resource sends each doctype again after reconnect because the server drops socket rooms on disconnect, while repeated registrations on one socket do not create duplicate subscriptions. Notifications for other doctypes are ignored. See src/…/realtime.ts• L20-41 and src/…/realtime.test.ts• L53-103.
References: src/…/realtime.ts• L7-18 src/…/realtime.ts• L20-41 src/…/realtime.test.ts• L53-103
Call composable
useCall represents one remote operation as reactive state. It can call a whitelisted Frappe method or a REST path with GET, POST, PUT, or DELETE, and it exposes the response as data with loading, error, and completion state beside it. See src/…/useCall.ts• L35-53 and src/…/types.ts• L6-23.
The call can run on setup or wait for an explicit execute() or submit(). Use execute() when the configured parameters are the operation, and use submit(params) when each user action supplies new parameters. A throwing beforeSubmit stops the request before it reaches the server. See src/…/useCall.ts• L188-216.
References: src/…/useCall.ts• L35-53 src/…/useCall.ts• L188-216 src/…/types.ts• L6-23
Imperative call
The call helper is for one direct server operation when a component does not need a long-lived reactive request object. It sends a POST body to a dotted Frappe method path and returns the response message; an absolute API path can bypass the dotted-path conversion. See src/…/call.ts• L19-34 and Utilities.
The helper uses the same Frappe transport as the composables. It therefore shares request base URL, headers, credentials, CSRF handling, server messages, and error conversion. Its onError callback reports an HTTP-level failure, but the returned promise still rejects. See src/…/call.ts• L25-50 and src/…/frappeRequest.ts• L79-137.
References: src/…/call.ts• L19-50 src/…/frappeRequest.ts• L79-137
Mutation lifecycle
Mutation state describes a write from the moment a caller starts it until its own request settles. The lifecycle keeps request outcome, UI status, consumer hooks, and shared record state distinct so a failure or a race in one submit does not corrupt another submit.
Submit stages
When a screen submits a mutation, the action validates the input, starts a request for that input, and tracks the request as pending. On success it exposes the response and may update shared stores; on failure it exposes the error and rejects that submit. A submit can still resolve to null when its underlying isolated call reports a failed request, so higher-level insert flows add their own failure check. See src/…/useAction.ts• L135-224 and src/…/useNewDoc.ts• L53-75.
The lifecycle has aggregate and per-target status. loading stays true while any submit is in flight, while isLoading(target) identifies work for one document or method. This lets a list disable or show progress for one row without blocking unrelated rows. See src/…/useAction.ts• L46-49 and src/…/useDoctype.ts• L54-77.
References: src/…/useAction.ts• L135-224 src/…/useNewDoc.ts• L53-75 src/…/useDoctype.ts• L54-77
Isolated submits
An isolated call creates a private request state for every submit(). This is required when one action can run more than once before an earlier request settles; sharing one useCall would allow the next request to abort or replace the first request's result. See src/…/useIsolatedCall.ts• L20-39.
The isolated call is internal. It supports the full call-shaped state needed by useDoc, useNewDoc, and document methods, while public screens reach it through those composables. It starts only when submitted by default, and each caller receives its own response even when another response becomes the current shared result. See src/…/useIsolatedCall.ts• L50-64 and src/…/useDoc.ts• L114-170.
References: src/…/useIsolatedCall.ts• L20-64 src/…/useDoc.ts• L114-170 src/…/useNewDoc.ts• L26-50
Action concurrency
useAction is the common write model for operations that can target different documents or methods at the same time. It is internal and is exposed through actions such as useDoctype().setValue, useDoctype().delete, and list insert or update helpers. See src/…/useAction.ts• L37-83 and src/…/useDoctype.ts• L10-26.
Its reactive data and error describe the newest started submit. A late result still resolves its own promise, but it cannot replace newer action state. Consumer hooks use the same target rule so an older success does not trigger side effects after a newer success for that target. A newer failure does not block an older success because the failed request changed nothing on the server. See src/…/useAction.ts• L50-73 and src/…/useAction.ts• L201-217.
References: src/…/useAction.ts• L37-83 src/…/useAction.ts• L50-73 src/…/useAction.ts• L201-224 src/…/useDoctype.ts• L10-26
Write safety
Mutation safety has two related rules. The action instance protects the state and hooks that belong to that instance, while the shared stores protect the document data that may be written by many composables or many request paths.
Dispatch order
Each request receives a sequence when it is sent, not when it finishes. A mutating request records its sequence for the documents it changes; a read can be admitted but does not become a newer write. The gate compares sequences per document, so unrelated records do not block each other. See src/…/useFrappeFetch.ts• L60-75 and src/…/writeGate.ts• L1-15.
This rule follows user intent rather than network timing. If an older save finishes after a newer save, the stores reject the older response. If a newer request fails, it records no successful write, so an older successful response can still become the stored value. See src/…/writeGate.ts• L30-45 and src/…/writeGate.ts• L77-89.
References: src/…/useFrappeFetch.ts• L60-75 src/…/writeGate.ts• L1-15 src/…/writeGate.ts• L30-45 src/…/writeGate.ts• L77-89
Store write boundary
Successful action responses reach shared document and list state through a private store-write path that carries the request sequence. The consumer-facing success hook cannot write those stores directly, which keeps every document update inside the same freshness rule. See src/…/useCall.ts• L13-32 and src/…/useAction.ts• L75-79.
The same response can update a document and its matching list row. Both stores use the same document identity and gate decision, so a document view and a list view do not disagree because their requests settled in different orders. See Document and list state for the shared state model and src/…/useFrappeFetch.ts• L92-103.
References: src/…/useCall.ts• L13-32 src/…/useCall.ts• L108-135 src/…/useAction.ts• L75-79 src/…/useFrappeFetch.ts• L92-103
Delete sealing
A successful delete removes the document from shared state and seals its identity against requests still in flight. This prevents an older save or read from recreating a record that the server has already deleted. A later request starts after the seal and can be admitted normally. See src/…/writeGate.ts• L92-108 and src/…/useDoctype.ts• L54-68.
This behavior differs from local invalidation. Invalidation means the local value is not usable while the server document still exists, so an in-flight save may repopulate it. Delete sealing means the server document is gone. See src/…/docStore.ts• L197-243 and Document and list state.
References: src/…/writeGate.ts• L92-108 src/…/useDoctype.ts• L54-68 src/…/docStore.ts• L197-243
Operation surfaces
Higher-level composables select the mutation surface that matches the target. This keeps URL and payload rules with the operation while giving screens a consistent submit and status model.
Document operations
useDoc owns the read view for one document and exposes isolated writes for field updates, deletion, and configured document methods. These writes update the document store and matching list rows when the server returns document data. See src/…/useDoc.ts• L144-170.
DocType operations
useDoctype groups writes that do not need one document as their read owner. Its operations cover insert, delete, field updates, methods on one document, and methods on the DocType. Each operation exposes its own result and loading state, with target-specific loading for row and method actions. See src/…/useDoctype.ts• L29-167.
New document operations
useNewDoc holds draft values until the caller submits an insert. The server response becomes the caller's created document, while the stamped store write makes the new record available to shared document state. The caller receives its own created document even if another concurrent insert wins the shared identity gate. See src/…/useNewDoc.ts• L15-50 and src/…/useNewDoc.ts• L57-75.
References: src/…/useDoc.ts• L144-170 src/…/useDoctype.ts• L29-167 src/…/useNewDoc.ts• L15-50 src/…/useNewDoc.ts• L57-75
Change checklist
When these files or sections are changed, remember to consider these:
- If a new write path can return document data, connect it to the stamped store-write boundary so document and list state use the same freshness decision.
- If an action target changes, review both
isLoading(target)and the hook gate. A target that is too broad can hide valid work; one that is too narrow can allow stale side effects. - If a mutation response can contain a new document name, preserve the difference between the caller's own response and the shared store winner.
- If request methods or response error shapes change, review
useCall,useAction, and the Frappe error handling together. A failed submit must not look like a successful write. - If delete or invalidation behavior changes, keep server deletion separate from local cache eviction so in-flight writes are not blocked or revived incorrectly.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
One shared useCall handles overlapping submits | The next request can abort or replace the earlier result | Use isolated submits for document, list, and insert mutations |
| An older response settles after a newer document write | Older data can replace the user's newer intent | Stamp requests at dispatch and gate shared writes per document |
| A read records itself as a write | A read that finishes early can reject a valid save response | Admit reads without recording a write sequence |
| A newer same-target request fails after an older request succeeds | The failed request can hide the valid older server value | Record freshness only for successful writes |
| A stale consumer hook refetches or reports its result | UI side effects can describe data that no longer won | Gate onSuccess and onError by the target's newest successful submit |
| A delete only removes the current store value | An in-flight save or read can recreate the deleted document | Seal the document identity when delete succeeds |
| A known Frappe error is treated as a cache or network fallback | The UI can show data that the server explicitly rejected | Inspect error and distinguish FrappeResponseError from transport failure |
Key files
| File | Why to read it |
|---|---|
| src/…/useCall.ts | Defines the public reactive call lifecycle and the private boundary for stamped store writes. |
| src/…/types.ts | Defines the options that shape URLs, methods, parameters, timing, and callbacks. |
| src/…/useIsolatedCall.ts | Explains why each overlapping submit gets a private call and how its state is aggregated. |
| src/…/useAction.ts | Defines concurrent mutation state, target loading, validation, and newest-submit behavior. |
| src/…/writeGate.ts | Defines dispatch order, per-document freshness, local writes, and delete sealing. |
| src/…/useFrappeFetch.ts | Connects request dispatch and Frappe responses to sequence stamps and shared stores. |
| src/…/useDoc.ts | Shows document-level update, delete, and method operations. |
| src/…/useDoctype.ts | Shows the main DocType-wide mutation surfaces and target-specific loading. |
| src/…/useNewDoc.ts | Shows draft insertion and the distinction between caller results and shared state. |
| src/…/call.ts | Defines the direct imperative call helper for one-off server methods. |
| src/…/frappeRequest.ts | Defines shared Frappe request shaping, response unwrapping, and error reporting. |
| src/…/realtime.ts | Defines doctype subscriptions and document-change notifications received outside a request. |
| src/…/realtime.test.ts | Verifies one-socket subscription reuse, reconnect behavior, socket isolation, and doctype filtering. |