Introducing a new way to review your PRs.

Read the announcement

frappe / frappe-ui

The all-in-one toolkit for modern frontends. High-quality components, app recipes and AI-ready docs.

View on GitHub

28 documents · Updated Nov 23, 2026

docs / document-and-list-state

Document and list state

Document and list state gives Vue screens a shared, reactive view of Frappe records. A DocType is the server-defined type of a record; a document is one record of that type; and a list is a query result for a DocType. The composables fetch these records, expose write actions, keep related views aligned, and use local data while fresh server data loads.

The page Data access gives the wider map of the data layer. This page focuses on the state shared by useDoc, useList, and useDoctype.

State model

The composables separate record reads from writes, but they feed the same document and list state. A read or successful write can update the shared stores, so a document view and a list row do not need separate refresh logic to agree.

flowchart LR
  A[useDoc] --> T[Request transport]
  B[useList] --> T
  C[useDoctype actions] --> T
  T --> D[Document store]
  T --> L[List store]
  D --> V[Reactive document views]
  L --> V2[Reactive list views]
  D <--> I[IndexedDB cache]
  L <--> I

Resource composables

The composables are the main entry points for screens that need document state or DocType actions. They return reactive values and action state instead of making each screen manage request results and cross-view updates on its own.

Document state

useDoc represents one document selected by a doctype and a document name. It reads the document, exposes its loading and error state, and provides actions for field updates, deletion, and configured document methods; its document value is resolved from the shared document store. See src/…/useDoc.ts• L36-59 and src/…/useDoc.ts• L172-224.

The document may be selected by a reactive name. This lets a view first wait for an identifier and then follow the matching shared state without creating a second document model. A transform changes the value exposed to the view, while the shared store keeps the server-shaped document so the transform does not build up across cache and network reads. See src/…/useDoc.ts• L61-107 and src/…/docStore.ts• L91-100.

References: src/…/useDoc.ts• L36-59 src/…/useDoc.ts• L172-224 src/…/docStore.ts• L91-100

List state

useList represents a query over one DocType. Its reactive state includes the current rows, query position, page limits, fetch status, and page-direction state; filters and sort values can be reactive, so a change can drive a new query. See src/…/useList.ts• L23-66 and src/…/useList.ts• L250-278.

The list keeps the rows fetched so far in one view of the result. The next and previous actions move the query window, while updateRow and removeRow allow shared document changes to appear without a new list request. Write helpers create, update, or delete rows and can refetch the current page after success. See src/…/useList.ts• L136-183 and src/…/useList.ts• L198-248.

References: src/…/useList.ts• L23-66 src/…/useList.ts• L136-183 src/…/useList.ts• L198-278

DocType actions

useDoctype groups actions that apply to a DocType without reading or holding a document. It is the common write surface for insert, delete, field updates, a method on one document, and a method on the DocType itself. Use it with useDoc or useList when a screen needs a read model and a separate write model. See src/…/useDoctype.ts• L6-26 and src/…/useDoctype.ts• L29-167.

Each action has its own result, error, and loading state. Actions for different targets can run at the same time, and a screen can ask whether one document or one method is in flight. This prevents one row's action from cancelling or hiding another row's action. See src/…/useAction.ts• L37-73 and src/…/useDoctype.ts• L54-136.

References: src/…/useDoctype.ts• L6-26 src/…/useDoctype.ts• L29-167 src/…/useAction.ts• L37-73

Shared identity and synchronization

The stores use one identity rule for every document: the trimmed DocType and document name form a doctype/name key. Lists are grouped by DocType and find a row by its name. This common identity is what lets a response update both a document view and every matching list row. See src/…/writeGate.ts• L117-124 and src/…/listStore.ts• L13-23.

Document identity

The document store keeps one reactive reference for each document key. All useDoc instances that resolve to the same DocType and name read that reference, so they observe the same server result and later store updates. An invalid name cannot create an ambiguous store entry. See src/…/docStore.ts• L11-34 and src/…/docStore.ts• L96-115.

References: src/…/docStore.ts• L11-34 src/…/docStore.ts• L96-115 src/…/writeGate.ts• L117-124

