Vue composable patterns

From Esposter/Esposter · View interactive version

Vue composable patterns is an AI agent skill from the GitHub repository Esposter/Esposter. Apply when writing or reviewing a composable, a form dialog, browser-aware reactive code, or any state that spans an await, a tick or a mount. Esposter's Vue 3 composable and form-dialog patterns, built on the primitives that already own async sequencing, persistence and teardown.

How to use this skill

Skill file

Vue Composable & Form Patterns

Settled — do not re-propose

Reach for the primitive — never hand-rolled

Most of what a composable is tempted to write by hand already exists here, and the hand-rolled copy is not merely

duplicated — it is the copy that drifts, forgets its teardown, or silently loses a write. **Before writing state

that spans an await, a tick or a mount, find the row.**

| Wanting to… | Use | Never |

| :----------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------- |

| order overlapping reads or writes | useQuery, or executeQuery with the caller's run scope; a server row's write is writeOptimistic on the overlay store, with no key | a promise chain, an in-flight promise map, a generation counter, a call id, an isSaving flag |

| let a pushed value beat a read already in flight | the overlay's arrive (app/store/overlay.ts): a row lands only at a higher version, or fills a held row's missing columns at the same one | a pair of counters beside a ref |

| skip a save when nothing changed | useSave ({ save, setState }) | a hand-rolled snapshot, or a set* wrapper in a store |

| know a save is still coming | the mutation's own isPending | a counter of armed debounces, or an isPending you assign yourself |

| run something on an interval for a component's life | useWorkerInterval | setInterval in onMounted + clearInterval in onUnmounted |

| pan and zoom a surface | usePanZoom | scale/offset refs and pointer handlers |

| read or insert into a Map | getOrCreate (@esposter/shared) | let x = map.get(k); if (!x) … |

| let a restore reach an editor that holds the document | useAdoptResourceContent (Tiptap, the survey Creator) | a :key remount, or trusting the store's ref to reach a library that parsed it once |

| load, re-read on restore and save a resource's content | createContentData — the base of every content store, read once per open resource | readResource + readContent hand-rolled in a store, or a blade's own isLoading and skeleton |

| write a resource's content after an await | createContentData's getContentWriter(), taken where the operation is issued | content.value = … in the callback that lands, filed under whichever resource is open by then |

A counter is the tell. Every row of the table under "Reach for the primitive — never hand-rolled" was written by hand somewhere first, and each time the shape was the

same: the problem looked complex enough that bookkeeping felt earned. It is the opposite signal. A count of

in-flight or armed _anything_ is the moment to stop and name the primitive that owns it — and where a flag really

is the answer, ask what one write actually cleans before reaching for a number. Counting two of something a

single operation resolves is a bug wearing rigour.

Deep dives

Composable Rules

MaybeRefOrGetter vs Function Argument

MaybeRefOrGetter<T> only for a value the composable watches; a value read at call time is a plain argument on the returned function (references/composable-arguments.md).

Validation Rules — Pick the Right Layer

A validation rule lives in one of three layers, chosen by what it depends on: a builder in the library's UiRules, a shared composable,

or a refinement on the form schema when the form is a schema form. Extract on the 2nd copy — never duplicate an inline

rule across dialogs. Where a schema form's refinement is built: references/form-dialogs.md.

Extract Duplicate Mutation Blocks — Builder Arg for Discriminated-Union Inputs

A mutation block repeated across siblings becomes one composable, and a discriminated-union input is a builder (field) => Input (references/mutation-blocks.md).

Unwrapping Reactive Proxies

toRawDeep, never Vue's one-level toRaw, and only for what came out of reactive state (references/reactive-proxies.md).

Resource Management

Anything that outlives a tick — an interval, a listener, an observer — is torn down at unmount and never on a

flag flipping, and the primitives under "Reach for the primitive — never hand-rolled" own the common cases. The rules and the reasoning:

references/resource-cleanup.md.

Observing the browser — `references/browser-observation.md`

Scroll, connectivity and every other browser-only reading has one right shape here, and measuring where the platform will observe is the recurring mistake. A composable reading scroll position or online state, or one that must not run during SSR, is that page.

Least API Calls — Dirty-Check Saves

Every API call must be necessary. Never fire a persistence call — tRPC mutation or localStorage write — when the payload equals what was last persisted. The check belongs to useSave, and a load goes through its setState so the snapshot resets; the silent offenders and the snapshot semantics are references/async-sequencing.md.

Async Sequencing — One Primitive

A composable never decides how concurrency is handled. A read holds the run scope it is ordered by, and a write to a server row names its entry, never a key: the primitive derives the rest, a queue for the writes whose rows overlap and an abort for the run a newer run of its key replaces. A composable that seems to need its own ordering is reaching for a primitive that already owns it, and a useMutation key is the residual form for writes the overlay does not hold yet.

A store is a call site too. A store that both reads a value and receives it pushed — a typed event, a broadcast, any handler writing the same ref the read writes — is ordering two async sources against each other, which is the primitive's job however few lines it takes to fake.

The entry points, the isExclusive opt-in, the pending flag, getSynchronizedFunction pairing and the abort signal a mid-flight check reads: references/async-sequencing.md. Full model: openspec/changes/archive/2026-11-22-architecture-optimistic-overlay/design.md, until that change is archived.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.