Table of Contents

Method UseOptimistic

Namespace
Velvet
Assembly
Velvet.Docs.dll

UseOptimistic<TState, TAction>(TState, Func<TState, TAction, TState>)

Returns the optimistic state and an addOptimistic action. With no optimistic update outstanding the returned state is passthroughState. Each addOptimistic(action) records an entry and requests a render, and every render returns passthroughState folded through the outstanding entries by applyOptimistic, in the order they were added — so an entry lands on whatever the authoritative state has become while it is outstanding. An entry added inside a startTransition callback belongs to the innermost transition open there whose isPending is lit, and is discarded when that transition settles (or, with an async action in flight, when none is left), whether or not passthroughState changed and whether the action succeeded or faulted; one transition settling leaves another's entries in place. An entry added where no such transition is open, while an async action is in flight, belongs to the actions in flight together. As in React, the actions in flight are entangled: a transition that settles while any is in flight hands its entries to them, so they are discarded once none is left. An action counts from its start until its task completes, whether or not the component that started it is still mounted, so one awaiting a task that never completes holds every later entry. A component rendered by the Transition-lane drain that lands the last work a transition queued leaves that transition's entries out of that render, and a component holding them that the drain did not render that way is rendered without them before that commit's layout effects run. An entry no render has shown when its owner settles is shown once first. An entry nothing owns is discarded by the component's next Transition-lane render.

public static (TState optimisticState, Action<TAction> addOptimistic) UseOptimistic<TState, TAction>(TState passthroughState, Func<TState, TAction, TState> applyOptimistic)

Parameters

passthroughState TState

The authoritative state. Shown when no optimistic update is outstanding.

applyOptimistic Func<TState, TAction, TState>

Pure reducer (currentState, action) => optimisticState, run during render. Must not be null.

Returns

(TState state, Action<TAction> dispatch)

2-tuple:

  • optimisticState: the pass-through state folded through the outstanding entries.
  • addOptimistic: records an entry owned by the transition whose callback is running, or by the async actions in flight, if either.

Type Parameters

TState

Optimistic state type.

TAction

Action / payload type passed to applyOptimistic.