Skip to main content
Version: v9.0.0

OptimisticOperation

One change of one target, built with optimistic(target) . Core applies it before the mutation is sent, and runs its

.commit() on success or its .rollback() when the mutation fails, is aborted or removed from the queue. Operations are immutable - .rollback() and .commit() return a new operation, so one can be reused.


Import
import { OptimisticOperation } from "@hyper-fetch/core"

Parameters​

class OptimisticOperation<Data, Mutation> {
constructor(options: OptimisticOperationOptions<Data>) {};
commit(callback: OptimisticOperationCommitType<Data, Mutation>) => OptimisticOperation<Data, Mutation>;
rollback(callback: OptimisticOperationRollbackType<Data, Mutation>) => OptimisticOperation<Data, Mutation>;
}
Parameters
NameTypeDescription
options
OptimisticOperationOptions<Data>

Methods​

rollback()​

Undo of this change, written by hand and run by core when the mutation fails for good, is aborted or removed from the queue. Rollbacks of one mutation run in reverse order of the changes. It receives

{ data, previous, reason, response, request } and returns the new data; returning nothing leaves the data as it is. Build the result from data and use previous only to look old values up. Must be synchronous. Without it nothing is undone locally and the target is refetched.

Preview
rollback(callback: OptimisticOperationRollbackType<Data, Mutation>)
Parameters
Parameters
NameTypeDescription
callback
OptimisticOperationRollbackType<Data, Mutation>
Returns
OptimisticOperation<Data, Mutation>

commit()​

Runs when the mutation succeeds, before the target is refreshed. It receives { data, response, request } and returns the new data, for example to swap a temporary item for the one from the response. Returning nothing leaves the data as it is. Must be synchronous.

Preview
commit(callback: OptimisticOperationCommitType<Data, Mutation>)
Parameters
Parameters
NameTypeDescription
callback
OptimisticOperationCommitType<Data, Mutation>
Returns
OptimisticOperation<Data, Mutation>