Skip to main content

PrismStore

PrismStore is the central piece of PrismArchitecture. It owns your application state, processes actions through a reducer, executes asynchronous effects, and supports scoping to derive child stores for sub-features.
Minimal Counter

Creating a Store

There are three ways to create a store:

Closure-based

Pass a closure that mutates state and returns an effect:
Closure Init

Typed Reducer

Pass a type conforming to PrismReducer:
Typed Reducer Init

With Middleware

Attach middleware for cross-cutting concerns like logging:
Store with Middleware

Sending Actions

Dispatch actions synchronously with send(_:) or asynchronously with dispatch(action:):
Dispatching Actions

Observing State in SwiftUI

PrismStore is @Observable, so SwiftUI views re-render automatically when state changes:
SwiftUI View

Scoping

Derive child stores that focus on a subset of state and actions. The child stays synchronized with the parent — actions sent to the child are transformed and forwarded upstream.
Scoping a Store
Scoped stores are lightweight. They don’t duplicate state — they project a view of the parent’s state and forward actions back. Use them freely to isolate sub-features.

Cancelling Effects

Cancel all in-flight asynchronous effects:
Cancel Effects
This is useful when navigating away from a screen or tearing down a feature.

Full Example

A realistic todo list feature with async persistence:
Todo Feature

Next Steps

Reducers

Pure business logic functions that transform state.

Middleware

Cross-cutting concerns like logging, analytics, persistence.

Router

Type-safe navigation driven by state.

UI Components

Build views with PrismUI components that bind to stores.