optimistic
Describes an optimistic change of a cached request. Return the operations from
setOptimistic
or pass them with the
optimistic
send option, core applies them, runs
.commit()
/
.rollback()
when the mutation settles and refreshes
the targets once the last pending mutation on them is done.
optimistic(getTodos)
- the entry of this request (its params and query params)
optimistic.all(getTodos)
- every cached variant of it, whatever the query params (filters, infinite pages)
Helpers:
set(updater)
,
patch(match, fields)
,
insert(item, { at })
,
remove(match)
,
increment(field, by)
/
increment(match, field, by)
and
.list("field")
for an array inside an object.
Import
import { optimistic } from "@hyper-fetch/core"
Source
Package
Usage
optimistic(getTodos)
.patch(params.todoId, payload)
.rollback(({ data, previous }) => {
const before = previous.find((todo) => todo.id === params.todoId);
return data.map((todo) => (todo.id === params.todoId && before ? before : todo));
});
Parameters
optimistic<Target>(target: OptimisticTargetRequestType<Target>)
Parameters
| Name | Type | Description |
|---|---|---|
| target | |
Returns
type OptimisticTargetType<Data,Mutation> = { set: (updater: (data: Data) => Data) => OptimisticOperation<Data, Mutation> } & (0 extends (1 & Data) ? (OptimisticListTargetType<Data, any, Mutation> & OptimisticEntryTargetType<Data, Mutation>) : ([Data] extends [readonly infer Item[]] ? OptimisticListTargetType<Data, Item, Mutation> : OptimisticEntryTargetType<Data, Mutation>));
optimistic
| Name | Type | Description |
|---|---|---|
| set | | Replaces the data with the updater's result |

