Skip to main content

Reducers & Effects

Reducers are pure functions that take the current state and an action, mutate the state, and return an effect describing any asynchronous work to perform. Effects are the bridge between synchronous state mutations and the async world.

PrismReducer Protocol

Conform to PrismReducer to define your feature’s business logic:
PrismReducer Protocol

Implementation

Feature Reducer

PrismReduce

PrismReduce is a closure-based concrete type that conforms to PrismReducer. Use it for inline reducers or when you don’t need a dedicated type:
Closure-based Reducer

Type Erasure

Convert any typed reducer to PrismReduce with eraseToReduce():

Composing with Middleware

Attach middleware to a reducer with handling(with:). The resulting reducer runs both the original reducer and the middleware, merging their effects:
Reducer + Middleware

PrismEffect

PrismEffect<Action> describes asynchronous side effects that produce zero or more actions over time. The store executes effects and feeds emitted actions back through the reducer.

.none

No side effect. Use when the action only mutates state:

.send

Emit a single action immediately:

.run

Execute asynchronous work. The send closure dispatches actions back to the store:
Async Effect

.sequence

Emit multiple actions in order:

.merge

Run multiple effects concurrently and merge their output:
Merged Effects

.concatenate

Run effects sequentially — each waits for the previous to complete:
Sequential Effects
Use .merge when effects are independent and can run in parallel. Use .concatenate when order matters.

Full Example: Search Feature

Search with Debounce