Skip to content

Add useOptimisticFlow on top of React useOptimistic - #3

Merged
javaquasar merged 1 commit into
mainfrom
claude/use-optimistic-flow
Dec 2, 2026
Merged

javaquasar merged 1 commit into
mainfrom
claude/use-optimistic-flow

Conversation

@javaquasar

Copy link
Copy Markdown
Owner

Summary

In the Next.js setup, Mutaflow kept optimistic state in its own ResourceStore. That made the store a second source of truth next to Server Component data. The store had to be hydrated by hand (StoreHydrator), it never picked up revalidated server data, and every flow needed rollback and reconcile code.

This PR adds useOptimisticFlow, which builds on React 19 useOptimistic and transitions instead:

function Todos({ todos }: { todos: Todo[] }) {          // todos from a Server Component
  const createTodo = useOptimisticFlow(createTodoFlow, todos, { events });
  // createTodo.state = todos + in-flight optimistic items
}
  • The whole run is one async transition. React keeps the optimistic item until the action and the revalidated RSC payload have settled, then drops it. On success, the server data already contains the real item. On failure, the item disappears. There is no store, no rollback, and no reconcile.
  • flow.config.optimistic.apply is reused as the useOptimistic reducer, so existing optimistic.insert/update/remove/replace configs work unchanged.
  • Events, devtools and testkit keep working. runFlow gets a new applyOptimistic option and still emits flow:optimistic-applied and flow:rolled-back.
  • This relies on the action revalidating the data, which flow contracts (withFlowContract, Add flow contracts to apply invalidations on the server #2) provide. This requirement is documented on the hook.
  • useFlow and the resource store stay for client-only state. Their shared state machine moved into an internal useFlowController.

Example app

examples/next-app now uses useOptimisticFlow. StoreHydrator and the resource store are removed, and the todo list is cached with the todos.list tag, which the contract revalidates.

This also uncovered a bug in the example: Next.js loaded lib/data.ts once for the page and once for the server action, so the page never saw new todos. The old store-based UI hid this. The in-memory data now lives on globalThis.

Test plan

  • npm test: 47/47 (3 new tests: runFlow with applyOptimistic, and the hook's success and failure paths in React)
  • npm run typecheck
  • examples/next-app: tsc --noEmit and next build on Next.js 15.5
  • Ran the example in a browser. The list went Read Mutaflow source → After fix (pending) | … → After fix | …, so the optimistic item was replaced by the server item without flicker, and the todo is still there after a reload.
  • Error and cancel paths in the browser (covered by unit tests only)

🤖 Generated with Claude Code

useOptimisticFlow(flow, state) runs a flow inside an async transition
and layers optimistic.apply on top of data from a Server Component via
useOptimistic. React drops the optimistic state when the run ends, so
there is no client store to hydrate and no rollback or reconcile step;
the revalidated server data replaces the optimistic item.

- runFlow: add applyOptimistic so the caller can own optimistic state;
  flow:optimistic-applied and flow:rolled-back are still emitted.
- useFlow: extract the shared state machine into useFlowController.
- examples/next-app: switch to useOptimisticFlow, drop the resource
  store and StoreHydrator, tag the todo list with todos.list, and keep
  in-memory data on globalThis because Next.js loaded the data module
  separately for the page and the server action.
- Add tests and update READMEs, concepts guide and changelog.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@javaquasar
javaquasar merged commit cb23349 into main Dec 2, 2026
3 checks passed
@javaquasar javaquasar mentioned this pull request Dec 2, 2026
3 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant