Repository navigation
Add useOptimisticFlow on top of React useOptimistic - #3
Merged
Merged
Conversation
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>
3 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 neededrollbackandreconcilecode.This PR adds
useOptimisticFlow, which builds on React 19useOptimisticand transitions instead:rollback, and noreconcile.flow.config.optimistic.applyis reused as theuseOptimisticreducer, so existingoptimistic.insert/update/remove/replaceconfigs work unchanged.runFlowgets a newapplyOptimisticoption and still emitsflow:optimistic-appliedandflow:rolled-back.withFlowContract, Add flow contracts to apply invalidations on the server #2) provide. This requirement is documented on the hook.useFlowand the resource store stay for client-only state. Their shared state machine moved into an internaluseFlowController.Example app
examples/next-appnow usesuseOptimisticFlow.StoreHydratorand the resource store are removed, and the todo list is cached with thetodos.listtag, which the contract revalidates.This also uncovered a bug in the example: Next.js loaded
lib/data.tsonce 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 onglobalThis.Test plan
npm test: 47/47 (3 new tests:runFlowwithapplyOptimistic, and the hook's success and failure paths in React)npm run typecheckexamples/next-app:tsc --noEmitandnext buildon Next.js 15.5Read 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.🤖 Generated with Claude Code