List membership

The list store keeps the active list views for each DocType. When a document response has a name, the store patches matching rows in those lists; a successful delete removes the rows. A document update therefore reaches list views even when the write began from a document view or a DocType action. See src/…/listStore.ts• L20-67 and src/…/useList.ts• L276-278.

The same response can carry document data through the request side channel. The transport sends that data to both stores with the same write stamp, which keeps the document and row decisions aligned. See Frappe request transport for the request boundary. See src/…/useFrappeFetch.ts• L81-103 and src/…/useDoc.ts• L93-105.

References: src/…/listStore.ts• L20-67 src/…/useList.ts• L276-278 src/…/useFrappeFetch.ts• L81-103 src/…/useDoc.ts• L93-105

Synchronization flow

The following flow shows how one document change becomes shared state instead of a local result owned by one component.

sequenceDiagram
  participant Screen as Document or list screen
  participant Request as Request transport
  participant Docs as Document store
  participant Lists as List store
  participant Views as Reactive views

  Screen->>Request: Read or mutate a document
  Request-->>Docs: Document response and write stamp
  Request-->>Lists: Row response and same write stamp
  Docs-->>Views: Publish document state
  Lists-->>Views: Patch or remove matching rows

This sharing applies to updates and deletes. Inserts normally become visible through the list's follow-up fetch, because the new document has no existing identity until the server returns its name. See src/…/useList.ts• L176-183 and src/…/useDoctype.ts• L29-51.

References: src/…/useDoc.ts• L144-169 src/…/useList.ts• L176-183 src/…/useDoctype.ts• L29-68

Cache and persistence

Document and list state has two layers: live reactive state in memory and serialized state in IndexedDB. The cache gives a screen a usable prior value while the request checks the server, but the server response remains the source used to refresh the live state.

Cache identity

Document cache identity comes from the DocType and name. List cache identity comes from the caller's cacheKey, normalized with a useList prefix so unrelated resource kinds do not share a slot by accident. A list cache key must describe the query's intended result; changing filters or sort without changing the key can show the old query while the new request runs. See src/…/docStore.ts• L96-115, src/…/useList.ts• L79-82, and src/…/utils.ts• L62-75.

The list cache stores the rows accumulated for the current list view. It does not replace the list's query state, page flags, or request status. Those values still come from the active composable and its current request. See src/…/useList.ts• L68-82 and src/…/useList.ts• L326-337.

References: src/…/docStore.ts• L96-115 src/…/useList.ts• L68-82 src/…/useList.ts• L326-337 src/…/utils.ts• L62-75

IndexedDB persistence

The persistence layer uses IndexedDB in a browser and a memory fallback where IndexedDB is not available. It stores JSON values, reads them asynchronously, and isolates storage failures from the in-memory store as far as possible. See src/…/idbStore.ts• L3-24 and src/…/idbStore.ts• L45-83.

The document store writes a successful document before it persists the same value. It reads a cached document on first use and reloads a stale document after the cache timeout. A list reads its cached rows at setup and can show them while a request is loading. See src/…/docStore.ts• L36-55, src/…/docStore.ts• L150-179, and src/…/useList.ts• L128-134.

References: src/…/idbStore.ts• L3-24 src/…/idbStore.ts• L45-83 src/…/docStore.ts• L36-55 src/…/docStore.ts• L150-179 src/…/useList.ts• L128-134

Stale fallback

staleOnError controls whether a cached value remains visible after a failed refresh. When enabled, a network or client failure can leave the last cached value on screen; a FrappeResponseError still clears that fallback because the server returned a known application error. See src/…/useList.ts• L110-126 and src/…/useCall.ts• L278-280.

This option changes display behavior, not write order. A cached value can be useful for offline-capable screens, but the screen must still inspect error to know that the current refresh failed. See Remote actions and mutations for the related action error model. See src/…/useDoc.ts• L177-195 and src/…/useList.ts• L281-283.

References: src/…/useList.ts• L110-126 src/…/useList.ts• L281-283 src/…/useCall.ts• L222-242 src/…/useCall.ts• L278-280

