MCPcopy Index your code
hub / github.com/TanStack/db / createOptimisticAction

Function createOptimisticAction

packages/db/src/optimistic-action.ts:54–81  ·  view source on GitHub ↗
(
  options: CreateOptimisticActionsOptions<TVariables>,
)

Source from the content-addressed store, hash-verified

52 * @returns A function that accepts variables of type TVariables and returns a Transaction
53 */
54export function createOptimisticAction<TVariables = unknown>(
55 options: CreateOptimisticActionsOptions<TVariables>,
56) {
57 const { mutationFn, onMutate, ...config } = options
58
59 return (variables: TVariables): Transaction => {
60 // Create transaction with the original config
61 const transaction = createTransaction({
62 ...config,
63 // Wire the mutationFn to use the provided variables
64 mutationFn: async (params) => {
65 return await mutationFn(variables, params)
66 },
67 })
68
69 // Execute the transaction. The mutationFn is called once mutate()
70 // is finished.
71 transaction.mutate(() => {
72 const maybePromise = onMutate(variables) as unknown
73
74 if (isPromiseLike(maybePromise)) {
75 throw new OnMutateMustBeSynchronousError()
76 }
77 })
78
79 return transaction
80 }
81}

Callers 6

createOfflineActionMethod · 0.90
scheduler.test.tsFile · 0.85

Calls 4

createTransactionFunction · 0.90
isPromiseLikeFunction · 0.90
mutationFnFunction · 0.50
mutateMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…