Write ordering

Concurrent requests can finish in a different order from the order in which the user made the changes. The shared stores use dispatch order to keep a late response from putting an older document back over a newer one.

Dispatch stamps

Each request receives a monotonically increasing stamp when it is sent. A mutating request records its stamp for each document that it changes; a read may be admitted but does not record a newer write order. This keeps a fast read from blocking a save that was already dispatched. See src/…/writeGate.ts• L1-25 and src/…/useFrappeFetch.ts• L60-75.

The rule is per document, not global. Two unrelated documents can update at the same time, while a document update and its matching list-row update share the same decision. See src/…/writeGate.ts• L78-89 and src/…/docStore.ts• L181-195.

References: src/…/writeGate.ts• L1-25 src/…/writeGate.ts• L68-89 src/…/useFrappeFetch.ts• L60-75 src/…/docStore.ts• L181-195

Stale-write protection

When a response is older than a newer accepted write for the same document, the stores drop it. They do not publish it to reactive views or persist it to IndexedDB. This protects both the current screen and the value that a later screen would read from cache. See src/…/docStore.ts• L64-89 and src/…/listStore.ts• L40-55.

A successful delete seals the document key. In-flight updates for that document can then no longer recreate it, while an explicit invalidation does not seal the key because the server document still exists. See src/…/writeGate.ts• L91-108 and src/…/docStore.ts• L197-243.

The action state has a separate newest-submit rule. Its data and error describe the most recently started submit, while each caller still receives the result of its own submit. Store freshness remains a shared per-document decision across composable instances. See src/…/useAction.ts• L46-79 and src/…/useAction.ts• L201-220.

References: src/…/docStore.ts• L64-89 src/…/listStore.ts• L35-55 src/…/writeGate.ts• L91-108 src/…/docStore.ts• L197-243 src/…/useAction.ts• L46-79 src/…/useAction.ts• L201-220

Change checklist

When these files or sections are changed, remember to consider these:

  • If document identity changes, update the key rule in the document store, list-row matching, write gate, and IndexedDB names together.
  • If a new request path writes document data, pass its dispatch stamp to both stores so document views and list rows keep one freshness decision.
  • If a list query's meaning changes, review its cacheKey; a reused key can serve rows for a different filter or sort.
  • If delete or invalidation behavior changes, preserve the difference between removing a server document and discarding a bad local copy.
  • If the response shape or error type changes, review cached fallback behavior and the FrappeResponseError boundary together.

Important failure modes

TriggerConsequenceGuard
Two writes to one document settle out of dispatch orderAn older response can overwrite the newer value in the document and list viewsThe shared write gate admits writes by per-document dispatch stamp
A stale response is persisted before it is rejectedA later screen can read the old value from IndexedDB and revive stale stateStore writes reject stale responses before publish or persistence
A delete result races with an in-flight updateThe update can recreate a document that the server has deletedA successful delete seals the document key
One cache key is reused for different list queriesA screen shows rows from the wrong filter or sort while fetchingInclude the query meaning in cacheKey and use the normalized list key
A known Frappe error is treated as a temporary network failureThe screen keeps showing data that the server explicitly rejectedDo not use staleOnError as a fallback for FrappeResponseError

Key files

FileWhy to read it
src/…/useDoc.tsShows the document read model and how document actions write shared state.
src/…/useList.tsShows list queries, pagination, cache use, row updates, and list write actions.
src/…/useDoctype.tsShows the DocType-wide write surface that does not own a read model.
src/…/docStore.tsDefines document identity, reactive document state, persistence, freshness, and deletion cleanup.
src/…/listStore.tsDefines how all active lists for a DocType receive row updates and removals.
src/…/writeGate.tsDefines dispatch order and the rule that rejects stale document writes.
src/…/idbStore.tsDefines the browser persistence boundary and its memory fallback.
src/…/useFrappeFetch.tsConnects request responses to shared stores and carries dispatch stamps.
src/…/useAction.tsExplains independent concurrent submits and the action-level newest-submit state.